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".
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
11 100 9999
Sample Output
22 no 43
Author
eddy
思路:通过递推公式推导。
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