W - 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内与n互质的所有数之和,公式:n*(n-1)/2-n*Euler(n)/2。
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int mod=1000000007;
 5 int Euler(int x){
 6     int r=x;
 7     for(int i=2;i*i<=x;i++){
 8         if(x%i==0){
 9             r=r/i*(i-1);
10             while(x%i==0)x/=i;
11         }
12     }
13     if(x>1)r=r/x*(x-1);
14     return r;
15 }
16 int main(){
17     int n;
18     while(cin>>n&&n){
19         LL sum=(LL)n*(n-1)/2-(LL)n*Euler(n)/2;
20         cout<<sum%mod<<endl;
21     }
22     return 0;
23 }

猜你喜欢

转载自www.cnblogs.com/acgoto/p/9424726.html
w2