poj 1284 Primitive Roots(原根+欧拉函数)
http://poj.org/problem?id=1284
题意:对于奇素数p,假设存在一个x(1<x<p),(x^i)%p两两不同(0<i<p),且解集等于{1,2....,p-1}。
称x是p的一个原根。
输入p问p的原根有多少个。
直接枚举的,TLE了。
看到discuss里面说是求原根。答案直接是phi[p-1]。百度百科上直接就给出答案了。m有原根的充要条件是m=
1,2,4,p,2p,p^n,当中p是奇素数,n是随意正整数。它所含原根的个数是phi[phi[m]],由于phi[m]=m-1,所以答案是phi[m-1]。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std; const int maxn = 65540;
int flag[maxn];
int prime[maxn];
int phi[maxn]; void init()
{
memset(flag,0,sizeof(flag));
phi[1] = 1;
prime[0] = 0;
for(int i = 2; i < maxn; i++)
{
if(flag[i] == 0)
{
phi[i] = i-1;
prime[++prime[0]] = i;
}
for(int j = 1; j <= prime[0]&&prime[j]*i<maxn; j++)
{
flag[prime[j]*i] = 1;
if(i%prime[j] == 0)
phi[i*prime[j]] = phi[i] * prime[j];
else
phi[i*prime[j]] = phi[i] * (prime[j]-1);
}
}
}
int p;
int main()
{
init();
while(scanf("%d",&p) != EOF)
{
printf("%d\n",phi[p-1]);
}
return 0;
}
poj 1284 Primitive Roots(原根+欧拉函数)的更多相关文章
- POJ 1284 Primitive Roots 原根
题目来源:POJ 1284 Primitive Roots 题意:求奇素数的原根数 思路:一个数n是奇素数才有原根 原根数是n-1的欧拉函数 #include <cstdio> const ...
- (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
/* * POJ_2407.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj 1284 Primitive Roots
从来没有接触过完全剩余系,不会证明,知道看了别人的题解才知道要用欧拉函数: 下面是证明过程: p是奇素数,如果{xi%p | 1 <= i <= p - 1} = {1,2,...,p-1 ...
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 2 ...
- poj 3090 && poj 2478(法雷级数,欧拉函数)
http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...
- POJ 2407:Relatives(欧拉函数模板)
Relatives AC代码 Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16186 Accept ...
- POJ 2478 线性递推欧拉函数
题意: 求sigma phi(n) 思路: 线性递推欧拉函数 (维护前缀和) //By SiriusRen #include <cstdio> using namespace std; # ...
- 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数
http://poj.org/problem?id=1284 题意:给定一个奇素数p,求p的原根个数. 原根: { (xi mod p) | 1 <= i <= p-1 } is equa ...
随机推荐
- AC日记——[SCOI2009]游戏 bzoj 1025
[SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...
- 感受C#6.0新语法
作为一门专为程(yu)序(fa)员(tang)考虑的语言,感受一下来自微软的满满的恶意... 1. 字符串内联在之前的版本中,常用的格式化字符串: var s = String.Format(&quo ...
- prerender.io 搜索引擎优化 部署成windows服务 实现开机自动开启服务
一 prerender.io服务端部署 参考官方网站的部署步骤: $ git clone https://github.com/prerender/prerender.git $ cd preren ...
- 使用for循环打印9×9乘法表
请使用for循环,倒序打印9×9乘法表. 打印结果如下图所示: 使用for循环打印9×9乘法表 #include <stdio.h> int main() { int i, j, resu ...
- Java之IO流的关闭
1.在finally中关闭流: OutputStream out = null; try { out = new FileOutputStream(""); // ...操作流代码 ...
- ZUFEOJ 2147 07染色带谜题
2147: 07染色带谜题 时间限制: 1 Sec 内存限制: 128 MB提交: 170 解决: 21[提交][状态][讨论版][Edit] [TestData] 题目描述 现在给你一个长为N的 ...
- oracle10g中判断字段是否为空的坑
RT,在oracle中,写SQL时,假设这个字段为STA Char(3),判断这个字段是否为空一般都是这两个:STA = '' or STA is null 但是今天这两种方法失效了,无论是STA = ...
- Vue 2.0 Application Sample
===搭建Demo=== http://blog.csdn.net/wangjiaohome/article/details/51728217 ===单页Application=== http://b ...
- Bzoj1015/洛谷P1197 [JSOI2008]星球大战(并查集)
题面 Bzoj 洛谷 题解 考虑离线做法,逆序处理,一个一个星球的加入.用并查集维护一下连通性就好了. 具体来说,先将被消灭的星球储存下来,先将没有被消灭的星球用并查集并在一起,这样做可以路径压缩,然 ...
- MYSQL注入天书之导入导出介绍
Background-3 导入导出相关操作的讲解 load_file()导出文件 Load_file(file_name):读取文件并返回该文件的内容作为一个字符串. 使用条件: A.必须有权限读取并 ...