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;
} 

 

全部评论

相关推荐

从24年初开学开始接触到前端,和实验室几个同学一起学习,可似乎我总比他们慢一步,每每学完一个地方,我掌握的程度好像都不比他们,第一次实验室的任务实战,我两眼一抹黑,完全不知道从何下手,而他们却是游刃有余,可我当时没有丧气,只有一个念头,既然学习能力不如他们,那我就拿更多的时间去学,于是我把打游戏,运动锻炼的时间也拿来学习。到了暑假,实验室一起做项目,为了可以更好的参与进去,于是我暑假开始留校和同学师哥一起做项目,每天早上九点多去实验室,晚上十点多回宿舍,校田径队的训练没有去,中间也只回家待了一周。到暑假结束开学之后,一位很优秀的师哥拿到了几个offer,我从他身上看到了希望,双非本科就业的希望...
offer求求哩:我的评价是认知低,建议多看书,认知低的一个表现是人生仿佛没考上大学就是进厂,考上了就是考研考公找工作。股市里有一个很有意思的故事,说的是当门口大妈都在谈论股票的时候,说明行情已经见顶了。当你的父母在某些事上没有成功却支持你说明事情可能已经不可靠了,但在某些事上反对你,说明这件事可能还有成功的可能。(仅个人观点)😆😆
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务