Ignatius’s puzzle
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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:
, input a nonegative integer
, to find the minimal nonegative integer
, make the arbitrary integer
,
.
if no exists that
,then print "no"
.
Input
The input contains several test cases. Each test case consists of a nonegative integer , More details in the Sample Input.
Output
The output contains a string "no"
,if you can’t find
,or you should output a line contains the
.More details in the Sample Output.
Sample Input
11
100
9999
Sample Output
22
no
43
Reference Code
#include<bits/stdc++.h>
using namespace std;
int a,k,t;
void exgcd(int a,int b,int &x,int &y){
if (!b) x=1,y=0;
else exgcd(b,a%b,y,x),y-=a/b*x;
}
int main(){
while(~scanf("%d",&a)){
if (!(a%5)||!(a%13))
printf("no\n");
else{
exgcd(a,65,k,t);
k=(k*(-18)%65+65)%65;
printf("%d\n",k);
}
}
return 0;
}
Tips
看了Discuss里面的解答,似乎都不够严谨,笔者试着写一下自己的过程。
题目中要求
就等价于
这两个式子在当
和
时显然成立,下面讨论
和
的情形。此时,该式转化为
由费马小定理,
则该式继续等价为
则有
解得
因而,本题转化为求解不定方程
的解中
的最小正值。
由拓展欧几里得算法,该方程有解的条件为
而
所以要求
必须满足
当方程有解时,先求解出不定方程
的一个特解,再将其乘以
即为原方程的一个特解。然后再根据这个特解求
的最小正值。
至于特解的求解,可以利用
当然,最后这一部分也可以用暴力法,在
~
中寻找是否存在
满足