hdu 3501 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 <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <vector> 6 #include <set> 7 using namespace std; 8 typedef long long LL; 9 const int maxn = 1e6+5; 10 const LL mod = 1000000007; 11 LL n; 12 LL get_Euler(LL x){ 13 LL res = x; ///初始值 14 for(LL i = 2LL; i * i <= x; ++i) { 15 if(x % i == 0) { 16 res = res / i * (i - 1); ///先除后乘,避免数据过大 17 while(x % i == 0) x /= i; 18 } 19 } 20 if(x > 1LL) res = res / x * (x - 1); ///若x大于1,则剩下的x必为素因子 21 return res; 22 } 23 24 int main(){ 25 while(cin >> n && n) { 26 cout << (n * (n - 1) / 2 - n * get_Euler(n) / 2) % mod << endl; 27 } 28 return 0; 29 }