hdu 1098Ignatius's puzzle(math)
阿新 • • 發佈:2018-12-12
Ignatius's puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7652 Accepted Submission(s): 5326
Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal nonegative integer a,make the arbitrary integer x ,65|f(x)if
no exists that a,then print "no".
Input The input contains several test cases. Each test case consists of a nonegative integer k, More details in the Sample Input.
Output The output contains a string "no",if you can't find a,or you should output a line contains the a.More details in the Sample Output.
Sample Input
Sample Output 22 no 43
思路:通過遞推公式推導。
f ( x + 1) - f ( x ) = ...(省略明顯被65整除的部分) + 18 + k*a;
則只需求出最小的a,使18 + k*a 能被65整除即可。
方法一: 利用歐幾里德擴充套件公式求解。
程式碼如下:
#include <cstdio> void gcd_ex(int a, int b, int& d, int& x, int& y){ if(!b){d = a; x = 1; y = 0;} else {gcd_ex(b, a%b, d, y, x); y -= x*(a/b);} } int main(){ int k; //freopen("in.txt", "r", stdin); while(~scanf("%d", &k)){ int d, x, y; gcd_ex(-65, k, d, x, y); int mod = -65/d < 0 ? 65/d: -65/d; if(-18%d != 0){printf("no\n");} else {printf("%d\n", ((-18/d*y)%mod+mod)%mod);} } return 0; }
方法二: 分析:若18 + k*a 能被65整除,則必存在a = a + k*65,(k = 0, 1, 2....)使原式依然成立,則a的最小值必在[0, 65) 區間內。列舉即可。
程式碼:
#include <cstdio> int main(){ int k, i; // freopen("in.txt", "r", stdin); while(~scanf("%d", &k)){ for(i = 0; i < 65; i++){ if((18 + k*i)%65 == 0) {printf("%d\n", i);break;} } if(i == 65) printf("no\n"); } return 0; }
Author eddy