HDU3501 Calculation 2(欧拉函数)

题目求小于n不与n互质的正整数的和。

一个结论是小于n与n互质的正整数和=φ(n)*n/2。

  • 因为如果a与n互质,那么n-a也与n互质,即若gcd(a,n)=1则gcd(n-a,n)=1,反证法即可证明。
  • 也就是说小于n与n互质的数是成对的,且它们的和是n,共有φ(n)/2对。
  • 所以小于n与n互质的正整数和=φ(n)*n/2。
 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int phi(int n){
 5     int res=n;
 6     for(int i=2; i*i<=n; ++i){
 7         if(n%i) continue;
 8         while(n%i==0) n/=i;
 9         res-=res/i;
10     }
11     if(n!=1) res-=res/n;
12     return res;
13 }
14 int main(){
15     int n;
16     while(~scanf("%d",&n) && n){
17         printf("%lld\n",((long long)n*(n-1)/2-(long long)n*phi(n)/2)%1000000007);
18     }
19     return 0;
20 }
时间: 2024-10-27 08:05:23

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

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

hdu3501 Calculation 2 欧拉函数

//求小于n且和n不互质的所有数之和 //若gcd(n , i) == 1 那么 gcd(n , n-i) == 1 //可以用反证法 //设gcd(n , n-i) != 1; //那么可以有 n = k1*a //n - i = k2*a ; //i = (k1-k2)*a //gcd(n ,i) != 1 //那么 ans = n*(n-1)/2 - n*euler(n)/2 #include<cstdio> #include<cstring> #include<ios

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=0k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现; 所以先求出1--n-1 的和, 再用这个和 减去 上面公式求出来的值. 欧拉函数phi(m)

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 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

Calculation 2(欧拉函数的运用)

Description Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is said to be coprime to B if A, B share no common positive divisors except 1. Input For each test case, ther

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

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

HDU3501 Calculation 2 【欧拉函数】

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

HDOJ 3501 Calculation 2(欧拉函数拓展——求非互质数和)

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