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;
}


目录
相关文章
|
算法 前端开发
前端算法-最大三角形面积-鞋带公式&-海伦公式
前端算法-最大三角形面积-鞋带公式&-海伦公式
27 0
|
5月前
【每日一题Day350】LC2652倍数求和 | 数学+容斥原理
【每日一题Day350】LC2652倍数求和 | 数学+容斥原理
23 0
|
11月前
|
数据挖掘
[概率论]-离散型随机变量·二项分布
[概率论]-离散型随机变量·二项分布
80 0
|
12月前
7-9 计算摄氏温度
7-9 计算摄氏温度
49 0
|
存储
【CCCC】L2-018 多项式A除以B (25分),多项式除法
【CCCC】L2-018 多项式A除以B (25分),多项式除法
125 0
|
机器学习/深度学习
【CCCC】L3-023 计算图 (30分),dfs搜索+偏导数计算
【CCCC】L3-023 计算图 (30分),dfs搜索+偏导数计算
104 0
L1-004 计算摄氏温度 (5 分)
L1-004 计算摄氏温度 (5 分)
65 0
|
存储 算法 Python
【Python 百练成钢】高精度加法、阶乘计算、矩阵幂运算、矩阵面积交
【Python 百练成钢】高精度加法、阶乘计算、矩阵幂运算、矩阵面积交
162 0
【Python 百练成钢】高精度加法、阶乘计算、矩阵幂运算、矩阵面积交
|
C++
L1-004 计算摄氏温度
文章目录 L1-004 计算摄氏温度 (5 分) 总结
92 0
L1-004 计算摄氏温度
7-4 计算摄氏温度 (10 分)
给定一个华氏温度F,本题要求编写程序,计算对应的摄氏温度C。计算公式:C=5×(F−32)/9。题目保证输入与输出均在整型范围内。