首页 > 其他 > 详细

HDU-1852-Beijing 2008-一个神奇的公式求逆元

时间:2019-08-21 01:11:55      阅读:80      评论:0      收藏:0      [点我收藏+]
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. 

InputThe 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

题意:
好好理解看清楚:
S=2008^x,所有因子和 get the sum S of all positive integer divisors of 2008 N.
M=S%k(已知k) the rest of the division of S by K. The result is kept as M
求2008^M%k

 1 #include<stdio.h>
 2 typedef long long ll;
 3 
 4 ll ksm(ll x,ll n,ll mod)
 5 {
 6     ll res=1;
 7     while(n>0)
 8     {
 9         if(n&1)
10             res=res*x%mod;
11         x=x*x%mod;
12         n>>=1;
13     }
14     return res%mod;
15 }
16 
17 
18 //S=2008^x,所有因子和   get the sum S of all positive integer divisors of 2008 N.
19 //M=S%k(已知k)  the rest of the division of S by K. The result is kept as M
20 //求2008^M%k
21 
22 int main()
23 {
24     ll n,k;
25     while(~scanf("%lld %lld",&n,&k))
26     {
27         if(n==0&&k==0)
28             break;
29         ll k2=ksm(2,3*n+1,k*250);
30         if(k2-1<0)
31             k2=k2-1+k;
32         else
33             k2--;
34 
35         ll k251=ksm(251,n+1,250*k);
36         if(k251-1<0)
37             k251=k251-1+k;
38         else
39             k251--;
40 
41         ll M=k2*(k251)%(250*k)/250;
42      //   ll M=k2*(k251/k)%(250*k);
43   //  ll M=k2*(k251/250)%(250*k); WA,注意一下
44         ll ans=ksm(2008,M,k);
45         printf("%lld\n",ans);
46     }
47     return 0;
48 }

 

HDU-1852-Beijing 2008-一个神奇的公式求逆元

原文:https://www.cnblogs.com/OFSHK/p/11386302.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!