poj1284 && caioj 1159 欧拉函数1:原根
这道题不知道这个定理很难做出来。
除非暴力找规律。
我原本找的时候出了问题
暴力找出的从13及以上的答案就有问题了
因为13的12次方会溢出
那么该怎么做?
快速幂派上用场。
把前几个素数的答案找出来。
然后因为原根的一个条件是与p互质,所以可以把欧拉函数的值求出来尝试一下
打印出来,就可以发现规律
答案就是phi(p-1)
暴力找答案代码
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
const int MAXN = 21234567;
bool vis[MAXN];
void read(ll& x)
{
int f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
}
ll cal(ll a, ll b, ll p)
{
ll ret = 1 % p; a %= p;
while(b)
{
if(b & 1) ret = (ret * a) % p;
b >>= 1;
a = (a * a) % p;
}
return ret;
}
int main()
{
while(1)
{
ll p;
read(p);
int ans = 0;
_for(x, 1, p - 1)
{
memset(vis, 0, sizeof(vis));
_for(i, 1, p - 1)
{
ll t = cal(x, i, p);
if(vis[t] || t == 0) break;
vis[t] = 1;
if(i == p - 1) ans++;
}
}
printf("p: %d ans: %d\n", p, ans);
}
return 0;
}
求欧拉函数值代码
#include<cstdio>
#include<cctype>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
const int MAXN = 21234567;
ll euler(ll x)
{
ll ret = x;
for(int i = 2; i * i <= x; i++)
if(x % i == 0)
{
ret = ret / i * (i - 1);
while(x % i == 0) x /= i;
if(x == 1) break;
}
if(x > 1) ret = ret / x * (x - 1);
return ret;
}
void read(ll& x)
{
int f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
}
int main()
{
_for(i, 1, 30) printf("i: %d euler[i]: %lld\n", i, euler(i));
return 0;
}
AC代码
#include<cstdio>
#include<cctype>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
const int MAXN = 21234567;
ll euler(ll x)
{
ll ret = x;
for(int i = 2; i * i <= x; i++)
if(x % i == 0)
{
ret = ret / i * (i - 1);
while(x % i == 0) x /= i;
if(x == 1) break;
}
if(x > 1) ret = ret / x * (x - 1);
return ret;
}
void read(ll& x)
{
int f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
}
int main()
{
ll n, x; read(n);
while(n--)
{
read(x);
printf("%lld\n", euler(x-1));
}
return 0;
}
poj1284 && caioj 1159 欧拉函数1:原根的更多相关文章
- POJ1284 Primitive Roots [欧拉函数,原根]
题目传送门 Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5434 Accepted: ...
- caioj 1161 欧拉函数3:可见点数
(x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...
- 【poj 1284】Primitive Roots(数论--欧拉函数 求原根个数){费马小定理、欧拉定理}
题意:求奇质数 P 的原根个数.若 x 是 P 的原根,那么 x^k (k=1~p-1) 模 P 为1~p-1,且互不相同. (3≤ P<65536) 解法:有费马小定理:若 p 是质数,x^( ...
- poj1248 (线性筛欧拉函数)(原根)
强烈鸣谢wddwjlss 题目大意:给出一个奇素数,求出他的原根的个数,多组数据. 这里先介绍一些基本性质 阶 设\((a,m)=1\),满足\(a^r \equiv 1 \pmod m\)的最小正整 ...
- caioj 1158 欧拉函数
直接套模板,这道题貌似单独求还快一些 解法一 #include<cstdio> #include<cctype> #define REP(i, a, b) for(int i ...
- poj1284(欧拉函数+原根)
题目链接:https://vjudge.net/problem/POJ-1284 题意:给定奇素数p,求x的个数,x为满足{(xi mod p)|1<=i<=p-1}={1,2,...,p ...
- poj1284:欧拉函数+原根
何为原根?由费马小定理可知 如果a于p互质 则有a^(p-1)≡1(mod p)对于任意的a是不是一定要到p-1次幂才会出现上述情况呢?显然不是,当第一次出现a^k≡1(mod p)时, 记为ep(a ...
- (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
/* * POJ_2407.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...
- 数学之欧拉函数 &几道poj欧拉题
欧拉函数总结+证明 欧拉函数总结2 POJ 1284 原根 #include<iostream> #include<cstdio> #include<cstring> ...
随机推荐
- 浅谈自底向上的Shell脚本编程及效率优化
作者:沐星晨 出处:http://blog.csdn.net/sosodream/article/details/6276758 浅谈自底向上的Shell脚本编程及效率优化 小论文,大家多批评指导:) ...
- These relative modules were not found:...{"sourceM ap":false} 报错解决
今天在使用vue2.0 + webpack 时,没有动过任何配置文件,也没更新依赖,但是报下面的错误: These relative modules were not found: * ./star1 ...
- [读书笔记] R语言实战 (六) 基本图形方法
1. 条形图 barplot() #载入vcd包 library(vcd) #table函数提取各个维度计数 counts <- table(Arthritis$Improved) count ...
- STM32 HAL库 IIC 协议库函数
/* 第1个参数为I2C操作句柄 第2个参数为从机设备地址 第3个参数为从机寄存器地址 第4个参数为从机寄存器地址长度 第5个参数为发送的数据的起始地址 第6个参数为传输数据的大小 第7个参数为操作超 ...
- Apache Tez 0.7、0.83、 0.82 安装、调试笔记
———————————————————— 准备 Tez 编译环境 ———————————————————— 1 需要的支持 tez0.7 需要 Hadoop 2.60 以上 2 需要的 linux 相 ...
- Java ThreadLocal类学习
ThreadLocal为线程局部变量,通过线程名(key)-对象(value)的Map来获取每个线程对应的对象.我们不能通过ThreadLocal处理多线程并发问题,但是每个线程可以通过ThreadL ...
- xcode5.1生成framework,支持arm64报错
错误例如以下: ld: Assertion failed: (_machoSection != 0), function machoSection, file /SourceCache/ld64/ld ...
- HDU5233
Gunner II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- jQuery操作元素的属性与样式
本文学习如何使用jQuery获取和操作元素的属性和CSS样式. 元素属性和Dom属性 对于下面这样一个标签元素: <img id='img' src="1.jpg" alt= ...
- JAVA设计模式之【观察者模式】
观察者模式 交通信号灯是汽车的观察目标,汽车是观察者 一个对象的状态或行为的变化将导致其他对象的状态或行为也发生变化 为了描述这种一对多或一对一的联动,观察者模式应运而生 在观察者模式中,发生改变的对 ...