GCD - Extreme (II) UVA - 11426 数学
Given the value of
N
, you will have to nd the value of
G
. The de nition of
G
is given below:
G
=
i<N
∑
i
=1
j
N
∑
j
=
i
+1
GCD
(
i; j
)
Here
GCD
(
i; j
) means the greatest common divisor of integer
i
and integer
j
.
For those who have trouble understanding summation notation, the meaning of
G
is given in the
following code:
G=0;
for(i=1;i<N;i++)
for(j=i+1;j<=N;j++)
{
G+=gcd(i,j);
}
/*Here gcd() is a function that finds
the greatest common divisor of the two
input numbers*/
Input
The input le 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 corresponding
N
. The value of
G
will t in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160
设 f(n)=gcd(1,n)+gcd(2,n)+...+gcd(n-1,n),则
s(n)=f(2)+f(3)+...+f(n);
对于 f(n)=gcd(1,n)+...+gcd(n-1,n);
设 g(n,i)= { gcd(x,n)=i 的个数 },则 f(n)=Sum{ i*g(n,i) };
gcd(x,n)=i -->gcd(x/i,n/i)=1;
那么满足条件的x/i有 phi(n/i)个;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 4000005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-4 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ int phi[maxn]; void init() { for (int i = 2; i <= maxn; i++)phi[i] = 0; phi[1] = 1; for (int i = 2; i <= maxn; i++) { if (!phi[i]) { for (int j = i; j <= maxn; j += i) { if (!phi[j])phi[j] = j; phi[j] = phi[j] / i * (i - 1); } } } } ll sum[maxn], f[maxn]; int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); init(); for (int i = 1; i <= maxn; i++) { for (int j = i * 2; j <= maxn; j += i)f[j] += i * phi[j / i]; } sum[2] = f[2]; for (int j = 3; j <= maxn; j++)sum[j] = sum[j - 1] + f[j]; int n; while (rdint(n) == 1&&n) { printf("%lld\n", sum[n]); } return 0; }
EPFL - Fighting