HDU 3501 Calculation 2(欧拉函数)

Calculation 2
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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,求小于n,并且不与n互质的数的和模1000000007的值。
 
解题思路:
欧拉函数,先求出小于n且与n互质的数的和,用总数减去互质的数的和,就得到了不互质的数的和。
 
代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int mod=1000000007;
 6 int main()
 7 {
 8     int n;
 9     while(scanf("%d",&n)!=EOF&&n)
10     {
11         long long temp=n,eular=1;
12         long long ans=(temp*(temp+1)/2)%mod;
13         for(int i=2;i*i<=temp;i++)
14             if(temp%i==0)
15             {
16                 temp/=i;
17                 eular*=i-1;
18                 while(temp%i==0)
19                 {
20                     temp/=i;
21                     eular*=i;
22                 }
23             }
24         if(temp>1) eular*=temp-1;
25         eular%=mod;
26         temp=n;
27         long long w=(eular*temp/2)%mod;
28         ans-=w;
29         ans-=n;
30         while(ans<0) ans+=mod;
31         printf("%lld
",ans);
32     }
33     return 0;
34 }
Calculation 2
原文地址:https://www.cnblogs.com/I-love-HLD/p/4624723.html