HDU——1852 Beijing 2008(除法取模,不能取逆元)

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

题意:求2008^n的所有因子和s对k取余的值作为m,然后求2008^m对k取余。

题解:注意:不能求逆元,因为不能保证gcd(250,k)=1.请看另一种方法:公式为:x/y%k=x%(k*y)/y 做法:用求因子和的公式,2008=2^3*251,则:2008^n=2^3n*251^n所以因子和为:s=(2^(3^n+1)-1)*(251^(n+1)-1)/250

那么m=s%k=(2^(3^n+1)-1)*(251^(n+1)-1)%(250*k)/250,请看代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
long long quick(long long a,long long b,long long c){//快速幂模板
	long long ans=1;
	a=a%c;
	while(b!=0){
		if(b&1) ans=(ans*a)%c;
		b>>=1;
		a=(a*a)%c;
	}
	return ans;
}
int main(){
	long long n,k,t,m;
	while(cin >> n >> k,n+k){
		k=k*250;//推的公式
		t=(quick(2,3*n+1,k)-1)*(quick(251,n+1,k)-1);//推的公式
		m=t%k/250;//推的公式
		k=k/250;//注意k是原始值,所以要除以250回到原始值
		cout << quick(2008,m,k) << endl;
	}
	return 0;
} 

 

全部评论

相关推荐

03-12 21:51
门头沟学院 C++
pdd卡怎么严吗&nbsp;笔试a出来两道,第三题a出来20%直接给挂了😭😭😭
鳍足目:我a了2.5道也挂了,但是组里同学只a了1.6道进面了,而且我和他都是无实习,本硕同校,感觉全是玄学
点赞 评论 收藏
分享
01-23 14:54
同济大学 Java
热爱敲代码的程序媛:给你提几点【专业技能】这个模块里面可优化的地方:1.【具备JVM调优经验】可以去b站上搜一下JVM调优的视频,估计一两个小时凭你的学习能力就能掌握JVM调优的实践方面的技能。2.【MySql优化】MySql这一栏,你去b站或者找个博客看看MySql优化,学一下,如果你本身比较熟悉MySql语句的话,那基本半天时间凭你的学习能力MySql语句优化方面的技能你也能掌握个差不多。以上1,2两点主要是因为我看你专业技能大部分都说的是偏理论,没有写应用。再就是最后,你结合你的项目,想一想你的项目中哪些sql语句是可以用MySql优化的,到时候你面试的时候也好结合着说一下。
点赞 评论 收藏
分享
01-24 04:44
门头沟学院 Java
数学转码崽:项目感觉有点简单,再加上学历不是92的话,大厂实习很难过筛吧,即使给几个面试,感觉也通过不了,还是放低预期,先去中厂沉淀吧,暑期实习可以试着冲大厂,如果非大厂不去的话,不如去考研,双非学历真的硬伤
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务