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> ...
随机推荐
- JS - 浅拷贝与深拷贝的理解以及简单实现方法
前几天撸项目代码时, 由一个技术点间接牵扯出了这东西. 所以就来总结一下. 深拷贝 拷贝对象每个层级的属性. 作用的对象是 js中引用类型的对象,基本类型没有涉及. 本质上将引用类型的对象在堆上重新开 ...
- 八、frps服务端与nginx可共用80端口
我的服务器,已经用nginx 做网站了,80端口只有一个,我还想我的frps一起使用,可以吗?这个是可以实现的,利用nginx的反向代理实现. 以下是在frps服务器上安装的nginx配置文件中设置的 ...
- 使用python备份指定目录并删除备份超过一定时长的文件
#!/usr/bin/env python #-*- coding: utf-8 -*- """ @Project:Py @author: @Email: @Softwa ...
- 论文阅读笔记“Attention-based Audio-Visual Fusion for Rubust Automatic Speech recognition”
关于论文的阅读笔记 论文的题目是“Attention-based Audio-Visual Fusion for Rubust Automatic Speech recognition”,翻译成中文为 ...
- jvm 虚拟机参数_栈内存分配
1.参数 -Xss 指定线程最大的栈空间,整个参数也直接决定了函数可调用的最大深度 2.测试代码 private static int count; public static void addCou ...
- iipccsxxtnsoiq
gxspvyheuetwqgnbwmwd
- QQ在线人数统计图数据解析
转载请注明出处:http://blog.csdn.net/xiaoy_h/article/details/27980851 我相信非常多人一定去过这个地方: http://im.qq.com/onli ...
- Java程序命令行打包Jar
最近要跑爬虫程序,需要打包成jar发在linux服务器中运行.主要是第三方的lib包与配置文件,不进行打包,方便修改. 1.eclipse中src中源码编译后生成的源码在bin文件中,把里面源码单独拿 ...
- Maven导入ojdbc14.jar和ojdbc6.jar
Maven导入ojdbc14.jar和ojdbc6.jar 学习了:http://blog.csdn.net/johon_medison/article/details/51689690 在 ‘运行’ ...
- iptables 防火墙 只允许某IP访问某端口、访问特定网站
iptables 防火墙 只允许某IP访问某端口.访问特定网站 1.先备份iptables /var/tmp 需要开80端口,指定IP和局域网 下面三行的意思: 先关闭所有的80端口 开启ip段192 ...