URAL 1141 计算模n的e次根

本文深入探讨了数论中求解同余方程的数学原理及算法实现,通过实例展示了如何利用扩展欧几里得算法、欧拉函数和幂次运算解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


# U225627 数星星 ## 题目背景 城市的天空没有星星…… ## 题目描述 天空中有一些星星,这些星星都在不同的位置,每颗星星都有一个坐标。 如果一个星星的左下方(包含正左和正下)有 $k$ 颗星星,就说这颗星星是 $k$ 级的。 ![](https://img-blog.csdnimg.cn/20210223141756101.png#pic_center) $$Pic\ _1$$ E.G: $Pic\ _1$ 中星星 $5$ 是 $3$ 级的($1, 2, 4$ 在它左下),星星 $2, 4$ 是 $1$ 级的。 $Pic\ _1$ 中有 $1$ 个 $0$ 级,$2$ 个 $1$ 级,$1$ 个 $2$ 级,$1$ 个 $3$ 级的星星。 给定星星的位置,要求输出各级星星的数目。 ## 输入格式 第一行有一整数 $n$,表示星星的数目; 接下来 $n$ 行给出每颗星星的坐标,坐标用两个整数 $x, y$ 表示。 ## 输出格式 $n$ 行,每行一个整数,分别是 $1$ 级,$2$ 级,$3$ 级,$\dots$,$n$ 级的星星的数量。 ## 输入输出样例 #1 ### 输入 #1 ``` 5 1 1 5 1 7 1 3 3 5 5 ``` ### 输出 #1 ``` 1 2 1 1 0 ``` ## 说明/提示 **【数据范围】** 对于 $100\%$ 的数据,$1 \leqslant n \leqslant 1.5 \times 10^4, 0 \leqslant x, y \leqslant 3.2 \times 10^4$。 **【数据规范】** 不会有星星重叠。 星星按 $y$ 坐标增序给出,$y$ 坐标相同的按 $x$ 坐标增序给出。 **【题目来源】** [Ural 1028](http://acm.timus.ru/problem.aspx?space=1&num=1028) c++,不要vector,可用函数:int lowbit(int x) { return x & -x; } void updata(int x, int val) { while(x <= n) { c[x] += val; x += lowbit(x); } } int getsum(int x) { int res = 0; while(x > 0) { res += c[x]; x -= lowbit(x); } return res; } //区间修改 void updata(int x, int val) { int r = x; //因为常量部分是一直不变的,所以需要提前记录好有边界 while(x <= n) { d[x] += val; d_i[x] += val * r; x += lowbit(x); } } void add(int l, int r, int val) { updata(l, val); updata(r+1, -val); } //区间查询 int getsum(int x) { int res = 0, r = x; while(x > 0) { res += (r + 1)*d[x] - d_i[x]; x -= lowbit(x); } return res; } int query(int l, int r) { return getsum(r) - getsum(l-1); }
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值