閱讀967 返回首頁    go 阿裏雲 go 技術社區[雲棲]


URAL 1141 計算模n的e次根

給出 n=p*q p,q為素數 gcd(e, (p-1)*(q-1)) = 1, e < (p-1)*(q-1) ) 求m滿足方程me = c (mod n)。

摘自《數論概論》



#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
using namespace std;
#define maxn 32050
typedef long long ll;
ll phi[maxn];
void getphi()
{
    for(int i=1; i<maxn; i++) phi[i]=i;
    for(int i=2; i<maxn; i+=2) phi[i]>>=1;
    for(int i=3; i<maxn; i+=2)
        if(phi[i]==i)
            for(int j=i; j<maxn; j+=i)
                phi[j]=phi[j]/i*(i-1);
}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
    if(b==0)
    {
        x=1,y=0,d=a;
        return;
    }
    exgcd(b,a%b,d,x,y);
    ll temp=x;
    x=y,y=temp-(a/b)*y;
}
ll exp_pow(ll a,ll b,ll c)
{
    a%=c;
    ll q=1;
    while(b)
    {
        if(b&1) q=q*a%c;
        b>>=1,a=a*a%c;
    }
    return q;
}
int main()
{
    getphi();
    int t;
    ll e,n,c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d%I64d",&e,&n,&c);
        ll u,v,d;
        exgcd(e,phi[n],d,u,v);
        u=(u%phi[n]+phi[n])%phi[n];
        printf("%I64d\n",exp_pow(c,u,n));
    }
    return 0;
}


最後更新:2017-04-03 16:49:07

  上一篇:go poj 2068 NIM 博弈+dp
  下一篇:go 基於xml文件實現係統屬性配置管理