Beijing in 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.

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.

OutputFor each test case, in a separate line, please output the result.

Sample Input
1 10000
0 0
Sample Output
5776
题意:这还是一个关于A^B的因子和取模于k的余数,只不过时求出来的因子和作为2008的指数然后再求关于k的余数;A为已知的数,也就是2008;
解题思路:整数的唯一分解定律:任意整数都有且只有一种方式写出其素因子的乘积表达式,所以2008=2^3*251;所以乘积的素因子就只有2和251,然后利用求因子和的公式即可;
注意:求等比数列求和时的一些注意事项。

#include 
#include 
using namespace std;
long long p(long long a,long long b,int k)
{
    long long x=1;
    a=a%k;
    while(b)
        {
            if(b%2==1)
                x=(x*a)%k;
            b/=2;
            a=a*a%k;
        }
    return x;
}
int main()
{
    long long n;
    int k;
    while(scanf("%lld%d",&n,&k)!=EOF)
        {
            if(!n&&!k) break;
            long long m=250*k;
            long long s;
            s=(p(2,3*n+1,m)-1)*(p(251,n+1,m)-1)%m/250;
            long long ans=p(2008,s,k);
            cout<<ans<<endl;
        }
    return 0;
}

你可能感兴趣的:(Beijing in 2008(数论))