题目链接

大题流程: 判定是否有原根->求出最小原根->利用最小原根找出全部原根

#include<bits/stdc++.h>
using namespace std;
typedef long long LL; const int maxn=1e6+;
int prime[maxn+];
bool check[maxn+];
int phi[maxn+];
int num_prime;
void init()
{
memset(check, false, sizeof(check));
phi[]=;
for(int i=; i<=maxn; i++)
{
if(!check[i])
{
prime[num_prime++]=i;
phi[i]=i-;
}
for(int j=; j<num_prime; j++)
{
if(i*prime[j]>maxn) break;
check[i*prime[j]]=true;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else
{
phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
}
} LL gcd(LL a, LL b)
{
return b? gcd(b,a%b):a;
} void get(LL n,vector<LL>& fac) //对n进行因式分解
{
fac.clear();
for(LL i=; i*i<=n; i++)
if(n%i==)
{
fac.push_back(i);
while(n%i==) n/=i;
}
if(n>) fac.push_back(n);
} LL qpow(LL x,LL n,LL mod) //求x^n%mod
{
LL ret=;
for(; n; n>>=)
{
if(n&) ret=ret*x%mod;
x=x*x%mod;
}
return ret;
} vector<LL> fac;
vector<LL> ans; bool ok(LL x)
{
if(x%==) x/=;
if(x%==) return false;
for(int i=; prime[i]*prime[i]<=x; i++) if(x%prime[i]==)
{
while(x%prime[i]==) x/=prime[i];
return x==;
}
return true;
} LL get_g(LL p) //得到一个正整数p的最小原根
{
for(int i=; i<p; i++)
{
bool flag=false;
for(LL x:fac)
if(qpow(i,phi[p]/x,p)==)
{
flag=true;
break;
}
if(!flag&&qpow(i,phi[p],p)==) return i;
}
} void GetAns(LL g,LL p,vector<LL>& ans) //由最小原根p,得到某正整数p的全部原根
{
ans.clear();
ans.push_back(g);
for(int i=; i<phi[p]; i++)
if(gcd(i,phi[p])==) ans.push_back(qpow(g,i,p));
} int main()
{
init();
LL p;
while(~scanf("%lld",&p))
{
if(p==||p==)
{
printf("%lld\n",p-);
continue;
}
if(!ok(p)) //首先判断是否有原根
{
puts("-1");
continue;
}
get(phi[p],fac);
LL g=get_g(p); //定义g为最小原根
GetAns(g,p,ans);
sort(ans.begin(),ans.end());
for(int i=; i<ans.size(); i++)
printf("%lld%c",ans[i],i==ans.size()-? '\n':' ');
}
}

hdu 4992 Primitive Roots 【求原根模板】的更多相关文章

  1. POJ 1284:Primitive Roots 求原根的数量

    Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3381   Accepted: 1980 D ...

  2. HDU - 4992 Primitive Roots (原根)

    模板题,可用于求一个数的所有原根. #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f ...

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

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

  4. poj 1284 Primitive Roots (原根)

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

  5. POJ 1284 Primitive Roots 数论原根。

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

  6. 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 ...

  7. poj 1284 Primitive Roots(原根+欧拉函数)

    http://poj.org/problem?id=1284 fr=aladdin">原根 题意:对于奇素数p,假设存在一个x(1<x<p),(x^i)%p两两不同(0&l ...

  8. [POJ1284]Primitive Roots(原根性质的应用)

    题目:http://poj.org/problem?id=1284 题意:就是求一个奇素数有多少个原根 分析: 使得方程a^x=1(mod m)成立的最小正整数x是φ(m),则称a是m的一个原根 然后 ...

  9. POJ 1284 Primitive Roots (求原根个数)

    Primitive Roots 题目链接:id=1284">http://poj.org/problem?id=1284 利用定理:素数 P 的原根的个数为euler(p - 1) t ...

随机推荐

  1. Python编程:从入门到实践—if 语句

    Python编程:从入门到实践—if 语句

  2. NOIP2012 洛谷P1083 借教室

    传送门 题意:有一些学(xian)生(quan)要借教室.在n天内,第i天学校有ri个教室.有m份订单,每份订单有三个数值dj,sj,tj,分别表示这个订单从第sj天开始到第tj天结束(包括端点),每 ...

  3. 设计模式-Runoob:工厂模式

    ylbtech-设计模式-Runoob:工厂模式 1.返回顶部 1. 工厂模式 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供 ...

  4. C# 内存建表备忘

    #region=====建表===== DataSet dataSet; // 创建表 DataTable table = new DataTable("testTable"); ...

  5. 红帽虚拟化RHEV-PXE批量安装RHEV-H

    目录 目录 前言 软件环境 前提 部署 PXE 环境 使用 yum 安装所需软件 配置 DHCP 配置 TFTP-Server 配置 vsftpd 服务用于提供安装系统所需软件包 安装 kicksta ...

  6. ClientDataSet初步使用

    https://blog.csdn.net/onebigday/article/details/5602619 ClientDataSet初步使用 2010年05月18日 08:36:00 阅读数:5 ...

  7. 孩子王app自动签到送现金券

    今天在某商场的孩子王店给小孩买东西时发现,app连续签到3天即可获得一张5元现金券. 回到家中,花几分钟Fiddler抓包分析写了个自动签到的代码. var cookies = new string[ ...

  8. pandas入门(1)

    import pandas as pd import numpy as np # 自动创建索引 obj = pd.Series([4, 7, -5, 2]) print(obj, type(obj)) ...

  9. STL 仿函数(函数对象)

    ##定义 仿函数(functor):一种具有函数性质的对象. 仿函数在C++中的新名称为函数对象(function object). 仿函数类对象像函数一样被调用,调用仿函数类对象时,实际调用的是仿函 ...

  10. jdk下载安装后为什么要设置环境变量?

    因为电脑不知道javac这个命令是在C:\Program Files\JAVA\jdk1.8.0_65\bin的这个路径下面,所以我们要设置好环境变量,来让电脑知道其路径