bzo4802 欧拉函数 miller_rabin pollard_rho
欧拉函数
Time Limit: 5 Sec Memory Limit: 256 MB
Submit: 1112 Solved: 418
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
Sample Output
HINT
Source
大整数分解主要背代码,证明非常麻烦。
题目bzoj4802是到经典例题
主要用到了miller_rabin和pollard_rho,算法导论p567与p571
以下是比较理想代码,算法复杂度n^(1/4),及——根号根号n,用到了以下map
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<map>
#include<ctime>
typedef long long ll;
using namespace std;
const int times=;
int number=;
map<ll,int>m;
ll q_mul(ll a,ll b,ll mod)
{
ll ans=;
while (b)
{
if (b&)
{
ans=(ans+a)%mod;
}
b/=;
a=(a+a)%mod;
}
return ans;
}
ll q_pow(ll a,ll b,ll mod)
{
ll ans=;
while (b)
{
if (b&)
{
ans=q_mul(ans,a,mod);
}
b/=;
a=q_mul(a,a,mod);
}
return ans;
}
bool witness(ll a,ll n)
{
ll tem=n-;
int j=;
while (tem%==)
{
tem/=;
j++;
}
ll p;
ll x=q_pow(a,tem,n);
while (j--)
{
p=q_mul(x,x,n);
if (p== && x!= && x!=n-) return true;
x=p;
}
if (p!=) return true;
else return false;
}
bool miller_rabin(ll n)
{
if (n==)
return true;
if (n<||n%==)
return false;
for (int i=;i<=times;i++)
{
long long a=rand()%(n-)+;
if (witness(a,n))
return false;
}
return true;
}
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
long long pollard_rho(ll n,ll c)
{
ll x,y,d,i=,k=;
x=rand()%(n);
y=x;
while()
{
i++;
x=(q_mul(x,x,n)+c)%n;
d=gcd(y-x,n);
if (<d&&d<n)
return d;
if (y==x)
return n;
if (i==k)
{
y=x;
k*=;
}
if (i*i>n) return n;
}
}
void find(ll n)
{
if (n==) return;
if(miller_rabin(n))
{
m[n]++;
number++;
return;
}
ll p=n;
while (p==n)
p=pollard_rho(p,rand()%(n));
find(p);
find(n/p);
}
int main()
{
srand((unsigned)time(NULL));
ll tar;
while (~scanf("%lld",&tar))
{
ll fzy=tar;
number=;
m.clear();
find(tar);
for (map<ll,int>::iterator c=m.begin();c!=m.end();++c)
{
ll x=c->first;
fzy=fzy/x*(x-);
}
printf("%lld\n",fzy);
}
}
bzo4802 欧拉函数 miller_rabin pollard_rho的更多相关文章
- 【BZOJ4802】欧拉函数(Pollard_rho)
[BZOJ4802]欧拉函数(Pollard_rho) 题面 BZOJ 题解 这么大的范围肯定不好杜教筛. 考虑欧拉函数的计算式,显然只需要把\(n\)分解就好了. 直接\(Pollard\_rho\ ...
- BZOJ 4802 欧拉函数(Pollard_Rho)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题目大意] 已知N,求phi(N),N<=10^18 [题解] 我们用P ...
- 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式
找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...
- BZOJ4802:欧拉函数(Pollard-Rho,欧拉函数)
Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Output 4 Soluti ...
- 【BZOJ4803】逆欧拉函数
[BZOJ4803]逆欧拉函数 题面 bzoj 题解 题目是给定你\(\varphi(n)\)要求前\(k\)小的\(n\). 设\(n=\prod_{i=1}^k{p_i}^{c_i}\) 则\(\ ...
- UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。
10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...
- POJ3090 巧用欧拉函数 phi(x)
POJ3090 给定一个坐标系范围 求不同的整数方向个数 分析: 除了三个特殊方向(y轴方向 x轴方向 (1,1)方向)其他方向的最小向量表示(x,y)必然互质 所以对欧拉函数前N项求和 乘2(关于( ...
- BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)
题目 求大数的欧拉函数φ\varphiφ 题解 Pollard-Rho 板子 CODE #pragma GCC optimize (3) #include <bits/stdc++.h> ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
随机推荐
- 拥抱移动端,jQueryui触控设备兼容插件
http://touchpunch.furf.com/ ps:要FQ. jQuery UI Touch Punch Touch Event Support for jQuery UI Tested o ...
- java学习笔记-9.违例差错控制
1.违例规范是告诉程序员这个方法可能抛出哪些类型的异常.他的格式在方法声明中,位于自变量(参数)列表的后面,如void f() throws tooBig, tooSmall, divZero { ...
- c++容器 STL
2019-01-24 22:30:32 记录学习PAT的一些知识,有待更新 注:本文是对Algorithm 算法笔记 的总结 C++标准库模板(Standard Template Library,ST ...
- jQuery实现checkbox(复选框)选中、全选反选代码
谁都知道 在html 如果一个复选框被选中 是 checked="checked". 但是我们如果用jquery alert($("#id").attr(&qu ...
- [c++] Getting Started - CMake
CMake is an open-source cross platform build system, according to CMake's creator, Kitware. But CMak ...
- POJ 3384 Feng Shui(计算几何の半平面交+最远点对)
Description Feng shui is the ancient Chinese practice of placement and arrangement of space to achie ...
- c语言乐曲演奏——《千本樱》
这个程序着实花费了我好长的时间,我本身对音乐一窍不通,先是跟着girl friend学习了简谱,根据c调44拍的<千本樱>写下了下面的程序. #include<stdio.h> ...
- Mysql8 忘记Root密码(转)
第一步:修改配置文件免密码登录mysql vim /etc/my.cnf 1.2 在 [mysqld]最后加上如下语句 并保持退出文件: skip-grant-tables 1.3 重启mysql服务 ...
- mysqlslap工具测试mysql DB的性能
mysqlslap的一个主要工作场景就是对数据库服务器做基准测试. 测试方法 1.测试工具:mysqlslap,mysqlslap是MySQL5.1.4之后自带的benchmark基准测试工具 ...
- ubuntu 安装lua错误
转自:http://www.cnblogs.com/softidea/archive/2016/03/02/5236498.html lua.c:80:31: fatal error: readlin ...