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, there is a line containing a positive integer N(1 ≤ N ≤ 1000000000). A line containing a single 0 follows the last test case.

Output

For each test case, you should print the sum module 1000000007 in a line.

Sample Input

3 4 0

Sample Output

0 2

运用欧拉函数求与n互质的数的总和:phi[n]*n/2;

证明:在1<x<n中,与n互质的数有{x1,x2,x3....,xn},gcd(x,n)=gcd(n-x,n)=1,得:
{n-xn,....n-x2,n-x1},两个集合两两对应,相加除以2得phi[n]*n/2;

代码如下:

#include <stdio.h>
#include<math.h>
#include <string.h>

long long phi(long long n)
{
    long long res=n;
    for(int i=2; i*i<=n; i++)
    {
        if(n%i==0)
            {
                res=res-res/i;
                do
                    n/=i;
                while(n%i==0);
            }
    }
    if(n>1)
        res=res-res/n;
    return res;
}
int main()
{
    long long n,ans;
    while(scanf("%I64d",&n) != EOF)
    {
        if(n==0)
            break;
        ans=n*(n+1)/2-n;
        ans-=phi(n)*n/2;
        ans=ans%1000000007;
        printf("%I64d\n",ans);

    }

    return 0;
}
时间: 2024-12-17 21:44:55

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

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

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 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 欧拉函数

//求小于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

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

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