Description

We say that integer x, 0 < x < n, is a primitive root modulo n if and only if the minimum positive integer y which makes x y = 1 (mod n) true is φ(n) .Here φ(n) is an arithmetic function that counts the totatives of n, that is, the positive integers less than or equal to n that are relatively prime to n. Write a program which given any positive integer n( 2 <= n < 1000000) outputs all primitive roots of n in ascending order.

Input

Multi test cases. 
Each line of the input contains a positive integer n. Input is terminated by the end-of-file seperator.

Output

For each n, outputs all primitive roots of n in ascending order in a single line, if there is no primitive root for n just print -1 in a single line.

Sample Input

4
25

Sample Output

3
2 3 8 12 13 17 22 23
 
【题意】
  求n的所有原根,若没有就输出-1(n<=10^6)
 
【分析】
  

求出n的所有原根,不存在原根输出-1。

原根的定义题目已经给出,对于n的原根x,则满足x的y次幂模n等于1的最小y是n的欧拉函数值phi(n),也就是小于等于n且与n互质的个数。

官方的题解里面说暴力求出一个原根来,然后利用结论:

如果g是模m的原根,整数d>=0,则g的d次幂是模m的原根的一个充要条件是d和phi(m)互质。

d暴力枚举:

一个是如果gcd(g, m)=1,且g^d=1(mod m),则d为phi(m)的一个因子。换句话说如果g是m的原根,那么对于phi(m)的所有因子d(不包含phi(m)本身),g^d=1(mod m)是不成立的。我们可以通过枚举phi(m)的质因子以及g^phi(m)=1(mod m)是否成立来判断g是否是模m的原根。

然后有些不存在原根的数字利用另一条性质排除:

模m有原根的充要条件是m=2,4,p^n, 2p^n,p为奇质数,n任意正整数。

如果m没有满足上述条件,就直接输出-1。

http://blog.csdn.net/hongrock/article/details/39179291


代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long
#define Maxn 1000010 int phi[Maxn],pri[Maxn],pl;
bool q[Maxn],qs[Maxn]; int gcd(int a,int b)
{
if(b==) return a;
return gcd(b,a%b);
} void get_phi(int mx)
{
pl=;
memset(q,,sizeof(q));
memset(qs,,sizeof(qs));
phi[]=;
for(int i=;i<=mx;i++)
{
if(q[i]) pri[++pl]=i,qs[i]=i==?:,phi[i]=i-;
for(int j=;j<=pl;j++)
{
if(i*pri[j]>mx) break;
q[i*pri[j]]=;
if(i%pri[j]==)
{
phi[i*pri[j]]=phi[i]*pri[j];
if(qs[i]) qs[i*pri[j]]=;
}
else phi[i*pri[j]]=phi[i]*(pri[j]-);
if(i%pri[j]==) break;
}
}
for(int i=mx/;i>=;i--) if(qs[i]) qs[*i]=;
qs[]=;qs[]=;
} int qpow(int x,int y,int p)
{
LL ans=,xx=x,pp=p;
while(y)
{
if(y&) ans=(ans*xx)%pp;
xx=(xx*xx)%pp;
y>>=;
}
return (int)ans;
} int np[Maxn],nl;
void div(int x)
{
nl=;
for(int i=;pri[i]*pri[i]<=x;i++) if(x%pri[i]==)
{
np[++nl]=pri[i];
while(x%pri[i]==) x/=pri[i];
}
if(x>) np[++nl]=x;
} int ffind(int n)
{
div(phi[n]);
for(int i=;i<=n;i++)
{
if(qpow(i,phi[n],n)!=) continue;
bool ok=;
for(int j=;j<=nl;j++)
{
if(qpow(i,phi[n]/np[j],n)==) {ok=;break;}
}
if(!ok) continue;
return i;
}
return -;
} int op[Maxn]; void output(int g,int n)
{
op[]=;
op[++op[]]=g;
LL M=(LL)n,now=(LL)g,gg=(LL)g;
for(int i=;i<phi[n];i++)
{
now=(now*gg)%M;
if(gcd(i,phi[n])==) op[++op[]]=(int)now;
}
} int main()
{
get_phi();
int n;
while(scanf("%d",&n)!=EOF)
{
if(!qs[n]) {printf("-1\n");continue;}
int g=ffind(n);
output(g,n);
sort(op+,op+op[]+);
for(int i=;i<=op[];i++) printf("%d ",op[i]);
printf("\n");
}
return ;
}

[HDU 4992]

2016-09-06 16:39:31

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

  1. HDU - 4992 Primitive Roots (原根)

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

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

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

  3. POJ 1284 Primitive Roots 原根

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

  4. POJ1284 Primitive Roots (原根)

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

  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. maven搭建个人仓库

    Maven环境搭建: 本地仓库+maven运行环境+构建项目 1.搭建nexus 本地仓库 1)拷贝jdk1.6和tomcat62)配置端口为8010 (端口自行定义,只要下面各处一致即可)3)复制n ...

  2. Ubuntu 14.04 LAMP搭建小记

    文章目录 LAMP WinQQ Ubuntu 的使用的建模工具 JDK Chormium flash Eclipse 无法找到Jre LAMP 参考资料: 1. 安装php环境   http://ww ...

  3. 转载--SQL Server 2005的XQuery介绍

    原文地址: http://bbs.51cto.com/thread-458009-1-1.html   引用: 摘要 本文介绍了SQL Server 2005能够支持的XQuery的各方面特性如FLW ...

  4. SQL Server调优系列基础篇 - 性能调优介绍

    前言 关于SQL Server调优系列是一个庞大的内容体系,非一言两语能够分析清楚,本篇先就在SQL 调优中所最常用的查询计划进行解析,力图做好基础的掌握,夯实基本功!而后再谈谈整体的语句调优. 通过 ...

  5. SqlServer 由于未在SqlServer的此实例上安装复制组件解决方法

    sqlserver2005在复制订阅时出现: “由于未在SqlServer的此实例上安装复制组件,Microsoft SQL server 无法访问这些组件,请参阅SQL Server……” 解决方法 ...

  6. Java 十进制转十六进制

    1. /** * All possible chars for representing a number as a String */ final static char[] digits = { ...

  7. Ext.Net学习笔记12:Ext.Net GridPanel Filter用法

    Ext.Net学习笔记12:Ext.Net GridPanel Filter用法 Ext.Net GridPanel的用法在上一篇中已经介绍过,这篇笔记讲介绍Filter的用法. Filter是用来过 ...

  8. Win7上安装oracle中可能遇到的错误

    安装oracle,总是出现一个警告两个错误错误,其描述是:OUI-18001:不支持操作系统’Windows Vista版本6.1’,找了好久,终于找到原因,因为oracle不支持Win7操作系统. ...

  9. 【失败】制作CentOS镜像

    1.在vmware上安装centos虚拟机,安装过程中设置CPU时,点选 虚拟化Intel VT-x~ 和 虚拟化CPU性能计数器,网络使用NAT模式 2.#mkdir /opt/iso,上传镜像(用 ...

  10. centOS 6.4 vsftpd 配置

    ###########配置流程########### 1 新建一个ftp用户,为了跟vsftp的虚拟用户对应 #useradd  -d /home/vftpuser   -s /sbin/nologi ...