2018中國大學生程序設計競賽 - 網絡選拔賽(部分題解)

Find Integer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6597    Accepted Submission(s): 1852
Special Judge
c++

Problem Descriptionui

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.
spa

Inputcode

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)
ip

Outputci

print two integers b,c if b,c exits;(1≤b,c≤1000,000,000);
else print two integers -1 -1 instead.
it

Sample Inputio

1class

2 3require

Sample Output

4 5

題意:現有一個式子 a^n + b^n = c^n 給一個n和a, 求出b, c.

題解:根據費馬大定理,咱們知道了,當n>2是,爲無解,另n=0,也無解,所以n=1,

n=2時:即在一組勾股數中,當最小邊爲奇數時,它的平方恰好等於另外兩個連續的正整數之和.勾股數公式一:(2n+1,2n^2+2n,2n^2+2n+1)(n爲正整數)

即在一組勾股數中,當最小邊爲偶數時,它的平方恰好等於兩個連續整數之和的二倍,勾股數公式二:(2n,n^2-1,n^2+1)(n≥2且n爲正整數)

當n>=3, a^n + b^n = c^n 沒有正整數解。(費馬大定理)

#include<bits/stdc++.h>
#define LL long long
using namespace std;
///2n+1,2n2+2n,2n2+2n+1
/// 2n,n2-1,n2+1
int main() {
    int T, n, a;
    scanf("%d", &T);
    while(T--) {
        int k, b, c;
        scanf("%d%d", &n, &a);
        if(n == 0 || n > 2)printf("-1 -1\n");
        else {
            if(n == 1) printf("1 %d\n", a + 1);
            else if(n == 2){
                if(a % 2 == 1) {
                    k = (a-1)/2;
                    b = 2*k*k+2*k;
                    c = b + 1;
                }
                else {
                    k = a/2;    
                    b = k*k-1;
                    c = k*k+1;
                }
                printf("%d %d\n", b, c);
            }
        }
    }
    return 0;
}
相關文章
相關標籤/搜索