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

 #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. C#ModBus Tcp

    C#ModBus Tcp 报文解析   上一篇博客已经完成 C#ModBus Tcp Master的实现 本篇主要对不同的功能码所发出的报文进行解析(包括请求报文及响应报文) 读操作 功能码 0x01 ...

  2. flask钩子函数

    @app.context_processor def context_processor(): return {"current_user":"zhiliao" ...

  3. history 命令

    history 命令用来显示执行过的命令,也可以根据显示的命令重新执行需要的命令. 用法: n 显示n个最近的记录 -a 添加记录到history文件中 -c 将目前shell中的所有history命 ...

  4. xtrabackup备份失败(error writing file 'UNOPENED')

    xtrabackup备份失败 解决了,是因为limit open files值设置太小了 (3)修改资源限制参数 vi /etc/security/limits.conf nproc:用户创建进程数限 ...

  5. 小记--------SparkContext初始化原理机制图解

  6. markdown中使用缩进

    在markdown中直接敲空格是不生效的. 使用html标签来实现 一个空格大小的表示:  两个空格的大小表示:  不换行空格:  别忘记分号 参考了大神的文章: markdown空格缩进以及HTML ...

  7. Python三大主流框架的对比

    相信做Python这一块的程序员都有听说这三个框架,就像神一样的存在,每一个框架的介绍我就不写出来了,感兴趣可以自己百度了解了解!下面我就说正事 Django:Python 界最全能的 web 开发框 ...

  8. sql之游标

    --select * from   master..sysprocessesuse testdeclare my_cursor cursor scroll dynamic --scroll表示可以向前 ...

  9. Codeforces 1178F2. Long Colorful Strip

    传送门 首先涂区间,那么区间最多有 $2n$ 个相邻位置不同的情况,并且连续相同的颜色可以合并起来 那么这样操作完以后,区间长度最多为 $2n$ 发现涂完一段区间以后其他的操作都不能出现一边在区间内而 ...

  10. oracle创建用户表空间

    --本次因工作需要,为其他部门提供部分表数据,创建一个新用户与表空间.--system用户下drop user sys_outside cascade;drop tablespace sys_outs ...