http://acm.hdu.edu.cn/showproblem.php?pid=1852
Beijing 2008
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 917 Accepted Submission(s): 394
Problem Description
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.
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
Author
lxlcrystal@TJU
题解:http://blog.youkuaiyun.com/weixin_36571742/article/details/76832624。这题是类似的一题,可以除法可以通过求逆元。因为那题的mod是29,gcd(2*166,29)==1,存在逆元。但是这里的gcd(250,k)不一定满足等于1,也就是说不一定存在逆元。那么观察我们要求的m,m肯定有250这个分母,所以可以表示成m=x/250。m%k=(x/250)%k转化为(x%(250*k))/250。求变成了先除法再取余了。
代码:
#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 100000 + 5;
LL n,k,a,b,ans;
LL quick(LL a,LL b,LL mod){
LL ans=1;
while(b){
if(b&1){
ans=(ans*a)%mod;
}
b>>=1;
a=(a*a)%mod;
}
return ans;
}
int main(){
while(~scanf("%lld%lld",&n,&k)){
if(n==0&&k==0){
break;
}
a=quick(2,3*n+1,250*k);
b=quick(251,n+1,250*k);//mod取250*k,目的是先要保留250这个因子,放在取余后再除
a--,b--;
ans=(a*b)%(250*k);
ans/=250;
ans=((ans%k)+k)%k;
printf("%d\n",quick(2008,ans,k));
}
return 0;
}