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

题意:有几个变量N.K.M.S;S等于2008的N次方的因子和,M=S%K;求2008^M % K = 5776.

思路
首先我们先来看这一组数据1、2、4、8、251、502、1004、2008。

我们发现可一些写成2^0, 2^1, 2^2, 2^3, 251 * 2^0, 251 * 2^1, 251 * 2^2, 251 * 2^3;

所以2008 ^N= 2^3 * 251 * 2^0 = 2^2 * 251 * 2^1……

因此,2008^N= 2^(3N) * (251 * 2^0) ^N=……,由此可以看出 2^(3N) 和251^N都是2008 ^N的因子,

我们知道a ^n等于n个a想乘,即a ^m(0<=m<=n)是a的因子,所以2的0~3N次方都是 2^(3N)的因子即2008 ^N的因子,同理251的0~N次方也都是2008 ^N的因子

此时我们可以求得2的0~3N的和是2 ^(3n+1)-1,251的0~N次方的和是(251 ^(n+1)-1)/250.

又因为2008 ^N =2^(3N) * (251 * 2^0) ^N

所以任意的2 ^a*251 ^b(0<=a<=3N, 0<=b<=N)都是2008 ^N的因子
(都可以通过乘2和251得到 2^(3N) * (251 * 2^0) ^N)

所以2的0~3N次方与251的 0~N次方的乘积所构成的集合就是2008 ^N的所有因子
(因为是2008的N次幂,所以因子只能在2008因子的幂以及相关运算中找,2的0~3N次方与251的 0~N次方的乘积所构成的集合已经是该运算所构成的最大集合了)

所以S=(2 ^(3n+1)-1)*(251 ^(n+1)-1)/250;

M=S%K=(2 ^(3n+1)-1)*(251 ^(n+1)-1)/250%K;

因为S太大,所以要先进行取模运算:((2 ^(3n+1)-1)%(250K)(251 ^(n+1)-1)%(250*K)/250)%K

最终可求得:2008^M % K

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define LL long long
LL n,k,m,s1,s2,w;
LL pow_mod(LL a, LL b,LL MOD)
{
    if(b == 0) return 1;
    LL ret = pow_mod(a, b/2,MOD);
    ret = ret * ret % MOD;
    if(b % 2 == 1) ret = ret * a % MOD;
    return ret;
}

int main()
{
    while(scanf("%lld%lld",&n,&k)&&(n+k))
    {
        s1=pow_mod(2,3*n+1,k*250)-1;
        s2=pow_mod(251,n+1,k*250)-1;
        m=(s1*s2/250)%k;
        w=pow_mod(2008,m,k);
        printf("%lld\n",w);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值