hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

时间:2021-07-07 10:40:15

hdu4135

求[L,R]范围内与N互质的数的个数。

分别求[1,L]和[1,R]和n互质的个数,求差。

利用容斥原理求解。

二进制枚举每一种质数的组合,奇加偶减。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int N = ; int fac[N], cnt; void factor(int n) {
cnt = ;
int limit = sqrt(n); for (int i = ; i <= limit; ++i) {
if (n % i == ) fac[cnt++] = i;
while (n % i == ) n /= i;
}
if (n > ) fac[cnt++] = n;
} ll solve(ll x, int n) { //1~x中与n互质的数
ll ans = ;
ll val;
int num;
//printf("cnt=%d\n", cnt); int st = <<cnt;
for (int i = ; i < st; ++i) {// 枚举所有情况,奇加偶减
val = ;
num = ;
for (int j = ; j < cnt; ++j) {
if (i & (<<j)) {
val *= fac[j];
num++;
}
}
//printf(">>%d %lld %d\n", i, val, num);
if (num & ) ans += x/val;
else ans -= x/val;
}
return x - ans;
} int main()
{
ll x, y;
int n;
int t, cas=; cin >> t;
while (t--) {
cin >> x >> y >> n;
cout << "Case #" << ++cas << ": ";
factor(n);
cout << solve(y,n) - solve(x-,n) << endl;
} return ;
}

Codeforces 547C Mike and Foam

差不多啦

求新加入的数和已经存在的数有多少是不互质的。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath> using namespace std; const int N = ;
int a[N], vis[N], fac[N], tfac[N];
int n, m, q, tot; void getfactor(int x, int fac[], int &cnt) {
int limit = sqrt(x);
cnt = ;
for (int i = ; i <= limit; ++i) {
if (x % i == ) {
fac[cnt++] = i;
while (x % i == ) x /= i;
}
}
if (x > ) fac[cnt++] = x;
} int add(int x, int d) {
int cnt;
getfactor(x, tfac, cnt);
int st = <<cnt;
int ans = ;
for (int i = ; i < st; ++i) {
int num = ;
int tmp = ;
for (int j = ; j < cnt; ++j) {
if (i & (<<j)) {
num++;
tmp *= tfac[j];
}
}
if (d == -) fac[tmp]--;
if (num & ) ans += fac[tmp];
else ans -= fac[tmp];
if (d == ) fac[tmp]++;
}
if (d == -) return -tot+ans;
else return tot--ans;
} int main()
{
while (~scanf("%d%d", &n, &m)) {
memset(vis, , sizeof vis);
tot = ;
for (int i = ; i <= n; ++i) scanf("%d", &a[i]);
long long ans = ,tmp;
while (m--) {
scanf("%d", &q);
if (vis[q]) vis[q] = , tot--, tmp = add(a[q], -);
else vis[q] = , tot++, tmp = add(a[q], );
ans += tmp;
printf("%lld\n", ans);
}
}
return ;
}