HDU 3501 Calculation 2

 
   

Calculation 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1296    Accepted Submission(s): 544


Problem 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
 
 
   
Author
GTmac
 
 
   
Source
 
 
   
Recommend
zhouzeyong
N以内与N互质的数和 的扩展
N以内与N互质的数和 =》phi[N]*N/2 
而本题是求与N不互质的和,就很简单
#include <iostream> #include <map> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; #define N 32000 bool h[N]; int p[4000]; void prime()//帅选求素数 { int cnt=1; int i,j; p[0]=2; for(i=2;i<N;i+=2) h[i]=1; for(i=3;i<N;i+=2) if(!h[i]) { p[cnt++]=i; for(j=i*i;j<N;j+=i) h[j]=1; } } __int64 solve(__int64 n) { int m=(int)sqrt(n*1.0); __int64 r=n,tp=n; int i; for(i=0;p[i]<=m;i++) if(n%p[i]==0) { tp=tp/p[i]*(p[i]-1); while(n%p[i]==0) n/=p[i]; if(n==1) break; } if(n>1) tp=tp/n*(n-1); return (r*tp)>>1; } int main() { prime(); __int64 n,sum; while(scanf("%I64d",&n),n) { sum=((n-1)*n)>>1; sum-=solve(n); printf("%I64d\n",sum%1000000007); } return 0; }

你可能感兴趣的:(HDU)