Description has only two Sentences
**Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1108 Accepted Submission(s): 345
**
Problem Description
an = X*an-1 + Y and Y mod (X-1) = 0.
Your task is to calculate the smallest positive integer k that ak mod a0 = 0.
Input
Each line will contain only three integers X, Y, a0 ( 1 < X < 231, 0 <= Y < 263, 0 < a0 < 231).
Output
For each case, output the answer in one line, if there is no such k, output "Impossible!".
Sample Input
2 0 9
Sample Output
1
Author
WhereIsHeroFrom
思路:baby_step;
先构造等比数列,a[n]=X*a[n-1]+Y;那么(a[n]+k)=X(a[n-1]+k);那么展开求得k=(Y)/(X-1);那么a[n]+k=(a[0]+k)*(x)^n;
然后a[n]=(a[0]+k)*(x)^n-k;那么要求最小的n满足a[n]%a[0]=0;那么就是求k*x^n%(a[0]) = k%a[0],那么这个用扩展baby_step;求下就可以了,复杂度(sqrt(a[0])*log(a[0]));
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
baby_step