Primitive Roots

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 583    Accepted Submission(s): 144

Problem 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 xy = 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,xk≡1(mod n)的最小正整数k是φ(n),那么就称x是n的原根。
题目里面也讲了。φ(n)就是欧拉函数。
原根有很多美丽的性质。比如说:
  1. 有原根的数只有2,4,p^n,2p^n(p为质数,n为正整数)。
  2. 一个数的最小原根的大小是O(n0.25)的。
  3. 如果g为n的原根,则gd为n的原根的充要条件是(d,φ(n))=1;
  4. 如果n有原根,它的原根个数为φ(φ(n))。

那么来看一下这道题:

首先根据性质1,我们可以通过预处理质数,把不存在的情况判掉。

然后根据性质3,找到一个原根后枚举次方判gcd就可以了。

怎么找到一个原根呢?按照性质2傻傻去跑在这种多组数据的题目里是肯定不行的。

那么有一个喜大普奔的结论来帮助我们:

因为gφ(n)≡1(mod n),而对于比φ(n)小的数都不成立。

枚举φ(n)的质因子p,看gφ(n)/p在模意义下是否是1。

是1的话g就不是原根。

证明起来有点麻烦,这里就不写了。

所以找原根大概是O(n0.25/2)的。

找到之后枚举次方就可以了,因为是充分条件。

想剪个好枝却剪烂的某人在HDU上留下了5个WA... ...

#include    <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define LL long long int
#define ls (x << 1)
#define rs (x << 1 | 1)
#define MID int mid=(l+r)>>1
using namespace std; const int N = 1000000+10;
int P[N],vis[N],phi[N],tot,n; inline int gcd(int a,int b){return b?gcd(b,a%b):a;} inline void prepare()
{
phi[1]=1;
for(int i=2;i<N;++i){
if(!vis[i])P[++tot]=i,phi[i]=i-1;
for(int j=1;j<=tot;++j){
if(i*P[j]>=N)break;
vis[i*P[j]]=1;
if(i%P[j])phi[i*P[j]]=phi[i]*phi[P[j]];
else{phi[i*P[j]]=phi[i]*P[j];break;}
}
}
} inline int QPow(int d,int z,int Mod)
{
int ans=1;
for(;z;z>>=1,d=1ll*d*d%Mod)if(z&1)ans=1ll*ans*d%Mod;
return ans;
} inline bool check(int x)
{
if(x==2 || x==4)return 1;
if((x&1)^1)x>>=1;
for(int i=2;P[i]<=x;++i)
if(x%P[i]==0){
while(x%P[i]==0)x/=P[i];
return x==1?P[i]:0;
}
return 0;
} inline int get_rg(int fx)
{
int pt[1010],tt=0,Txd=phi[fx];
for(int i=1;P[i]*P[i]<=Txd;++i)
if(Txd%P[i]==0){
pt[++tt]=P[i];
while(Txd%P[i]==0)Txd/=P[i];
}
if(Txd!=1)pt[++tt]=Txd;
for(int i=2;i<=fx;++i)
if(QPow(i,phi[fx],fx)==1){
int flag=1;
for(int j=1;j<=tt;++j)
if(QPow(i,phi[fx]/pt[j],fx)==1){
flag=0;break;
}
if(flag)return i;
}
return 0;
} inline void work(int fx)
{
int tt=0,pr[N];
if(fx==2){printf("1\n");return;}
if(fx==4){printf("3\n");return;}
int T=check(fx);
if(!T){printf("-1\n");return;}
int g=get_rg(fx);
for(int i=1,k=g;i<phi[fx];++i,k=1ll*k*g%fx)
if(gcd(i,phi[fx])==1)
pr[++tt]=k;
sort(pr+1,pr+tt+1);
for(int i=1;i<tt;++i)
printf("%d ",pr[i]);
printf("%d",pr[tt]);
printf("\n");
} int main()
{
prepare();
while(scanf("%d",&n)!=EOF)work(n);
return 0;
}

  

HDU4992 求所有原根的更多相关文章

  1. 51nod1135(求最小原根)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1135 题意:中文题诶- 思路:设m是正整数,a是整数,若a模 ...

  2. 2018秦皇岛ccpc-camp Steins;Gate (原根+FFT)

    因为给定的模数P保证是素数,所以P一定有原根. 根据原根的性质,若\(g\)是\(P\)的原根,则\(g^k\)能够生成\([1,P-1]\)中所有的数,这样的k一共有P-2个. 则\(a_i*a_j ...

  3. 【bzoj2219-数论之神】求解x^a==b(%n)-crt推论-原根-指标-BSGS

    http://www.lydsy.com/JudgeOnline/problem.php?id=2219 弄了一个晚上加一个午休再加下午一个钟..终于ac..TAT 数论渣渣求轻虐!! 题意:求解 x ...

  4. fft练习

    数学相关一直都好弱啊>_< 窝这个月要补一补数学啦, 先从基础的fft补起吧! 现在做了 道. 窝的fft 模板 (bzoj 2179) #include <iostream> ...

  5. 【学习整理】NOIP涉及的数论 [updating]

    扩展欧几里得 求二元一次不定式方程 的一组解. int exgcd(int a,int b,int &x,int &y) { int t; ;y=;return a;} t=exgcd ...

  6. 【解高次同余方程】51nod1038 X^A Mod P

    1038 X^A Mod P 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 X^A mod P = B,其中P为质数.给出P和A B,求< P的所有X. 例如:P = 11 ...

  7. XII Open Cup named after E.V. Pankratiev. GP of Eastern Europe (AMPPZ-2012)

    A. Automat $m$超过$1600$是没用的. 从后往前考虑,设$f[i][j][k]$表示考虑$[i,n]$这些物品,一共花费$j$元钱,买了$k$个物品的最大收益. 时间复杂度$O(n^5 ...

  8. HAOI(十二省联考)2019 qwq记

    \(\large{Day\ -1}:\) 放假了,白天大概是抱着最后一次在机房的心态复习着板子过去的.看着机房里的各位神仙丝毫不慌的颓倒是有点慌了,敲了一下多项式的板子感觉写的相当自闭,感觉AFO应该 ...

  9. [日常] HEOI 2019 退役记

    HEOI 2019 退役记 先开坑 坐等AFO 啥时候想起来就更一点(咕咕咕) Day 0 早上打了个LCT, 打完一遍过编译一遍AC...(看来不考这玩意了) 然后进行了一些精神文明建设活动奶了一口 ...

随机推荐

  1. laravel 表单验证 Exists 规则的基本使用方法

    public function rules(){ return [ 'm_pushing_frequency_level_id' => 'integer|required|exists:m_pu ...

  2. zzuli 1816: 矩形 排序思维

    1816: 矩形 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 179  Solved: 54 SubmitStatusWeb Board Descr ...

  3. MYSQL常见运算符和函数

    字符函数 (1)CONCAT():字符连接 SELECT CONCAT('IMOOC','-','MySQL');//IMOOC-MySQL SELECT CONCAT (first_name,las ...

  4. 移动GIS在企业各个行业中的应用解决方案

    “移动GIS的设备厂商越来越多地关注行业用户的需求,所以移动GIS的市场前景是非常广阔的.当前国内移动GIS,已广泛应用于测绘.国土.环境.水利.农业.林业和矿产等传统资源管理领域和城市规划方面.在应 ...

  5. 深入理解java虚拟机_第三章(上)----->垃圾收集器与内存分配策略

    1.  前言 这一版块内容比较多,分为两篇文章来做笔记.本文讲述上半部分垃圾收集部分;下一篇文章写内存分配部分. 概述 对象已死吗? 引用技术算法 可达性分析算法 再谈引用 两次标记 回收方法区 2. ...

  6. 大白话Vue源码系列(01):万事开头难

    阅读目录 Vue 的源码目录结构 预备知识 先捡软的捏 Angular 是 Google 亲儿子,React 是 Facebook 小正太,那咱为啥偏偏选择了 Vue 下手,一句话,Vue 是咱见过的 ...

  7. node.js之express框架入门篇

    一.express框架简介 express框架是后台的Node框架,在后台的受欢迎的程度,和jQuery一样 英语官网:http://expressjs.com/ 中文官网:http://www.ex ...

  8. mybatis延迟加载一对多

    1.实体类 package cn.bdqn.bean; import java.util.Set; /** *国家的实体类 */ public class Country { private Inte ...

  9. bzoj4974 字符串大师

    4974: 字符串大师 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 310  Solved: 155[Submit][Status][Discuss] ...

  10. WebLogic部署报java.lang.ClassCastException: weblogic.xml.jaxp.RegistrySAXParserFactory cannot be cast to javax.xml.parsers.SAXParserFactory

    今天在部署WebLogic项目时,报了java.lang.ClassCastException: weblogic.xml.jaxp.RegistrySAXParserFactory cannot b ...