(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 ...
随机推荐
- table的border-collapse属性与border-spacing属性
table border-collapse:collapse; 表示边框合并在一起. border-collapse:separate;表示边框之间的间距,间距的大小用border-spacing:p ...
- linux查询当前进程数的命令
$command = "ps aux | grep 'zb_insure/get_order_info_from_queue.php' | grep -v 'grep' | grep - ...
- Navicat:cant create OCI environment.
一直在使用 Navicat ,这是一个数据库客户端软件,能连接多种不同类型的数据库,给我们的日常的工作带来了不少的便捷. 最近,我在电脑上安装了oracle的客户端ODTwihtODAC121012, ...
- Debugging Failed Because Integrated Windows Authentication Is Not Enabled
To enable integrated Windows authentication Log onto the Web server using an administrator account. ...
- LayoutInflater.inflate() 参数研究
参考连接:http://blog.csdn.net/lovexieyuan520/article/details/9036673 http://www.2cto.com/kf/201407/31305 ...
- RecycleView 滑动到底部,加载更多
android.support.v7 包提供了一个新的组件:RecycleView,用以提供一个灵活的列表试图.显示大型数据集,它支持局部刷新.显示动画等功能,可以用来取代ListView与GridV ...
- asp.net 定时器
using System;using System.Collections.Generic;using System.Web;using System.IO;using System.Web.Secu ...
- BetWeen和模糊查询
--区分大小写性能比较低select * from Students where Age >1 and Age <4select * from Students where Age bet ...
- access 2007 vba 开发中学到的知识(二)
文件的导入和导出 excel 'excel导入Private Sub btnInExcel_Click() Dim strSelectFile As StringWith Application.Fi ...
- 使用shiro标签遇到的部分问题的解决思路
最近几天,在shiro进行系统权限控制.在处理JSP页面的时候,遇到几个问题,纠结好几天,终于成功解决这些问题. 1.使用<shiro:principal>的时候,如何得到整个类的信息? ...