題解報告:hdu 6441 Find Integer(費馬大定理+智慧數)
阿新 • • 發佈:2018-11-08
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
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 3Sample Output
4 5 解題思路:①費馬大定理:當整數n>2時,關於x, y, z的方程 x^n + y^n = z^n 沒有正整數解。 ②智慧數:一個自然數若能表示為兩個自然數的平方差,則這個自然數為“智慧數”。形如2k+1或4k的形式必為智慧數,k≥0。舉個栗子:驗證2687是否為智慧數,∵2687為奇數,∴設2687=2k+1(k為正整數),∴k=1343,∴2687=1344²-1343²,∴2687是智慧數。驗證16是否為智慧數,∵4|16,∴k=4,∴16=52AC程式碼:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int t,n,a; 4 int main(){ 5 while(~scanf("%d",&t)){ 6 while(t--){ 7 scanf("%d%d",&n,&a); 8 if(!n||n>2)printf("-1 -1\n");//無解 9 elseif(n==1)printf("%d %d\n",1,a+1);//輸出最小即可 10 else{ 11 a*=a; 12 if(a&1)printf("%d %d\n",a/2,a/2+1);//奇數 13 else{ 14 if(a%4)printf("-1 -1\n");//無解 15 else printf("%d %d\n",a/4-1,a/4+1); 16 } 17 } 18 } 19 } 20 return 0; 21 }