CCPC-Wannafly Winter Camp Day7 D---二次函数【数论】【构造】

时间:2022-04-09 01:40:25

CCPC-Wannafly Winter Camp Day7 D---二次函数【数论】【构造】

题意:

有三个二次函数,分别是$x^2 + a_1x + b_1$, $x^2 + a_2x + b_2$, $x^2 + a_3x + b_3$

现在要找三个整数$x_1, x_2, x_3$, 使得三个函数值中至少有两个相等。

思路:

主要的难点是要找三个整数。

Camp时候洪老师说的平移啥啥的,理解不了......

看了网上另一个题解的思路。

假设两个二次函数相等的函数值是$y$, 并假设是第一个和第二个相等

那么我们可以知道$x_1 =- \frac{a_1 + \sqrt{a_1^2 - 4(b_1 - y)}}{2}, x_2 =- \frac{a_2 + \sqrt{a_2^2 - 4(b_2 - y)}}{2}$

令$T^2 = a_1^2 - 4(b_1 - y), t^2 = a_2^2 - 4(b_2 - y)$

可以得到$T^2 - t^2 = a_1^2 - 4b_1 - a_2^2 + 4b_2 = (T+t)(T-t)$

于是我们进行因式分解,枚举$T^2-t^2$的因子,判断得到的$x_1, x_2$是否为整数。

对于$T^2-t^2$为0和1时进行特判就行了。

对于每一对$a$和$b$,判断是否有这样两个整数解。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<cmath> using namespace std;
typedef long long LL; LL a[], b[];
int t;
LL ans1, ans2; bool solve(LL a1, LL b1, LL a2, LL b2)
{
int sub = abs(a1 * a1 - * b1 - a2 * a2 + * b2);
if(sub == ){
if((a1 - a2) % == ){
ans1 = ;
ans2 = (a1 - a2) / ;
return true;
}
else return false;
}
else if(sub == ){
if(a1 % != a2 % ){
if(a1 * a1 - * b1 > a2 * a2 - * b2 && a1 % ){
ans1 = ( - a1) / ;
ans2 = -a2 / ;
return true;
}
else if(a1 * a1 - * b1 < a2 * a2 - * b2 && a2 % ){
ans2 = ( - a2) / ;
ans1 = -a1 / ;
return true;
}
else return false;
}
return false;
}
else{
for(int i = ; i * i < sub; i++){
if(!(sub % i) && !((sub / i + i) % ) && !((i - sub / i) % )){
LL T = (sub / i + i) / ;
LL t = (i - sub / i) / ;
if(a1 * a1 - * b1 < a2 * a2 - * b2)swap(T, t);
if(!((T + a1) % ) && !((t + a2) % )){
ans1 = -(a1 + T) / ;
ans2 = -(a2 + t) / ;
return true;
}
}
}
return false;
}
} void work()
{
for(int i = ; i < ; i++){
for(int j = i + ; j < ; j++){
if(solve(a[i], b[i], a[j], b[j])){
if(i == )
{
printf("%lld %lld %lld\n", ans1, j == ?ans2:, j==?:ans2);
//return;
}
else{
printf("0 %lld %lld\n", ans1, ans2);
//return ;
}
return;
}
}
}
} int main()
{
scanf("%d", &t);
while(t--){
for(int i = ; i < ; i++){
scanf("%lld%lld", &a[i], &b[i]);
}
work();
} return ;
}