hdu 4992 Primitive Roots 【求原根模板】
大题流程: 判定是否有原根->求出最小原根->利用最小原根找出全部原根
#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 【求原根模板】的更多相关文章
- POJ 1284:Primitive Roots 求原根的数量
Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3381 Accepted: 1980 D ...
- HDU - 4992 Primitive Roots (原根)
模板题,可用于求一个数的所有原根. #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f ...
- 【HDU 4992】 Primitive Roots (原根)
Primitive Roots Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...
- poj 1284 Primitive Roots (原根)
Primitive Roots http://poj.org/problem?id=1284 Time Limit: 1000MS Memory Limit: 10000K Descr ...
- POJ 1284 Primitive Roots 数论原根。
Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2479 Accepted: 1385 D ...
- 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 ...
- poj 1284 Primitive Roots(原根+欧拉函数)
http://poj.org/problem?id=1284 fr=aladdin">原根 题意:对于奇素数p,假设存在一个x(1<x<p),(x^i)%p两两不同(0&l ...
- [POJ1284]Primitive Roots(原根性质的应用)
题目:http://poj.org/problem?id=1284 题意:就是求一个奇素数有多少个原根 分析: 使得方程a^x=1(mod m)成立的最小正整数x是φ(m),则称a是m的一个原根 然后 ...
- POJ 1284 Primitive Roots (求原根个数)
Primitive Roots 题目链接:id=1284">http://poj.org/problem?id=1284 利用定理:素数 P 的原根的个数为euler(p - 1) t ...
随机推荐
- 007-Spring Boot-@Enable*注解的工作原理-EnableConfigurationProperties、ImportSelector、ImportBeanDefinitionRegistrar
一.@Enable* 启用某个特性的注解 1.EnableConfigurationProperties 回顾属性装配 application.properties中添加 tomcat.host=19 ...
- VMware 虚拟化编程(11) — VMware 虚拟机的全量备份与增量备份方案
目录 目录 前文列表 全量备份数据的获取方式 增量备份数据的获取过程 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化编 ...
- Oracle删除表时候有外键 不能删除
SELECT A .constraint_name, A .table_name, b.constraint_nameFROM user_constraints A, u ...
- NOPI导入导出EXCEL
一.简介 1. 什么是NPOI NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office 97 ...
- oracle--多表联合查询sql92版
sql92学习 -查询员工姓名,工作,薪资,部门名称 sql的联合查询(多表查询) --1.sql92标准 ----笛卡尔积:一件事情的完成需要很多步骤,而不同的步骤有很多种方式,完成这件事情的所有方 ...
- dp(买票优惠)
CodeForces - 1154F There are n shovels in the nearby shop. The i-th shovel costs ai bourles. Misha h ...
- dfs(枚举)
http://codeforces.com/gym/100989/problem/L L. Plus or Minus (A) time limit per test 1.0 s memory lim ...
- pip源地址
pip国内的一些镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...
- ex2、逻辑回归
介绍: 在本练习中,您将实现逻辑回归,并将其应用于两个不同的数据集.在开始编程练习之前,我们强烈要求建议观看视频讲座并完成相关主题的问题.要开始练习,您需要下载起始代码并将其内容解压缩到要完成练习的目 ...
- 14、前端知识点--Vue生命周期浅析
vue生命周期 每个Vue实例或组件从创建到显示再到废弃的过程就是vue的生命周期.很多时候我们希望能在这个过程中执行一些操作,于是就有了生命周期钩子. 生命周期钩子函数允许我们在实例不同阶段执行各种 ...