BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数

Description

已知N,求phi(N)

Input

正整数N。N<=10^18

Output

输出phi(N)

Sample Input

8

Sample Output

4

直接MR+Pollard rho分解质因数即可。具体可见https://www.cnblogs.com/suika/p/9127065.html
记得判重,我的map不知道为何T了。
 
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
typedef double f2;
map<ll,int>mp;
ll ch(ll x,ll y,ll mod) {ll re=0;for(;y;y>>=1ll,x=(x+x)%mod) if(y&1ll) re=(re+x)%mod; return re;}
ll random(ll x,ll y) {return ((rand()*(1ll<<45))+(rand()*(1ll<<30))+(rand()<<15)+(rand()))%(y-x+1)+x;}
ll qp(ll x,ll y,ll mod) {ll re=1;for(;y;y>>=1ll,x=ch(x,x,mod)) if(y&1ll) re=ch(re,x,mod); return re;}
ll a[]={2,3,5,7,11,13,17,19,23,29};
ll ans;
ll Abs(ll x) {return x>0?x:-x;}
ll gcd(ll x,ll y) {return y?gcd(y,x%y):x;}
ll b[250000];
bool check(ll a,ll n,ll r,ll s) {
ll x=qp(a,r,n),y=x,i;
for(i=1;i<=s;i++,y=x) {x=ch(x,x,n); if(x==1&&y!=1&&y!=n-1) return 0;}
return x==1;
}
bool MR(ll n) {
if(n<=1) return 0; ll r=n-1,s=0,i;
for(;!(r&1);r>>=1ll,s++);
for(i=0;i<=9;i++) {
if(a[i]==n) return 1;
if(!check(a[i],n,r,s)) return 0;
}
return 1;
}
ll f(ll x,ll c,ll mod) {return (ch(x,x,mod)+c)%mod;}
ll PR(ll n,ll c) {
ll x=random(0,n-1),y=f(x,c,n),p;
for(p=1;p==1&&x!=y;) {
x=f(x,c,n); y=f(f(y,c,n),c,n); p=gcd(Abs(x-y),n);
}
return p==1?n:p;
/*ll k=2,x=rand()%n,y=x,p=1,i;
for(i=1;p==1;i++) {
printf("%lld %lld\n",x,y);
x=f(x,c,n); p=gcd(n,Abs(x-y)); if(i==k) y=x,k+=k;
}
return p;*/
}
void solve(ll n) {
if(n==1) return ;
if(MR(n)) {
b[++b[0]]=n;
return ;
}
ll t=n;
while(t==n) t=PR(n,rand()%n);
solve(t); solve(n/t);
}
int main() {
ll n;
srand(19260817);
scanf("%lld",&n); ans=n;
int i;
for(i=0;i<=9;i++) {
if(n%a[i]==0) {
ans=ans/a[i]*(a[i]-1); while(n%a[i]==0) n/=a[i];
}
}
solve(n);
int tot=unique(b+1,b+b[0]+1)-b-1;
for(i=1;i<=tot;i++) ans=ans/b[i]*(b[i]-1);
printf("%lld\n",ans);
}

BZOJ_4802_欧拉函数_MR+pollard rho+欧拉函数的更多相关文章

  1. BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数

    BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数 Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行 ...

  2. BZOJ4804 欧拉心算(莫比乌斯反演+欧拉函数+线性筛)

    一通套路后得Σφ(d)μ(D/d)⌊n/D⌋2.显然整除分块,问题在于怎么快速计算φ和μ的狄利克雷卷积.积性函数的卷积还是积性函数,那么线性筛即可.因为μ(pc)=0 (c>=2),所以f(pc ...

  3. 初学Pollard Rho算法

    前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...

  4. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  5. Pollard Rho 算法简介

    \(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...

  6. Pollard Rho算法浅谈

    Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...

  7. 第三十五个知识点:给针对ECDLP问题的Pollard rho,Pollard "Kangaroo",parallel Pollard rho攻击的一个粗略的描述

    第三十五个知识点:给针对ECDLP问题的Pollard rho,Pollard "Kangaroo",parallel Pollard rho攻击的一个粗略的描述 我们的目标是对任 ...

  8. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  9. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

随机推荐

  1. 树莓派 - 驱动hello

    树莓派上Linux驱动,从hello world 开始 ... hello.c #include <linux/init.h> #include <linux/module.h> ...

  2. phtyon,通过while循环简单的用户名和密码登录

    _username='zhangxin' _password='abc123' _username1='zhaopeng' _password1='abc1234' _username2=" ...

  3. STM32F407 开发环境搭建 程序下载 个人笔记

    详细资料: http://www.openedv.com/thread-13912-1-1.html 需要安装的软件: 1.keil(MDK,必选),用keygen破解 2.CH340驱动,(usb串 ...

  4. Python字典(Dictionary)

    Python字典 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = {key1 : value1, ...

  5. HDU 4473

    题目大意: 给定一个long long 型的数 n,找到一共有多少对a,b,使比n小的某一个数的是a*b的倍数 这样我们可以理解为 存在a*b*c <= n,令 a <= b <= ...

  6. [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)

    传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...

  7. MTK平台释疑android M 配置中断相关问题

    1.使用老方法(android L)配置中断,调用request_irq函数时出错,错误代码 -22  Dear Customer: 您好! 如电话沟通,贵司可以在发过来的code基础上做下面的修改再 ...

  8. node.js 读取文件--createReadStream

    createReadStream 是fs模块里面读流的一个方法 这个方法基于fs模块的,所以我们先要引进fs模块 let fs=require("fs"); createReadS ...

  9. .net如何统计在线人数

    原文发布时间为:2008-10-17 -- 来源于本人的百度文章 [由搬家工具导入] 统计在线用户的作用不言而喻,就是为了网站管理者可以知道当前用户的多少,然后根据用户数量来观察服务器或者程序的性能, ...

  10. Charm Bracelet-POJ3624(01背包)

    http://poj.org/problem?id=3624 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...