1. 程式人生 > >【2018 CCPC網絡賽】1004 - 費馬大定理&數學

【2018 CCPC網絡賽】1004 - 費馬大定理&數學

偶數 沒有 關於 都沒有 == name span 意義 while

題目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6441

Knowledge Point:

  1. 費馬大定理:當整數n >2時,關於x, y, z的方程 x^n + y^n = z^n 沒有正整數解。

  2. 0^0次沒有意義!!

所以我們知道 n=0, n>2的時候都沒有正整數解;

n=1 時顯然 b=1, c=a+1 為一組整數解;

n=2 時,a2 = c2-b2 = (c+b)*(c-b);

令x = c+b, y = c-b; 則有 a2 = x*y; b = (x-y)/2, c = (x+y)/2;

因為 b, c 都是正整數,故有 x>y, 且 x, y 奇偶性相同;

當 a為奇數時,x = a2 , y = 1;

當 a為偶數時,a2 和 1一奇一偶,不滿足條件,故可令 x = a2/ 2, y = 2;

另外註意數據輸入量過大,用 scanf 避免超時;

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int T, n, a;
 8     scanf("%d", &T);
 9     
10     while(T--) {
11         scanf("
%d %d", &n, &a); 12 13 if(!n || n>2) printf("-1 -1\n"); 14 else { 15 if(n == 1) printf("%d %d\n", 1, a+1); 16 else { 17 int t = a*a; 18 if(a&1) printf("%d %d\n", (t-1)/2, (t+1)/2); 19 else
printf("%d %d\n", t/4-1, t/4+1); 20 } 21 } 22 } 23 return 0; 24 }

【2018 CCPC網絡賽】1004 - 費馬大定理&數學