跳到正文

个人记录

题解:[GESP202503 五级] 原根判断

XingLuo_King约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月27日
收录
2025年12月12日
更新
2026年9月12日
篇幅
约 2 分钟

版本

  1. v1当前

上次考级栽在这题上了,来luogu发个题解: ),希望大家不要被这题难住,说实话这题真的有点超纲了,但在官方狡辩下,这题还是出出来了,而且还被我碰上了,运气绝对大凶!!!

C++
#include <bits/stdc++.h>
using namespace std;
int a,p;
int ans;
int fpw(int b,int e)
{
    if (e==0) return 1;
    int r=fpw(b,e>>1);
    r=1ll*r*r%p;
    if (e&1) r=1ll*r*b%p;
    return r;
}
void check(int e)
{
    if (fpw(a,e)==1) ans=0;
}
int main()
{
    int t;
    cin >> t;
    while (t--) 
    {
            cin >> a >> p;
            ans=1;
            int phi=p-1,r=phi;
            for (int i=2;i*i<=phi;i++)
            {
                if (phi%i==0) 
                {
                    check(phi/i);
                    while (r%i==0) r/=i;
                }
            }
            if (r>1) check(phi/r);
            if (ans) cout << "Yes" << endl;
            else cout << "No" << endl;
    }
    return 0;
}
原文