2018中国大学生程序设计竞赛 - 网络选拔赛 --1004 Find Integer(费马大定理-a值奇偶数列法则)

Find Integer

Problem Description

people in USSS love math very much, and there is a famous math problem .


give you two integers n,a,you are required to find 2 integers b,c such that an+bn=cn.

Input

one line contains one integer T;(1≤T≤1000000)

next T 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.

 

扫描二维码关注公众号,回复: 3049349 查看本文章

Sample Input

 

1 2 3

 

Sample Output

 

4 5

题意:

给你 n , a , 让你求 b , c ,满足 a ^ n + b ^ n = c ^ n.

费马大定理内容:

当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解。

平方整数解a^2+^b2=c^2的a值奇偶数列法则:

当a 为奇数时:

 if(a%2){
                ll t = (a-1)/2;
                c = t*t+(t+1)*(t+1);
                b = c-1;
            }

当a 为偶数时:

else{
                ll t = (a+2)/2;
                c = 1+(t-1)*(t-1);
                b = c-2;
            }

这个式子可以由这些规律推出来

3  4   5
5  12  13
7  24  25
8  15   17
12  35  37
20  21  29

最后一个样例也可以是20  99  101  , 只要输出任意一组符合条件的即可

也可以是 

if(a%2 == 1)
{
	t = (a-1)/2;
	b = 2*t*t + 2*t;
	c = 2*t*t + 2*t + 1;
 } 
 else
 {
 	t = a / 2;
 	b = t*t - 1;
 	c = t*t + 1;
 }

下面AC代码:

#include <bits/stdc++.h>
#define ll long long
ll T, a, b, c, n;

int main(){
    scanf("%lld", &T);
    while(T--){
        scanf("%lld %lld", &n, &a);
        if(n == 2){
            if(a%2){
                ll t = (a-1)/2;
                c = t*t+(t+1)*(t+1);
                b = c-1;
            }
            else{
                ll t = (a+2)/2;
                c = 1+(t-1)*(t-1);
                b = c-2;
            }
            printf("%lld %lld\n", b, c);
        }
        else if( n == 1){
            printf("1 %lld\n", a+1);
        }
        else{
            printf("-1 -1\n");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41593380/article/details/82057258