模板题,可用于求一个数的所有原根。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+,inf=0x3f3f3f3f;
int n,fac[N],nf;
vector<int> ans;
int Pow(int x,int p,int mod) {
int ret=;
for(; p; p>>=,x=(ll)x*x%mod)if(p&)ret=(ll)ret*x%mod;
return ret;
}
int phi(int x) {
int ret=x;
for(int i=; i<=x/i; ++i)if(x%i==) {
ret=ret/i*(i-);
for(; x%i==; x/=i);
}
if(x>)ret=ret/x*(x-);
return ret;
}
void getfac(int x) {
nf=;
for(int i=; i<=x/i; ++i)if(x%i==)
for(fac[nf++]=i; x%i==; x/=i);
if(x>)fac[nf++]=x;
}
bool check(int x) {
if(x==||x==)return ;
if(x%==)return ;
if(x%==)x/=;
for(int i=; i<=x/i; ++i)if(x%i==) {
for(; x%i==; x/=i);
return x==;
}
return ;
}
int minRoot(int x,int phix) {
if(x==||x==)return x-;
for(int i=; i<x; ++i)if(Pow(i,phix,x)==) {
bool flag=;
for(int j=; j<nf; ++j)if(Pow(i,phix/fac[j],x)==) {flag=; break;}
if(flag)return i;
}
}
void solve(int x) {
int phix=phi(x);
getfac(phix);
int r=minRoot(x,phix);
ans.clear(),ans.push_back(r);
for(int i=; i<phix; ++i)if(__gcd(i,phix)==)ans.push_back(Pow(r,i,x));
sort(ans.begin(),ans.end());
}
int main() {
while(scanf("%d",&n)==) {
if(!check(n))puts("-1");
else {
solve(n);
for(int i=; i<ans.size(); ++i)printf("%d%c",ans[i]," \n"[i==ans.size()-]);
}
}
return ;
}

HDU - 4992 Primitive Roots (原根)的更多相关文章

  1. hdu 4992 Primitive Roots 【求原根模板】

    题目链接 大题流程: 判定是否有原根->求出最小原根->利用最小原根找出全部原根 #include<bits/stdc++.h> using namespace std; ty ...

  2. POJ 1284 Primitive Roots 原根

    题目来源:POJ 1284 Primitive Roots 题意:求奇素数的原根数 思路:一个数n是奇素数才有原根 原根数是n-1的欧拉函数 #include <cstdio> const ...

  3. POJ1284 Primitive Roots (原根)

    题目链接:http://poj.org/problem?id=1284 题目描述: 题目大意: 一个质数原根的个数 题解: 结论题 一个数n的原根的个数等于$\varphi(\varphi(n))$ ...

  4. 【HDU 4992】 Primitive Roots (原根)

    Primitive Roots   Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...

  5. POJ 1284:Primitive Roots(素数原根的个数)

    Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5709 Accepted: 3261 Descr ...

  6. POJ 1284 Primitive Roots 数论原根。

    Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2479   Accepted: 1385 D ...

  7. poj 1284 Primitive Roots (原根)

    Primitive Roots http://poj.org/problem?id=1284 Time Limit: 1000MS   Memory Limit: 10000K       Descr ...

  8. POJ1284 Primitive Roots [欧拉函数,原根]

    题目传送门 Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5434   Accepted:  ...

  9. POJ_1284 Primitive Roots 【原根性质+欧拉函数运用】

    一.题目 We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if t ...

随机推荐

  1. android简易跑马灯

    重点:焦点的选择(返回true使得焦点不被选择) MarqueeText.java package com.example.demo02; import android.content.Context ...

  2. 2019icpc西安邀请赛 J And And And (树形dp)

    题目链接:https://nanti.jisuanke.com/t/39277 题意:给出一棵有边权的树,求所有简单路径包含异或和为0的简单路径的总数和. 思路: 首先,对于异或为0这一限制,我们通过 ...

  3. Linux多线程编程 - sleep 和 pthread_cond_timedwait

    #include <stdio.h> #include <stdlib.h> int flag = 1; void * thr_fn(void * arg) {   while ...

  4. Design Log Storage System

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  5. Analyzing Polyline -- Codeforces Round #123 (Div. 2)

    题意:https://codeforc.es/problemset/problem/195/D 求折线段数. 思路: 对pos进行sort,对不同区间段加k,两个dp处理不同k>0 or k&l ...

  6. AI面试必备/深度学习100问1-50题答案解析

    AI面试必备/深度学习100问1-50题答案解析 2018年09月04日 15:42:07 刀客123 阅读数 2020更多 分类专栏: 机器学习   转载:https://blog.csdn.net ...

  7. luogu题解P4198楼房重建--线段树神操作

    题目链接 https://www.luogu.org/problemnew/show/P4198 分析 一句话题意,一条数轴上有若干楼房,坐标为\(xi\)的楼房有高度\(hi\),那么它的斜率为\( ...

  8. O052、Create Volume 操作 (Part III)

    参考https://www.cnblogs.com/CloudMan6/p/5617980.html       Jun 20 17:15:56 DevStack-Rocky-Compute-22 c ...

  9. JavaScript笔记(1)

    1.JavaScript的基本概念 JavaScript是一个解释型的脚本语言 JavaScript可以写在HTML文档内部的任何地方 行内式 内嵌式 链入式:<script src=" ...

  10. C#中构建多线程应用程序[转]

    原文:http://blog.sina.com.cn/s/blog_4e61c4290100ndyl.html C#中构建多线程应用程序[转] (2011-01-04 21:29:29) 转载▼ 标签 ...