2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)
传送门
Pollard−rhoPollard-rhoPollard−rho模板题。
题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18
先把nnn用Pollard−rhoPollard-rhoPollard−rho分解质因数,然后就可以算了。
代码:
#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define ri register int
using namespace std;
typedef unsigned int uint;
typedef long long ll;
tr1::unordered_map<ll,int>S;
int pri[10]={2,3,5,7,11,13,17,19,23,29};
vector<ll>fac;
inline uint unit(){
static uint state0=19491001;
state0^=(state0>>13);
state0^=(state0<<17);
state0^=(state0>>5);
return state0;
}
inline ll ksc(ll a,ll b,ll mod){return (a*b-(ll)((long double)a/mod*b)*mod+mod)%mod;}
inline ll ksm(ll a,ll p,ll mod){ll ret=1;a%=mod;for(;p;p>>=1,a=ksc(a,a,mod))if(p&1)ret=ksc(ret,a,mod);return ret;}
inline bool check(ll x,ll a,ll s,ll t){
a=ksm(a,t,x);
ll p=a;
if(a==1||a==x-1)return 1;
while(s--){
a=ksc(p,p,x);
if(a==1&&(p!=x-1&&p!=1))return 0;
p=a;
}
return p==1;
}
inline bool MRT(ll x){
if(x==2||x==3)return fac.push_back(x),1;
if(!(x&1))return 0;
if(x%6!=1&&x%6!=5)return 0;
ll s=0,t=x-1;
while(!(t&1))t>>=1,++s;
for(ri i=0;i<10;++i){
if(x==pri[i])return fac.push_back(x),1;
if(!(x%pri[i]))return 0;
if(!check(x,pri[i],s,t))return 0;
}
return fac.push_back(x),1;
}
inline ll F(ll x,ll c,ll mod){return (ksc(x,x,mod)+c)%mod;}
inline ll gcd(ll a,ll b){while(b){ll t=a;a=b,b=t%a;}return a;}
inline ll rho(ll n,ll c){
ll x=unit()%n+1,y=x,p=1;
for(ri i=1,k=2;p==1;++i){
x=F(x,c,n),p=gcd(y>x?y-x:x-y,n);
if(i==k)y=x,k<<=1;
}
return p;
}
inline void solve(ll n){
if(n==1||MRT(n))return;
ll d=rho(n,unit()%n);
while(d==n)d=rho(n,unit()%n);
solve(n/d),solve(d);
}
int main(){
freopen("lx.in","r",stdin);
ll n,ans;
scanf("%lld",&n),solve(n),ans=n;
for(ri i=fac.size()-1;~i;--i){
if(S[fac[i]])continue;
S[fac[i]]=1,ans=ans/fac[i]*(fac[i]-1);
}
return cout<<ans,0;
}
2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)的更多相关文章
- BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)
题目 求大数的欧拉函数φ\varphiφ 题解 Pollard-Rho 板子 CODE #pragma GCC optimize (3) #include <bits/stdc++.h> ...
- bzoj4802 欧拉函数(附Millar-Rabin和Pollard-Rho讲解)
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题解] 参考:http://www.matrix67.com/blog/archiv ...
- BZOJ4802:欧拉函数(Pollard-Rho,欧拉函数)
Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Output 4 Soluti ...
- [日常摸鱼]bzoj4802 欧拉函数-PollardRho大整数分解算法
啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400 ...
- [BZOJ4802]欧拉函数
bzoj description 给出\(n\),求\(\varphi(n)\).\(n\le10^{18}\) sol \(Pollard\ Rho\),存个代码. code #include< ...
- BZOJ4802 欧拉函数 数论
原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html 题目传送门 - BZOJ4802 题意概括 Description 已知N,求phi(N) ...
- 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式
找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...
- 【BZOJ4802】欧拉函数(Pollard_rho)
[BZOJ4802]欧拉函数(Pollard_rho) 题面 BZOJ 题解 这么大的范围肯定不好杜教筛. 考虑欧拉函数的计算式,显然只需要把\(n\)分解就好了. 直接\(Pollard\_rho\ ...
- BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数
BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sa ...
随机推荐
- 33. Search in Rotated Sorted Array (Array;Divide-and-Conquer)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- jggrid应用,后台c#
参考网址: 1.https://www.cnblogs.com/miro/p/jqGrid.html 2.https://blog.csdn.net/ainuser/article/details/6 ...
- django的模板语言中一些之前没有用过的小点
1.模板语言中的for循环的最后一个的表示方式 {% for auther in auther_list %} {% if forloop.last %} {# 这里的意思是for循环的最后一个的意思 ...
- [leetcode]387. First Unique Character in a String第一个不重复字母
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- Linux下新建服务
1 首先在/etc/rc.d/init.d/下添加脚本 asr_cron #!/bin/bash # $Id: rc.redhat.asterisk -- ::43Z tilghman $ # # a ...
- Non-negative Integers without Consecutive Ones
n位二进制,求不包含连续1的二进制(n位)数字个数. http://www.geeksforgeeks.org/count-number-binary-strings-without-consecut ...
- 10.24JS日记
1.函数都有返回值,人为return,返回什么就是什么,否则,他的返回值就是undefined 而方法的本质也是函数,所以也有返回值 document.getElementById()返回的是获取的标 ...
- table-layout 显示规则以及其他一些零碎的东西
首先对中文显示的不够好 对中文失效 auto是表格的宽和高都会随着内容增多而改变 而fixed只会增加表格的高度 宽度不会发生改变 table中的td的宽,高会根据内容的多少而变化: fix ...
- Oracle_PL/SQL(10) 定时器job
定时器job1.定义 定时器指在特定的时间执行特定的操作. 可以多次执行.说明:特定的操作:指一个完成特定功能的存储过程.多次执行:指可以每分钟.每小时.每天.每周.每月.每季度.每年等周期性的运行. ...
- Linux.CommanlineTool.grep
grep grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] 1. grep的Regula ...