Application Euler function (hdu 3501)

Original title link 

http://acm.hdu.edu.cn/showproblem.php?pid=3501


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
题意分析:

To an integer number n, and n is less than the demand and n is not prime, and

 

Euler function [Phi] (n) value is less than n and prime with n the number of counts (end of the text), then think of a nature, if gcd gcd (n, i) = 1, then the gcd (n, Ni) = 1, we can see the quality and number of paired cross-n, i.e., the value is even Euler function ([Phi], but (1) = 1), and each pair and are to n.


Such problem-solving ideas came out with a 1-n and subtract n * φ (n) / 2, is the answer

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int maxn=1e5;
const int mod=1e9+7;
ll a[maxn];
//o(sqrt(n))求欧拉函数的值
int Phi(int n){
int m=(int)sqrt(n+0.5);
int ans=n;
for(int i=2;i<=m;i++){
if(n%i==0){
ans=ans/i*(i-1);
while(n%i==0) n/=i;
}
}
if(n>1) ans=ans/n*(n-1);
return ans;
}
int main()
{
ll n;
while(scanf("%lld",&n)!=-1&&n)
{
ll ans=(n*(n-1)/2)%mod;
ans-=(n*Phi(n)/2)%mod;
cout<<(ans+mod)%mod<<endl;
}
//printf("%lld\n",ans );
return 0;
}

 

Euler function φ: refers to the number of prime numbers, in particular the definition of φ (1) = 1 from Equation 1 given below it to n-1 and n

 

 

Wherein pi is the prime factors of n.

Watch my original micro-channel public number: small daily learning algorithm
to see more original articles, learn together and progress!

 

 

Guess you like

Origin www.cnblogs.com/zzl-dreamfly/p/11804435.html