题目链接

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

#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. React-Native 之 GD (二十)removeClippedSubviews / modal放置的顺序 / Android 加载git图\动图 / 去除 Android 中输入框的下划线 / navigationBar

    1.removeClippedSubviews 用于提升大列表的滚动性能.需要给行容器添加样式overflow:’hidden’.(Android已默认添加此样式)此属性默认开启 这个属性是因为在早期 ...

  2. 关闭掉mysql 8和mysql5.7的密码验证插件validate_password

    在mysql文档中的一段话If you installed MySQL 5.7 using the MySQL Yum repository, MySQL SLES Repository, or RP ...

  3. MyBatis逆向工程去除表名前缀

    https://www.jianshu.com/p/e09d2370b796 https://blog.csdn.net/baidu_16757561/article/details/75071476 ...

  4. 前端004/React常用UI组件

    每天进步一点点〜 Ant Design of React //蚂蚁金服设计平台.需要应用何种类型组件可参考API React + mobx + nornj 开发模式文件说明: [1].A.t.html ...

  5. python 入门学习之anaconda篇

    还没下载的同学先点击这里进入anaconda官方网站进行下载. 然后点击安装,注意的是这里 安装好了之后呢,我们就开始进行Conda的环境管理,Conda的环境管理功能允许我们同时安装 若干不同版本的 ...

  6. org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NumberFormatException: For input string: "W%" ### Cause: java.lang.NumberFormatException: For input s

    一个常见的myBatis xml文件中的引号错误: org.apache.ibatis.exceptions.PersistenceException: ### Error querying data ...

  7. py基础

    基本语句和函数等练习,知识点都在代码里... """ a = int(input('a = ')) b = int(input('b = ')) print('%d + ...

  8. Vue源码详细解析:transclude,compile,link,依赖,批处理...一网打尽,全解析!

    用了Vue很久了,最近决定系统性的看看Vue的源码,相信看源码的同学不在少数,但是看的时候却发现挺有难度,Vue虽然足够精简,但是怎么说现在也有10k行的代码量了,深入进去逐行查看的时候感觉内容庞杂并 ...

  9. CentOS 5.5编译安装lnmp

    如果是安装Centos6.5记得Perl是必选的,否则无法安装VMWare Tools!!!!切记 如果出现make错误需要安装其他软件,装好后  make clean   make install ...

  10. docker网络 macvlan

    docker 还开发了另一个支持跨主机容器网络的 driver:macvlan. macvlan 本身是 linxu kernel 模块,其功能是允许在同一个物理网卡上配置多个 MAC 地址,即多个 ...