1004 Find Integer
Problem
people in USSS love math very much, and there is a famous math problem .
give you two integers , ,you are required to find integers , such that a^n+b^n=c^n
Input
one line contains one integer T;(1<=T<=1000000)
next lines contains two integers n,a; (0 ≤ n ≤ 1000 000 000,3 ≤ a ≤ 40000)
Output
print two integers b,c, if b,c exits; (1<=b,c<=1000 000 000)
else print two integers -1 -1 instead.
Sample Input
1
2 3
Sample Output
4 5
解析:
费马大定理可知,n>2或n<=0,不存在正整数解,n=1的时候好构造,n=2的时候就不好构造了。
这里有一个毕达哥拉斯三元组解析点这里
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
ll a, n;
scanf("%lld%lld", &n, &a);
if (n > 2)printf("-1 -1\n");
else
{
if (n == 0)printf("-1 -1\n");
else if (n == 1)printf("%lld %lld\n", 1, a + 1);
else if (n == 2)
{
if (a & 1)
{
ll x = (a - 1) / 2;
ll c = x*x + (x + 1)*(x + 1);
printf("%lld %lld\n", c - 1, c);
}
else
{
ll x = a / 2;
printf("%lld %lld\n", x*x - 1, x*x + 1);
}
}
}
}
}
模板2:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
int t,a,n;
ll b,c;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&a);
if(n>2 || n==0)
puts("-1 -1");
else if(n==1) {
printf("1 %d",a+1);
} else {
if(a&1) {
c=(a*a+1)/2;
b=c-1;
} else {
c=(a*a/2+2)/2;
b=c-2;
}
printf("%lld %lld\n",b,c);
}
}
}