广义欧拉降幂(欧拉定理)——bzoj3884,fzu1759

广义欧拉降幂对于狭义欧拉降幂任然适用

https://blog.csdn.net/qq_37632935/article/details/81264965?tdsourcetag=s_pctim_aiomsg

bzoj388

#include<bits/stdc++.h>
using namespace std;
#define ll long long 

ll Pow(ll a,ll b,ll p){
    ll res=1;
    while(b){
        if(b%2)
            res=res*a%p;
        b>>=1;a=a*a%p;
    }
    return res;
}
ll phi(ll x){
    ll res=x,tmp=x;
    for(ll i=2;i*i<=tmp;i++)
        if(tmp%i==0){
            res=res*(i-1)/i;
            while(tmp%i==0)tmp/=i;
        }
    if(tmp>1)    
        res=res*(tmp-1)/tmp;
    return res;
}

ll f(ll p){
    if(p==1)return 0;
    ll q=phi(p);
    return Pow(2,q+f(q),p);    
}

int main(){
    int t;ll p;cin>>t;
    while(t--){
        cin>>p;
        cout<<f(p)<<'\n';
    }
}
View Code

fzu1759 注意在计算phi时要用先除再乘,防止爆精度

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define ll long long 
#define maxn 1000005
char b[maxn];
ll A,C,B;

ll phi(ll x){
    ll res=x,tmp=x;
    for(ll i=2;i*i<=tmp;i++)
        if(tmp%i==0){
            res=res-res/i;
            while(tmp%i==0)
                tmp/=i;
        }
    if(tmp>1)
        res=res-res/tmp;
    return res;
}
ll Pow(ll a,ll b,ll p){
    ll res=1;
    while(b){
        if(b%2)
            res=res*a%p;
        b>>=1;a=a*a%p;
    }
    return res;
}

int main(){
    while(cin>>A){
        scanf("%s",b);
        cin>>C;
        ll p=phi(C);
        int len=strlen(b);
        B=0;
        for(int i=0;i<len;i++)
            B=(B*10+b[i]-'0')%p;
        cout<<Pow(A,B,C)<<'\n';
    }
}

 

posted on 2019-06-13 11:12  zsben  阅读(339)  评论(0编辑  收藏  举报

导航