思路:

  因为当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)

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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<set>
  5. #include<vector>
  6. #include<map>
  7. #include<string>
  8. #include<iomanip>
  9. #include<iostream>
  10. #include<cmath>
  11. #include<queue>
  12. using namespace std;
  13. #define ALL(x,S) for(x=S.begin();x!=S.end();x++)
  14. typedef long long LL;
  15. typedef long double real;
  16. bool pd_prime[];
  17. set<LL> S;
  18. set<LL>::iterator pos;
  19. LL n;
  20. void prepare()
  21. {
  22. int i,j;
  23. memset(pd_prime,,sizeof pd_prime);
  24. pd_prime[]=;
  25. for(int i=;i<=;i++)
  26. if(pd_prime[i])
  27. for(int j=;j<=/i;j++)
  28. pd_prime[i*j]=;
  29. }
  30. void addp(LL t)
  31. { S.insert(t);}
  32. LL gcd(LL a,LL b)
  33. {
  34. if(!b) return a;
  35. return gcd(b,a%b);
  36. }
  37. LL mul(LL a,LL b,LL mod)
  38. {
  39. LL ans=;
  40. while(b)
  41. {
  42. if(b&) ans=(ans+a)%mod;
  43. a=(a+a)%mod;
  44. b>>=;
  45. }
  46. return ans;
  47. }
  48. LL pow(LL a,LL b,LL mod)
  49. {
  50. LL ans=;
  51. while(b)
  52. {
  53. if(b&) ans=mul(ans,a,mod);
  54. b>>=;
  55. a=mul(a,a,mod);
  56. }
  57. return ans;
  58. }
  59. bool MR(LL n,LL k)
  60. {
  61. LL s=n-,w=;
  62. while(!(s&))
  63. w++,s>>=;
  64. LL y=pow(k,s,n);
  65. if(y== || y==n-)
  66. return ;
  67. while(w--)
  68. {
  69. y=mul(y,y,n);
  70. if(y==n-)
  71. return ;
  72. }
  73. return ;
  74. }
  75. bool prime(LL n)
  76. {
  77. if(n<=)
  78. return pd_prime[n];
  79. bool flag=;
  80. for(int i=;i<=;i++)
  81. if(pd_prime[i])
  82. if(!MR(n,i)) flag=;
  83. return flag;
  84. }
  85. void rho(LL n)
  86. {
  87. if(n==) return ;
  88. if(n==)
  89. {
  90. addp();
  91. addp();
  92. return ;
  93. }
  94. if(prime(n))
  95. {
  96. addp(n);
  97. return ;
  98. }
  99. LL x,y,d,p;
  100. while()
  101. {
  102. x=,y=,d=;
  103. p=mul(rand(),rand(),);
  104. if(d==)
  105. {
  106. x=(mul(x,x,n)+p)%n;
  107. y=(mul(y,y,n)+p)%n;
  108. y=(mul(y,y,n)+p)%n;
  109. d=gcd(abs(x-y),n);
  110. }
  111. if(d==n) continue;
  112. rho(d);rho(n/d);
  113. return ;
  114. }
  115. }
  116. LL phi(LL x)
  117. {
  118. S.clear();
  119. rho(x);
  120. LL ans=x;
  121. ALL(pos,S)
  122. ans=ans/(*pos)*((*pos)-);
  123. return ans;
  124. }
  125. int main()
  126. {
  127. freopen("phi.in","r",stdin);
  128. freopen("phi.out","w",stdout);
  129. prepare();
  130. scanf("%lld",&n);
  131. cout<<phi(n);
  132. return ;
  133. }

求一个极大数的欧拉函数 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程序打包工具 ── cx_Freeze

    cx_Freeze是一个类似py2exe的工具,它们区别是py2exe是将python程序打包成windows下可以执行的exe文件的,而cx_Freeze则是将python程序打包为linux下可以 ...

  2. bzoj2673

    限制这么多 肯定是网络流 考虑连边 首先我们计算出每行最多放的棋子数$sx[i]$,每列最多放的棋子数$sy[i]$ 首先由源点向第$i$行连流量为$sx[i]$费用为$0$的边,第$i$列向汇点连流 ...

  3. BZOJ-3626:LCA(离线+树链剖分)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q ...

  4. CodeForces - 123E Maze

    http://codeforces.com/problemset/problem/123/E 题目翻译:(翻译来自: http://www.cogs.pw/cogs/problem/problem.p ...

  5. 【Lintcode】135.Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  6. 【Lintcode】098.Sort List

    题目: Sort a linked list in O(n log n) time using constant space complexity. Example Given 1->3-> ...

  7. CUDA 9.1/9.2 与 Visual Studio 2017 (VS2017 15.6.4) 的不兼容问题

    2018年7月9日更新: CUDA已推出9.2版本,最高支持MSVC++ 14.13 _MSC_VER == 1913 (Visual Studio 2017 version 15.6). 然而最新版 ...

  8. MVVM里绑定TreeView控件的SelectedItem

    <TreeView x:Name="treeView"> <i:Interaction.Triggers> <i:EventTrigger Event ...

  9. socket网络编程实践要点

    1.创建udp的socket句柄 // 当host_port为0时,则表示让操作系统自动分配 bool createUdpSocket(string host_ip,unsigned short ho ...

  10. 洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片

    P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a ...