(Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
/*
* POJ_2407.cpp
*
* Created on: 2013年11月19日
* Author: Administrator
*/ #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; typedef long long ll; const int maxn = 1000015; bool u[maxn];
ll su[maxn];
ll num; ll gcd(ll a, ll b) {
if (b == 0) {
return a;
} return gcd(b, a % b);
} void prepare() {//欧拉筛法产生素数表
ll i, j;
memset(u, true, sizeof(u)); for (i = 2; i <= 1000010; ++i) {
if (u[i]) {
su[++num] = i;
} for (j = 1; j <= num; ++j) {
if (i * su[j] > 1000010) {
break;
} u[i * su[j]] = false; if (i % su[j] == 0) {
break;
}
}
}
} ll phi(ll x) {//欧拉函数,用于求[1,x)中与x互质的整数的个数
ll ans = 1;
int i, j, k;
for (i = 1; i <= num; ++i) {
if (x % su[i] == 0) {
j = 0;
while (x % su[i] == 0) {
++j;
x /= su[i];
} for (k = 1; k < j; ++k) {
ans = ans * su[i] % 1000000007ll;
}
ans = ans * (su[i] - 1) % 1000000007ll;
if (x == 1) {
break;
}
}
} if (x > 1) {
ans = ans * (x - 1) % 1000000007ll;
} return ans;
} int main() {
prepare();
int n;
while (scanf("%d", &n) != EOF) {
printf("%lld\n", phi(n-1));//以n为模的本原根的个数为phi(n-1)。而,[1,n]与n互质的整数的个数为phi(n)
} return 0;
}
(Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))的更多相关文章
- POJ 1284 Primitive Roots (欧拉函数+原根)
<题目链接> 题目大意: 满足{ ( $x^{i}$ mod p) | 1 <=$i$ <= p-1 } == { 1, …, p-1 }的x称为模p的原根.给出p,求原根个数 ...
- POJ1284 Primitive Roots [欧拉函数,原根]
题目传送门 Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5434 Accepted: ...
- 【POJ1284】Primitive Roots 欧拉函数
题目描述: 题意: 定义原根:对于一个整数x,0<x<p,是一个mod p下的原根,当且仅当集合{ (xi mod p) | 1 <= i <= p-1 } 等于{ 1, .. ...
- POJ 1284 Primitive Roots 原根
题目来源:POJ 1284 Primitive Roots 题意:求奇素数的原根数 思路:一个数n是奇素数才有原根 原根数是n-1的欧拉函数 #include <cstdio> const ...
- poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)
http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...
- POJ 2407 Relatives(欧拉函数)
http://poj.org/problem?id=2407 题意: 给出一个n,求小于等于的n的数中与n互质的数有几个. 思路: 欧拉函数的作用就是用来求这个的. #include<iostr ...
- POJ 1284 Primitive Roots 数论原根。
Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2479 Accepted: 1385 D ...
- 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 题目链接:id=1284">http://poj.org/problem?id=1284 利用定理:素数 P 的原根的个数为euler(p - 1) t ...
随机推荐
- GridView下DropDownList 的选择方法onselectedindexchanged 实现方法
在GridView下面绑定好了下拉框,我们常常会遇到一个问题, 选择方法怎么实现呢,用js总是难的去算是在GridView的第几行第几个元素,因为服务器的id和客户端的id经常变化让js根本无从找起, ...
- 'swap file "xx" exists' linux
solution: 1)swap to another tty, kill processes using 'sudo kill -9 pid' 2)'Recover' the 'warn-openn ...
- java.io.FileNotFoundException: class path resource [bean/test/User.hbm.xml] cannot be opened because it does not exist
确定下 WEB-INF/classes下有没有,不是src下哦 工程的src下创建后,会发布到tomcat下项目下的classes中
- mysql查询计划
mysql查询计划 1:客户端发起查询请求 2:服务器接收到请求后,先查询缓存 如果缓存命中,直接返回数据给客户端 否则,解析sql 3:sql解析完成后,进行预处理 4:有查询优化器生存查询计划 5 ...
- 【转】windows消息16进制对应表
来源:http://blog.sina.com.cn/s/blog_962250db0101d4mj.html windows mobile编程,无论使用eVC还是.net CF,都脱不开window ...
- DOM---documentFragment
代码暂时寄存 创建:document.createDocumentFragment() 案例: var a=document.createDocumentFragment(); var b=docum ...
- Android webview 应用
现在在android把一个网站打包成apk 是很简单的事,不懂一点代码的都可以去百度生成一个apk,app.当然样式就不是你所能控制的. 今天我就介绍一下 android 的 webview 在And ...
- UBUNTU 下设置全局 path变量
全局的对所有用户都可以的使用的PATH: 可以通过修改配置文件: /etc/bashrc 和 /etc/profile 来时配置, 全局的PATH; 例如: vi /etc/profile 在最后后加 ...
- centos 开启apache rewrite模式
mod_rewrite能使网页伪静态,对于搜索引擎友好,下面就是开启这个功能的说明!启用mod_rewrite模块在conf目录的httpd.conf文件中找到 LoadModule rewrite_ ...
- LNMP笔记:域名重定向、读写权限、显示WP主题、北京时间
边写边记,以后还会用到的. 将 xxx.com 重定向到 www.xxx.com 1.打开 /usr/local/nginx/conf/vhost/你网站的域名.com.conf 2.查看原有的 se ...