W - Calculation 2 (欧拉函数的扩展)

Posted acgoto

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }



以上是关于W - Calculation 2 (欧拉函数的扩展)的主要内容,如果未能解决你的问题,请参考以下文章

HDU3501 Calculation 2(欧拉函数)

hdu_2837_Calculation(欧拉函数,快速幂求指数循环节) (待查

HDU 3501 Calculation 2

C - Calculation 2 HDU - 3501 (欧拉)

HDU2837 Calculation(指数循环节)题解

最大公约数之和欧拉函数