# [BZOJ2820]YY的GCD

[BZOJ2820]YY的GCD

T行，每行一个整数表示第i组数据的结果

2
10 10
100 100

30
2791

T = 10000
N, M <= 10000000

……式1

……式2

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int BufferSize = 1 << 16;
inline char Getchar() {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
}
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
}

#define maxn 10000010
#define LL long long
int T, n, m;

int prime[maxn], cnt, u[maxn], g[maxn];
LL sum[maxn];
bool vis[maxn];
void u_table() {
int N = maxn - 10;
u[1] = 1; g[1] = 0;
for(int i = 2; i <= N; i++) {
if(!vis[i]) prime[++cnt] = i, u[i] = -1, g[i] = 1;
for(int j = 1; j <= cnt && (LL)prime[j] * (LL)i <= (LL)N; j++)
if(i % prime[j]) vis[i*prime[j]] = 1, u[i*prime[j]] = -u[i], g[i*prime[j]] = u[i] - g[i];
else{ vis[i*prime[j]] = 1, u[i*prime[j]] = 0, g[i*prime[j]] = u[i]; break; }
}
for(int i = 1; i <= N; i++) sum[i] = sum[i-1] + (LL)g[i];
return ;
}

int main() {
u_table();

while(T--) {
if(n > m) swap(n, m);
int p = 1;
LL ans = 0;
for(; p <= n;) {
int np = p;
p = min(n / (n / np), m / (m / np));
ans += (sum[p] - sum[np-1]) * (LL)(n / np) * (LL)(m / np);
p++;
}
printf("%lld\n", ans);
}

return 0;
}


posted @ 2016-08-21 18:52 xjr01 阅读(...) 评论(...) 编辑 收藏