思路:

  因为当n>=1e10的时候,线性筛就不好使啦。所以要用一个公式

  φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn)

  证明详见:《公式证明:欧拉函数》

  Miller-Rabin算法:

    判断某个数是否是素数,不是素数则返回一个因子。

  Pollard-Rho算法:

    利用Miller-Rabin求出 质因数。

    具体的:

      如果当前的数不是质数,找质因数 再搜Rho(n/d)和Rho(d)

      如果是质数(不一定准确),再去判断。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<iomanip>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
#define ALL(x,S) for(x=S.begin();x!=S.end();x++)
typedef long long LL;
typedef long double real;
bool pd_prime[];
set<LL> S;
set<LL>::iterator pos;
LL n;
void prepare()
{
int i,j;
memset(pd_prime,,sizeof pd_prime);
pd_prime[]=;
for(int i=;i<=;i++)
if(pd_prime[i])
for(int j=;j<=/i;j++)
pd_prime[i*j]=;
}
void addp(LL t)
{ S.insert(t);}
LL gcd(LL a,LL b)
{
if(!b) return a;
return gcd(b,a%b);
}
LL mul(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&) ans=(ans+a)%mod;
a=(a+a)%mod;
b>>=;
}
return ans;
}
LL pow(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&) ans=mul(ans,a,mod);
b>>=;
a=mul(a,a,mod);
}
return ans;
}
bool MR(LL n,LL k)
{
LL s=n-,w=;
while(!(s&))
w++,s>>=;
LL y=pow(k,s,n);
if(y== || y==n-)
return ;
while(w--)
{
y=mul(y,y,n);
if(y==n-)
return ;
}
return ;
}
bool prime(LL n)
{
if(n<=)
return pd_prime[n];
bool flag=;
for(int i=;i<=;i++)
if(pd_prime[i])
if(!MR(n,i)) flag=;
return flag;
}
void rho(LL n)
{
if(n==) return ;
if(n==)
{
addp();
addp();
return ;
}
if(prime(n))
{
addp(n);
return ;
}
LL x,y,d,p;
while()
{
x=,y=,d=;
p=mul(rand(),rand(),);
if(d==)
{
x=(mul(x,x,n)+p)%n;
y=(mul(y,y,n)+p)%n;
y=(mul(y,y,n)+p)%n;
d=gcd(abs(x-y),n);
}
if(d==n) continue;
rho(d);rho(n/d);
return ;
}
}
LL phi(LL x)
{
S.clear();
rho(x);
LL ans=x;
ALL(pos,S)
ans=ans/(*pos)*((*pos)-);
return ans;
}
int main()
{
freopen("phi.in","r",stdin);
freopen("phi.out","w",stdout);
prepare();
scanf("%lld",&n);
cout<<phi(n);
return ;
}

求一个极大数的欧拉函数 phi(i)的更多相关文章

  1. POJ3090 巧用欧拉函数 phi(x)

    POJ3090 给定一个坐标系范围 求不同的整数方向个数 分析: 除了三个特殊方向(y轴方向 x轴方向 (1,1)方向)其他方向的最小向量表示(x,y)必然互质 所以对欧拉函数前N项求和 乘2(关于( ...

  2. hdu 3501 Calculation 2 (欧拉函数)

    题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质 ...

  3. 筛法求欧拉函数(poj2478

    求1-n的欧拉函数的值 #include <iostream> #include <cstdio> #include <queue> #include <al ...

  4. UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。

                                                    10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...

  5. POJ 2480 (约数+欧拉函数)

    题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n ...

  6. 数论 - 欧拉函数模板题 --- poj 2407 : Relatives

    Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Descri ...

  7. 欧拉函数 cojs 2181. 打表

    cojs 2181. 打表 ★☆   输入文件:sendtable.in   输出文件:sendtable.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 有一道比赛题 ...

  8. NOIP模拟:切蛋糕(数学欧拉函数)

    题目描述  BG 有一块细长的蛋糕,长度为 n. 有一些人要来 BG 家里吃蛋糕, BG 把蛋糕切成了若干块(整数长度),然后分给这些人. 为了公平,每个人得到的蛋糕长度和必须相等,且必须是连续的一段 ...

  9. 容斥原理、欧拉函数、phi

    容斥原理: 直接摘用百度词条: 也可表示为 设S为有限集, ,则 两个集合的容斥关系公式:A∪B = A+B - A∩B (∩:重合的部分) 三个集合的容斥关系公式:A∪B∪C = A+B+C - A ...

随机推荐

  1. python处理时间汇总

    1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 import time timeArray = time.strpt ...

  2. Codeforces617E XOR and Favorite Number(分块 异或)

    Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...

  3. <十七>UML核心视图动态视图之时序图

    一:时序图 --->时序图是用于描述按时间顺序排列的对象之间的交互模式. --->它按照参与交互的对象所具有的“生命线”和他们相互发送的消息来显示这些对象. --->时序图包含对象和 ...

  4. hdu 4609 3-idiots —— FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 算不合法的比较方便: 枚举最大的边,每种情况算了2次,而全排列算了6次,所以还要乘3: 注意枚举最大 ...

  5. TCP/IP 详解卷一之 HTTP协议

    HTTP协议 简介 超文本传输协议(HypertextTransfer Protocol,简称HTTP)是应用层协议.HTTP 是一种请求/响应式的协议,即一个客户端与服务器建立连接后,向服务器发送一 ...

  6. POJ(有向图求LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 18013   Accept ...

  7. zk 09之:Curator之二:Path Cache监控zookeeper的node和path的状态

    在实际应用开发中,当某个ZNode发生变化后我们需要得到通知并做一些后续处理,Curator Recipes提供了Path Cache 来帮助我们轻松实现watch ZNode. Path Cache ...

  8. Entity Framework之领域驱动设计实践

    http://www.cnblogs.com/daxnet/archive/2010/11/02/1867392.html

  9. pysam操作sam文件

    pysam模块 因为要分析sam文件中序列的情况,因此要对reads进行细分,所以之前想用数据库将sam文件信息存储,然后用sql语句进行分类.后来发现很麻烦,pysam就是一个高效读取存储在SAM ...

  10. 如何解决WAMP Server 与IIS端口冲突问题

    PHP也是一门开发网页的语言,WAMP Server 是它的一个较好的集成开发环境,今日,小编发现好多Wamp Server 安装后启动local host 出现的却是IIS页面!为什么会这样呢? 出 ...