GCD - Extreme (II) UVA - 11426
Given the value of N, you will have to find the value of G. The definition of G is given below:
G =
GCD(i,j)
Input
The input file contains at most 100 lines of inputs. Each line contains an integer N (1 < N < 4000001).
The meaning of N is given in the problem statement. Input is terminated by a line containing a single
zero.
Output
For each line of input produce one line of output. This line contains the value of G for the correspondingN. The value of G will fit in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160
题意:
给定N,求所给公式的值
分析:
由给出到公式,我们设
同理
.
.
.
所以公式求和
所以我们到任务变成求
为了便于说明我们再设一个变量叫
满足
到个数,其中x是[1,n]的数
,问题再次简化,设
,我们只要找到i以内和i互质到个数即可,这样我们枚举倍数j只要i×j < n 即可,而这个倍数就是上面到最大公因数。
这样我们就相当于知道了和n公因数为j到个数一乘便是和即
求一个数以内互质到个数用欧拉函数即可
code:
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6+10;
typedef long long ll;
int phi[N];
ll f[N];
void getphi(){
for(int i = 1; i < N; i++) phi[i] = i;
for(int i = 2; i < N; i++){
if(phi[i] == i){
for(int j = i; j < N; j += i){
phi[j] = phi[j] / i * (i - 1);
}
}
for(int j = 1; j * i < N; j++){
f[j*i] += j * phi[i];
}
}
}
int main(){
getphi();
int n;
while(cin >> n && n){
ll ans = 0;
for(int i = 1; i <= n; i++){
ans += f[i];
}
printf("%lld\n",ans);
}
return 0;
}