码迷,mamicode.com
首页 > 其他好文 > 详细

hdu_3502_Calculation 2

时间:2017-11-15 21:50:41      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:not   int   ane   str   lld   which   欧拉   test   函数   

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.

InputFor 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.OutputFor each test case, you should print the sum module 1000000007 in a line.Sample Input

3
4
0

Sample Output

0
2

如果gcd(n,i)==1,gcd(n,n-i)==1
证:i=1(mod n)
  -i=-1(mod n)
  n-i=-1+n (mod n)
  n-i=1 (mod n)
通过欧拉函数一个数n中存在phi(n)个与之互质的数,因为i+(n-i)为n也就是有n对。
则与n互质数目之和为res=phi(n)/2*n,
ans=(n-1)*n/2-res。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define N 1000010
#define maxn 1000010
#define mod 1000000007
using namespace std;
typedef long long ll;
int p[N];
int prime[N];
int pn=0;
bool vis[N];
int main()
{
    for (int i = 2; i < N; i++) {
        if (vis[i]) continue;
        prime[pn++] = i;
        for (int j = i; j < N; j += i)
            vis[j] = 1;
    }
    ll n;
    while(~scanf("%lld",&n),n)
    {
        ll r=n;
        ll phi=n;
        for(int i=0;prime[i]*prime[i]<=r;i++)
        {
            ll tem=0;
            while(r%prime[i]==0)
            {
                r/=prime[i];
                tem++;
            }
            if(tem)
               phi=phi-phi/prime[i];
        }
        if(r!=1)
            phi-=phi/r;
        ll rem=n*phi/2;
        ll ans=n*(n-1)/2;
        printf("%lld\n",(ans-rem)%mod);
    }
}

  

hdu_3502_Calculation 2

标签:not   int   ane   str   lld   which   欧拉   test   函数   

原文地址:http://www.cnblogs.com/ygtzds/p/7840569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!