【BZOJ4802】欧拉函数(Pollard_rho)

题面

BZOJ

题解

这么大的范围肯定不好杜教筛。

考虑欧拉函数的计算式,显然只需要把\(n\)分解就好了。

直接\(Pollard\_rho\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
ll N;
ll Multi(ll a,ll b,ll MOD)
{
ll s=0;
while(b){if(b&1)s=(s+a)%MOD;a=(a+a)%MOD;b>>=1;}
return s;
}
ll fpow(ll a,ll b,ll MOD)
{
ll s=1;
while(b){if(b&1)s=Multi(s,a,MOD);a=Multi(a,a,MOD);b>>=1;}
return s;
}
bool Miller_Rabin(ll n)
{
if(n==2)return true;
for(int tim=10;tim;--tim)
{
ll a=rand()%(n-2)+2,p=n-1;
if(fpow(a,n-1,n)!=1)return false;
while(!(p&1))
{
p>>=1;ll nw=fpow(a,p,n);
if(Multi(nw,nw,n)==1&&nw!=1&&nw!=n-1)return false;
}
}
return true;
}
vector<ll> fac;
ll Pollard_rho(ll n,int c)
{
ll i=0,k=2,x=rand()%(n-1)+1,y=x;
while(233)
{
++i;x=(Multi(x,x,n)+c)%n;
ll d=__gcd((y-x+n)%n,n);
if(d!=1&&d!=n)return d;
if(x==y)return n;
if(i==k)y=x,k<<=1;
}
}
void Fact(ll n,int c)
{
if(n==1)return;
if(Miller_Rabin(n)){fac.push_back(n);return;}
ll p=n;while(p>=n)p=Pollard_rho(p,c--);
Fact(p,c);Fact(n/p,c);
}
int main()
{
cin>>N;Fact(N,233);
sort(fac.begin(),fac.end());
int l=unique(fac.begin(),fac.end())-fac.begin();
for(int i=0;i<l;++i)N=N*(1.0*(fac[i]-1)/fac[i]);
cout<<N<<endl;
return 0;
}

【BZOJ4802】欧拉函数(Pollard_rho)的更多相关文章

  1. BZOJ4802:欧拉函数(Pollard-Rho,欧拉函数)

    Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Output 4 Soluti ...

  2. BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)

    题目 求大数的欧拉函数φ\varphiφ 题解 Pollard-Rho 板子 CODE #pragma GCC optimize (3) #include <bits/stdc++.h> ...

  3. bzoj4802 欧拉函数(附Millar-Rabin和Pollard-Rho讲解)

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题解] 参考:http://www.matrix67.com/blog/archiv ...

  4. [日常摸鱼]bzoj4802 欧拉函数-PollardRho大整数分解算法

    啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400 ...

  5. BZOJ4802 欧拉函数 数论

    原文链接http://www.cnblogs.com/zhouzhendong/p/8117744.html 题目传送门 - BZOJ4802 题意概括 Description 已知N,求phi(N) ...

  6. 2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)

    传送门 Pollard−rhoPollard-rhoPollard−rho模板题. 题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18 先把nnn用Pollar ...

  7. [BZOJ4802]欧拉函数

    bzoj description 给出\(n\),求\(\varphi(n)\).\(n\le10^{18}\) sol \(Pollard\ Rho\),存个代码. code #include< ...

  8. bzo4802 欧拉函数 miller_rabin pollard_rho

    欧拉函数 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 1112  Solved: 418[Submit][Status][Discuss] Descr ...

  9. BZOJ 4802 欧拉函数(Pollard_Rho)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4802 [题目大意] 已知N,求phi(N),N<=10^18 [题解] 我们用P ...

随机推荐

  1. JUnit在intellij idea中只能在test里面才能使用,否则不能添加import

    只能在 src下的test下使用 不能再main下使用 否则不能import到指定的junit包 idea这样做的好处就是分离主项目和测试项目,这样一来就能够更加方便的测试了 如图直接这样把整个主包  ...

  2. LUA对象

    Rectangle = {width = , height = , area = }; function Rectangle:new(o, width, height) o = o or {}; se ...

  3. cocos2dx2.0 帧动画的创建和播放过程 深入分析

    一.帧动画的创建过程帧动画的实现有四个不可或缺的类,如下:1.CCSpriteFrame:精灵帧信息.存储帧动画的每一帧的纹理基本信息. class CC_DLL CCSpriteFrame : pu ...

  4. php实现快速排序和冒泡排序

    快速排序 实现思路:把第一个元素作为标记,依次判断后续的值,如果小于它则放在左边,如果大于它则放右边,同理把左右两部分看成一个整体一直递归,最后再数组拼接起来 它的最优时间复杂度为O(nlogn)[以 ...

  5. Django——多网页网站及网页互联

    在helloapp文件夹下添加名为templates的文件夹(此文件夹名称是固定的),并在其下添加html文件,文件内容根据自己网页想呈现的内容而定 在views文件内添加新的函数 在urls文件内添 ...

  6. 7个Node.js的Web框架

    NodeJS也就是Node,是众所周知的使用javascript构建Web应用框架,它启动一个服务器非常简单,如下: var http = require('http'); http.createSe ...

  7. linux源码学习-for_each_cpu

    刚开始读linux源码,第一眼就看到了这个很有意思的函数族,周末好好研究一下 3.13 这个组都是宏定义for循环,分析的时候注意到cpumask_next(),它在一个文件中定义了两次,还不是重载, ...

  8. 请教Amazon FBA里面Label Service, Stickerless, Commingled Inventory是什么意思?

    Accept Label Service接受标签服务,选择了以后下面的操作中会有一个让您打印标签的流程,您就可以按照FBA流程提示进行每一步标签服务的操作. Accept Stickless, Com ...

  9. [Linux] Migrate plugins and setting for vim

    Hot to migrate plugins and setting for vim from one computer to another ? Just copy  ~/.vim and ~/.v ...

  10. 测试与优化bugbugbugbug

    单元测试