Beijing 2008(快速幂模+逆元不存在时的处理)

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=( 2008 n 的因子和)mod k,输出 ( 2008 m )mod k

分析:

看到题意的时候很激动,这不就是求因子和公式吗,结果没有思考就直接套上去了
连样例都过不了
?????


#让我们细细分析:

根据算术基本定理可分解
2008 = 2 3 251

即素数幂积的形式

那么

2008 n = 2 3 n 251 n

再次,按照常人的想法下一步当然应该套用公式了

M = ( 2 0 + 2 1 + . . . + 2 3 n ) ( 251 0 + 251 1 + . . . + 251 n )   %   k

        = 2 k + 1 1 2 1 251 r + 1 1 250   %   k

        = ( 2 3 n + 1 1 ) 251 n + 1 1 250   %   k

因为涉及到了除法,又涉及到了取模,那么我们脑子里第一个想到的当然是求逆元
但是这时问题就出现了求250的逆元。

根据费马小定理

a p 1 1   ( m o d   p )

其中a,p必须是互质的才能根据费马小定理求a的逆元

而题目中的250和k并不一定互质因此可能不存在逆元,所给样例便是这样,因此样例不过。。。

那么我们必须转化

M = ( 2 3 n + 1 1 ) 251 n + 1 1 250   %   k

也可以表示成:

M ( 2 3 n + 1 1 ) 251 n + 1 1 250     ( m o d   k )

两边同乘一个数不改变同余性质那么我们给两边同乘250

得到:

250 M ( 2 3 n + 1 1 )   ( 251 n + 1 1 )     ( m o d   250 k )

这样我们就不用再求逆元了,直接快速幂取模即可

最后得到的是250M,所以结果除去250才是M

在这个地方相信大家都会有一个疑问,为什么这里就可以除去250了???
下面首先放上一个结论:
x d % k = x % ( d k ) d
这里不进行对这个式子的推导,只证明为什么这个式子是正确的,即根据结论说明其正确性
证明:
首先x必须被d整除,因为如果不整除除完得到一个小数那还取jb模,就没有意义了
这样我们可以得到 x d = k t + R
而这个R就是我们实际 x d % k 的正确结果
R 便是我们需要的,我们的所有努力都是为了求出R
即:
x d R (   m o d   k )
继续 x d = k t + R
我们完全可以等式两边同乘d,等号不变得到
x = d k t + R d
这个时候余数变成了Rd
根据结论我们对其取模dk
得到余数Rd
因此为了得到R
只需要 R = R d d
因此便证明了该结论的正确性
注意:!!!!!!!!
这种方法看上去很美,因为他看上去既可以用来求逆元存在情况也可以用来求逆元不存在的情况,那么我们一直用这个不就行了吗?
但是有两种情况并不适用
1.如果dk大小超了类型范围,而我们不能再对这个模数取模了(对谁取模。。)所以不行
2.这种方法只能求一个整体的除式mod一个数,而不能分步用该方法求,然后再把结果乘起来,为什么不行?
证明:
假设要求 x d y e   % k
根据结论我们只能得到:
x d y e   % k = x y   % ( d e k ) d e
但是我们不能分布求,再把结果乘起来
即:
x d y e   % k = x   % ( d k ) d y   % ( e k ) e = x % ( d k ) y % ( e k ) d e

x % ( d k ) y % ( e k ) d e x y   % ( d e k ) d e 并不一定是一样的吧,因为他们取模的数都不一样

然后在快速幂取模求 2008 M 即可


对这些概念定理掌握的还是不深入啊!

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k;
ll q_pow(ll a,ll b,ll mod){
    ll ans = 1;
    while(b){
        if(b & 1)
            ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans % mod;
}
int main(){
    while(~scanf("%lld%lld",&n,&k),n+k){
        ll a = (q_pow(2LL,3LL * n + 1,250LL * k) - 1) * (q_pow(251LL,n + 1,250LL * k) - 1) % (250 * k);
        a = (a % (250 * k) + (250 * k)) % (250 * k);
        a /= 250;
        printf("%lld\n",(q_pow(2008,a,k) + k) % k);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/81158962