Beijing 2008
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
Sample Output
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.
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.
1 10000 0 0
5776
题意:给定N和K,
首先求S,S为2008^N所有因子之和,分解2008^N=(2^3*251)^N=(2^3N )*(251^N)
2^3N , 因数有2^0 , 2^1 , 2^2,,,,,,,,2^3N
251^N ,因数有251^0,251^1,,,,251^N
令A=2^0 +2^1 + 2^2+,,,,,,,+2^3N
B=251^0+251^1+,,,,+251^N
所有因子之和=A*B=S
等比求和得到A=2^(3*N+1)-1;
B=(251^(N+1)-1)/250;
直接求S肯定超long long范围
然后求M,M=S mod K;
因此在求S时取模,在快速幂中取模
最后求2008^M%K
//快速幂
ll pow(ll a,ll m,ll mod)//分别是底数,幂,模
{
int res = 1;
while(m){
if(m&1){
res=res*a%mod;
}
a=a*a%mod;
m>>=1;
}
return res;
}
另外在求B时:因为B=[(251^(N+1)-1)/250 ]mod K
把250拿出来 B=(251^(N+1)-1)mod(250*K)/250;
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#define ll long long
using namespace std;
ll n,k;
ll pow(ll a,ll m,ll mod)
{
ll res=1;
while(m){
if(m&1){
res=(res*a)%mod;
}
a=a*a%mod;
m>>=1;
}
return res;
}
int main()
{
while(scanf("%lld %lld",&n,&k)){
if(n==0&&k==0) break;
ll M=250*k;
ll A=pow(2,3*n+1,M)-1;
// cout<<A<< endl;
ll B=pow(251,n+1,M)-1;
//cout << B << endl;
ll m=(A*B)%M/250;
//printf("----%lld\n",m);
ll s=pow(2008,m,k);
printf("%lld\n",s);
}
return 0;
}