hdu 3501 Calculation 2 (欧拉函数)

题目

题意:求小于n并且 和n不互质的数的总和。

思路:求小于n并且与n互质的数的和为:n*phi[n]/2 .

若a和n互质,n-a必定也和n互质(a<n)。也就是说num必定为偶数。其中互质的数成对存在。其和为n。

公式证明:

反证法:
如果存在K!=1使gcd(n,n-i)=k,那么(n-i)%k==0
而n%k=0
那么必须保证i%k=0
k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现;

所以先求出1……n-1 的和, 再用这个和 减去 上面公式求出来的值。

欧拉函数phi(m):当m>1是,phi(m)表示比m小且与m互质的正整数个数


 1 #include <iostream>
2 #include <cstdio>
3 #include <cmath>
4 #include <cstdlib>
5 #include <cstring>
6 #include <algorithm>
7 using namespace std;
8 #define LL long long
9 const int mo = 1000000007;
10
11 LL phi(LL n) //欧拉函数模板
12 {
13 LL i, m = (int)sqrt(n+0.5), ans = n;
14 for(i = 2; i <= m; i++)
15 {
16 if(n%i == 0)
17 ans = ans/i*(i-1);
18 while(n%i == 0)
19 n /= i;
20 }
21 if(n > 1) ans = ans/n*(n-1);
22 return ans;
23 }
24 int main()
25 {
26 LL res, n, i;
27 while(~scanf("%lld", &n) && n)
28 {
29 res = n*(n-1)/2;
30 res -= phi(n)*n/2; //当n等于2的时候,phi为1,所以不能写成 phi(n)/2*n;
31 printf("%lld\n", res%mo);
32 }
33 return 0;
34 }

再贴一个关于欧拉函数的讲解博客

hdu 3501 Calculation 2 (欧拉函数),布布扣,bubuko.com

时间: 2024-08-02 02:48:42

hdu 3501 Calculation 2 (欧拉函数)的相关文章

HDU 3501 Calculation 2(欧拉函数的应用)

HDU 3501 Calculation 2 大意:求1~n之间与n不互质的数的总和. 思路:欧拉函数的应用:先用欧拉函数求出与n互质的总数m,计算m个数的总和,用n的总和减去m的总和就是想要的结果. 1 #include <stdio.h> 2 #define LL __int64 3 4 int eular(int n){ 5 int ret = 1; 6 for(int i = 2; i*i <= n;i++) 7 if(n%i == 0){ 8 n /= i, ret *= i-

HDU 3501 Calculation 2 (欧拉函数应用)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2989    Accepted Submission(s): 1234 Problem Description Given a positive integer N, your task is to calculate the sum of the posit

hdu 3501 容斥原理或欧拉函数

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2181    Accepted Submission(s): 920 Problem Description Given a positive integer N, your task is to calculate the sum of the positiv

hdu 2837 Calculation【欧拉函数,快速幂求指数循环节】

欢迎关注__Xiong的博客: http://blog.csdn.net/acmore_xiong?viewmode=list Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1912    Accepted Submission(s): 413 链接:click me Problem Description A

hdoj 3501 -Calculation 2 (欧拉函数)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3305    Accepted Submission(s): 1367 Problem Description Given a positive integer N, your task is to calculate the sum of the positi

hdu 2814 快速求欧拉函数

1 /** 2 大意: 求[a,b] 之间 phi(a) + phi(a+1)...+ phi(b): 3 思路: 快速求欧拉函数 4 **/ 5 6 #include <iostream> 7 #include <cstring> 8 using namespace std; 9 #define Max 3000000 10 11 long long phi[Max+5]; 12 int prime[Max/10]; 13 bool flag[Max+5]; 14 15 void

HDU3501 Calculation 2(欧拉函数推广)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3501 题意:求小于n的与n不互质的数的和: 分析: 欧拉函数的推广: 小于n的与n互质的数为phi(n),小于n的与n互质的数的和为phi(n)*n/2; 代码如下: #include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long LL; cons

HDU 5430 Reflect(欧拉函数)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=5430 从镜面材质的圆上一点发出一道光线反射NNN次后首次回到起点. 问本质不同的发射的方案数. 输入描述 第一行一个整数T,表示数据组数.T≤10T \leq 10T≤10 对于每一个组,共一行,包含一个整数,表示正整数N(1≤N≤106)N(1 \leq N \leq 10^{6})N(1≤N≤10?6??). 输出描述 对于每一个组,输出共一行,包含一个整数,表示答案. 输入样例 1 4 输出样例

HDU 1695 GCD(欧拉函数+容斥原理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, y)有多少组,不考虑顺序. 思路:a = c = 1简化了问题,原问题可以转化为在[1, b/k]和[1, d/k]这两个区间各取一个数,组成的数对是互质的数量,不考虑顺序.我们让d > b,我们枚举区间[1, d/k]的数i作为二元组的第二位,因为不考虑顺序我们考虑第一位的值时,只用考虑小于i的情