hdu1452(逆元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1452

题意:求2004^x的因子和s, 求出s%29;

思路:根据求因子和 可以知道

2004=22x * 3x * 167x

所以

  S=(22x+1-1)/(2-1) * (3x+1-1)/(3-1) * (167x+1-1)/(167-1) =  r/332

 其中r=(22x+1-1)*(3x+1-1)*(167x+1-1)  由于x很大(1<=x<=10000000),而mod=29,

根据费马小定理  :ap-1mod p =1 ,则a28mod 29=1,所以 abmod 29=abmod 28 mod 29

对于29,我们可求出332的逆元为9   

  那么用 r1=2(2x+1)mod28   %29

      r2=3(x+1)mod 28    %29

                  r3=22(x+1)mod28   %29(167%29=22)

   答案S= 9*r1*r2*r3%29;

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;

int main()
{
    ll r1,r2,r3,x;
    while(cin>>x&&x)
    {
        r1=r2=r3=1;
        for(int i=0;i<(2*x+1)%28;i++)
            r1=(r1*2)%29;
        r1=(r1-1+29)%29;//最小正数 
        for(int i=0;i<(x+1)%28;i++)
        {
            r2=(r2*3)%29;
            r3=(r3*22)%29;    
        }    
        r2=(r2-1+29)%29;
        r3=(r3-1+29)%29;
        cout<<9*r1*r2*r3%29<<endl;
    }    
    return 0;
} 
原文地址:https://www.cnblogs.com/xiongtao/p/10878426.html