HDU 1852 - Beijing 2008(快速幂)

本文介绍了一个以2008年北京奥运会为主题的编程挑战题目,要求计算特定整数的正除数之和,并对结果进行模运算。通过使用高效的幂次运算和模运算技巧,该题目考验了选手的数学能力和编程实现能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Beijing 2008

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1284    Accepted Submission(s): 541


 

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 2008N. 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 2008M, 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, 2008M % 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

 

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define clr(a) memset(a,0,sizeof(a))

const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const int N = 1010;

LL n,k,m;

LL qkm(LL a, LL b, LL mode)
{
	LL sum = 1;
	while (b) {
		if (b & 1) {
			sum = (sum * a) % mode;
			b--;
		}
		b /= 2;
		a = a * a % mode;
	}
	return sum;
}


int main(){
    while(scanf("%d%d",&n,&k)!=EOF&&(n||k)){
        LL a = qkm(2,3 * n + 1,250 * k) - 1;
        LL b = qkm(251,n + 1,250 * k) - 1;
        m = (a * b) % (250 * k);
        m /= 250;
        LL ans = qkm(2008,m,k);
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值