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> ...
随机推荐
- 2017CCPC秦皇岛
热身赛 B题 Smartphone: 大整数相乘 Time Limit: 1 Second Memory Limit: 65536 KBHelianthuswolf Co. Ltd. is a mul ...
- UVA272-TEX Quotes(紫书例题3.1)
TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few ty ...
- js 事件监听,执行某操作
<script language=javascript> var ie; var firefox; if (document.all) ie = true; else ie = false ...
- Git:Git的安装过程
Git:Git的安装过程 路径不要存在空格 默认即可,第一项为是否在页面显示 文本编辑器,默认VIM即可 设置环境变量: 1)最安全的选择,path环境变量不会改变,你只能在git bash里使用命令 ...
- hive的mysql作元数据的hive-site.xml配置
<property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://s ...
- Ubuntu(Linux Mint):sudo apt-get upgrade升级失败
Ubuntu上进行sudo apt-get upgrade后出现异常,升级失败. 异常信息如下: E: dpkg was interrupted, you must manually run 'dpk ...
- POJ 2183
模拟题 #include <iostream> #include <cstdio> #include <algorithm> using namespace std ...
- POJ3624 Charm Bracelet 【01背包】
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22621 Accepted: 10157 ...
- 【转载】SQLITE3 使用总结
转载自网友董淳光的文章. 前序: 这里要注明,我是一个跨平台专注者,并不喜欢只用 windows 平台.我以前的工作就是为 unix 平台写代码.下面我所写的东西,虽然没有验证,但是我已尽量不使用任何 ...
- Windows下面使用curl
Windows下面使用curl 学习了:https://www.cnblogs.com/xing901022/p/4652624.html 下载地址:https://curl.haxx.se/down ...