问题:
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a little special somehow. You are looking forward to it, too, aren't you? Unfortunately there still are months to go. Take it easy. Luckily you meet me. I have a problem for you to solve. Enjoy your time.
Now given a positive integer N, get the sum S of all positive integer divisors of 2008 N. Oh no, the result may be much larger than you can think. But it is OK to determine the rest of the division of S by K. The result is kept as M.
Pay attention! M is not the answer we want. If you can get 2008 M, that will be wonderful. If it is larger than K, leave it modulo K to the output. See the example for N = 1,K = 10000: The positive integer divisors of 20081 are 1、2、4、8、251、502、1004、2008,S = 3780, M = 3780, 2008 M % K = 5776.
Input
The input consists of several test cases. Each test case contains a line with two integers N and K (1 ≤ N ≤ 10000000, 500 ≤ K ≤ 10000). N = K = 0 ends the input file and should not be processed.
Output
For each test case, in a separate line, please output the result.
Sample Input
1 10000
0 0
Sample Output
5776
题意: 输入n,k , 令m=(2008^n的因子和)mod k,输出 (2008^m)mod k。
思路:本题中,先求出m,即约数和,2008可以被分解为 2^3*251 ,250作为分母被除然后对k取模,但是K不一定是素数,即不一定与250互素,所以不能求逆元。我们应用公式 x/d%m = x%(d*m)/d 来求:所以在使用快速幂时 应对 d*m来取模,而不是m。
代码:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; typedef long long ll; ll pow(ll x,ll y,ll mod) { x=x%mod; ll sum=1; while(y>0) { if(y%2!=0) sum=(sum*x)%mod; x=x*x%mod; y=y/2; } return sum%mod; } int main() { int n,m; while(~scanf("%d%d",&n,&m)&&(n||m)) { ll a=(pow(2,3*n+1,m*250)-1)%(250*m); ll b=(pow(251,n+1,m*250)-1)%(250*m); ll k=a*b%(250*m)/250; printf("%lld\n",pow(2008,k,m)); } }