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. php-fpm 的安装与LNMP测试

    1. 下载并安装PHP 1.1 编译安装 1.1.1 安装前的准备 下载并安装libmcrypt 和 libmcrypt-devel[root@test /opt/soft]# wget ftp:// ...

  2. C# DataTable转List And List转DataTable

    // DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt); ...

  3. Force removing ActivityRecord,app died, no saved state报错的理解

    为什么说理解呢?出现这个问题,我的情景是,在activity里面直接起了一个Thread,线程里面进行了一个繁重的任务,当线程执行完后,这个activity也销毁了,界面显示的任务栈下面的一个活动.百 ...

  4. 错误编码 = 10022 错误消息 = SDK 组件 Qupaisdk 启动出错,错误消息为 [Qupaisdk], the android stack error message is Fail to start the plugin, which is caused by No implem

     so没有load到.几个可能,1.缺少so--在群共享下载拷贝到armeabi-v7a 2.so没有打入apk--检查打出来的apk.解压打开看下libs下面有没有so. 3.abi平台问题.检查平 ...

  5. socket的NIO操作

    一.前言 Java中直接使用socket进行通信的场景应该不是很多,在公司的一个项目中有这种需求,所以根据自己的理解和相关资料的参考,基于NIO 实现了一组工具类库,具体的协议还未定义,后续再整理 二 ...

  6. MVC小系列(十六)【在控制器级别或具体Action级别上动态设定模板页(Layout)】

    方法就是使用:ActionFilterAttribute它的几个方法:OnActionExecuted,OnActionExecuting,OnResultExecuted和OnResultExecu ...

  7. APP评价(星星点赞)很简单

    1.用代码或者storyboard创建5个button(现在一般都是5个星星) 我用的是storyboard 记得一定要设置button的tag值 在.h中 @property (weak, nona ...

  8. 升级Capitan 10.11以后CocoaPod 无效解决办法

    今天发现升级10.11的系统以后执行 pod install 的时候报错 zsh: command not found: pod 解决方法如下: 1.检查gem 的数据源 gem sources -l ...

  9. IOS开发中针对UIImageView的几种常用手势

    // //  ViewController.m //  05-手势 // //  Created by wanghy on 15/9/21. //  Copyright (c) 2015年 wangh ...

  10. DailyNote

    删除node-modules文件夹 npm install -g rimraf rimraf node_modules 绘制一条贝塞尔曲线: context.quadraticCurveTo(x1,y ...