poj 1845 + hdu 1452 快速幂+模逆元运算(扩展gcd)+数论求因子和

poj 1845

//poj 1845
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e5+5;
const int MD=9901;
typedef long long LL;
int p[maxn],a[maxn];
void ex_gcd(LL n,LL k,LL &x,LL &y)
{
    if(k==0)
    {
        x=1;
        y=0;
        return;
    }
    ex_gcd(k,n%k,x,y);
    LL t=x;
    x=y;
    y=t-n/k*y;
}
LL pow_mod(LL n,LL k)
{
    LL res=1;
    while(k)
    {
        if(k&1) res=res*n%MD;
        k>>=1;
        n=n*n%MD;
    }
    return res;
}
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)==2)
    {
        if(n==0) {
                cout<<"0"<<endl;
                continue;
        }
        if(n==1||k==0) {
            cout<<"1"<<endl;
            continue;
        }
        int cnt=0;
        for(int i=2;i*i<=n;i++)
        {
            if(n==1) break;
            if(n%i==0) {
                p[cnt]=i;
                a[cnt]=0;
                while(n%i==0) {
                    a[cnt]++;
                    n/=i;
                }
                cnt++;
            }
        }
        if(n!=1) {
            p[cnt]=n;
            a[cnt]=1;
            cnt++;
        }
        //利用因子求和公式
        for(int i=0;i<cnt;i++)
            a[i]*=k;
        //得到n^k的唯一分解式
        LL sum=1;
        //等比数列求和公式
        for(int i=0;i<cnt;i++)
        {
            if(p[i]%MD==1) {                      //影响到求逆元,只有当两个数互质时,才能求解逆元
                sum=sum*(a[i]+1)%MD;
                continue;
            }
            sum=sum*((pow_mod(p[i],a[i]+1)-1+MD)%MD)%MD;            //分子
            LL x,y;
            ex_gcd(p[i]-1,MD,x,y);
            x=(x%MD+MD)%MD;                 //无论x是正还是负数都可以写成这种形式
            sum=sum*x%MD;                   //分母
        }
        cout<<sum%MD<<endl;
    }
    return 0;
}

hdu 1452

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const LL MD=29;
void ex_gcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0) {
        x=1;
        y=0;
        return;
    }
    ex_gcd(b,a%b,x,y);
    LL t=x;
    x=y;
    y=t-a/b*y;
}
LL pow_mod(LL n,LL k)
{
    LL res=1;
    while(k)
    {
        if(k&1) res=res*n%MD;
        k>>=1;
        n=n*n%MD;
    }
    return res;
}
int main()
{
    LL p[5],a[5],b[5];
    int cnt=0,n=2004;
    for(int i=2;i*i<=2004;i++)
    {
        if(n==1) break;
        if(n%i==0) {
            p[cnt]=i;
            a[cnt]=0;
            while(n%i==0) {
                n/=i;
                a[cnt]++;
            }
            cnt++;
        }
    }
    if(n!=1) {
        p[cnt]=n;
        a[cnt]=1;
        cnt++;
    }
    //for(int i=0;i<cnt;i++)
     //   cout<<p[i]<<" "<<a[i]<<endl;
     int x;
     while(scanf("%d",&x)==1&&x)
     {
         //得到唯一分解式
            memcpy(b,a,sizeof(a));
            for(int i=0;i<cnt;i++)
                b[i]*=x;
            LL sum=1;
            for(int i=0;i<cnt;i++)
            {
                sum=sum*((pow_mod(p[i],b[i]+1)-1+MD)%MD)%MD;
                //cout<<sum<<endl;
                LL x1,y;
                ex_gcd(p[i]-1,MD,x1,y);
                //cout<<x1<<endl;
                x1=(x1%MD+MD)%MD;
                //cout<<x1<<endl;
                sum=sum*x1%MD;
               // cout<<sum<<endl;
            }
            cout<<sum%MD<<endl;
     }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值