# [UOJ#221][BZOJ4652][Noi2016]循环之美

[UOJ#221][BZOJ4652][Noi2016]循环之美

2 6 10

4

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

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 20000001
#define maxk 2010
#define LL long long

bool vis[maxn];
int cp, prime[maxn], mu[maxn];
void init() {
mu[1] = 1;
for(int i = 2; i < maxn; i++) {
if(!vis[i]) prime[++cp] = i, mu[i] = -1;
for(int j = 1; i * prime[j] < maxn && j <= cp; j++) {
vis[i*prime[j]] = 1;
if(i % prime[j] == 0){ mu[i*prime[j]] = 0; break; }
mu[i*prime[j]] = -mu[i];
}
}
return ;
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int f[maxk];
int calc(int n, int k) {
return n / k * f[k] + f[n%k];
}

int main() {
init();

for(int i = 1; i <= k; i++) f[i] = f[i-1] + (gcd(i, k) == 1);

LL sum = 0;
for(int d = 1; d <= n; d++) if(gcd(d, k) == 1) sum += (LL)mu[d] * (n / d) * calc(m / d, k);
printf("%lld\n", sum);

return 0;
}


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

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 1000001
#define maxk 2010
#define MOD 1000007
#define LL long long
#define oo 2147483647

bool vis[maxn];
int cp, prime[maxn], mu[maxn], smu[maxn];
void init() {
mu[1] = 1; smu[1] = 1;
for(int i = 2; i < maxn; i++) {
if(!vis[i]) prime[++cp] = i, mu[i] = -1;
for(int j = 1; i * prime[j] < maxn && j <= cp; j++) {
vis[i*prime[j]] = 1;
if(i % prime[j] == 0){ mu[i*prime[j]] = 0; break; }
mu[i*prime[j]] = -mu[i];
}
smu[i] = smu[i-1] + mu[i];
}
return ;
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int f[maxk];
int calc(int n, int k) {
return n / k * f[k] + f[n%k];
}

struct Hash {
int ToT, head[MOD], nxt[maxn], num[maxn], num2[maxn], val[maxn];
void Insert(int x, int v) {
int u = x % MOD;
nxt[++ToT] = head[u]; num[ToT] = x; val[ToT] = v; head[u] = ToT;
return ;
}
void Insert2(int x1, int x2, int v) {
int u = ((LL)x1 * 233 + x2) % MOD;
nxt[++ToT] = head[u]; num[ToT] = x1; num2[ToT] = x2; val[ToT] = v; head[u] = ToT;
return ;
}
int Find(int x) {
int u = x % MOD;
for(int e = head[u]; e; e = nxt[e]) if(num[e] == x) return val[e];
return 0;
}
int Find2(int x1, int x2) {
int u = ((LL)x1 * 233 + x2) % MOD;
for(int e = head[u]; e; e = nxt[e]) if(num[e] == x1 && num2[e] == x2) return val[e];
return oo;
}
} hh, hh2;

int getsum(int n) {
if(n < maxn) return smu[n];
if(hh.Find(n)) return hh.Find(n);
int sum = 1;
for(int i = 2, lst; i <= n; i = lst + 1) {
lst = n / (n / i);
sum -= getsum(n / i) * (lst - i + 1);
}
hh.Insert(n, sum);
return sum;
}

int fir_p[maxk], lst_q[maxk];
int Find(int n, int k) {
if(!n) return 0;
if(k == 1) return getsum(n);
if(hh2.Find2(n, k) < oo) return hh2.Find2(n, k);
int p = fir_p[k], q = lst_q[k];
int tmp = Find(n, q) + Find(n / p, p * q);
hh2.Insert2(n, k, tmp);
return tmp;
}

int main() {
init();

for(int i = 1; i <= k; i++) f[i] = f[i-1] + (gcd(i, k) == 1);

for(int K = 2; K <= k; K++)
for(int i = 1; i <= cp; i++) if(K % prime[i] == 0) {
fir_p[K] = prime[i];
lst_q[K] = K; while(lst_q[K] % fir_p[K] == 0) lst_q[K] /= fir_p[K];
break;
}
LL sum = 0;
for(int i = 1, lst; i <= min(n, m); i = lst + 1) {
lst = min(n / (n / i), m / (m / i));
sum += (LL)(Find(lst, k) - Find(i - 1, k)) * (n / i) * calc(m / i, k);
}
printf("%lld\n", sum);

return 0;
}


posted @ 2017-06-28 20:02  xjr01  阅读(280)  评论(0编辑  收藏  举报