D.
题意:给定一个整数n,并且给定n在二进制下1的个数,最多30次操作,你可以将n减去一个x(x<=n),然后得到当前n在二进制下1的个数.操作完成之后回答初始的n值.
考虑当前n的lowbit值,1000..0-1=0111..1,1的变化量为cnt,则变化前的lowbit值为2 ^ (cnt + 1),累加至答案中,因为该次操作不会影响cnt+1及以后的位置,我们可以用类似的方法得到下一个lowbit值,最后可以得到n.
E.
题意:给定l,r,求出[l,r]两两的gcd有多少种.
设gcd=t,如果能产生t,说明⌊r/t⌋-⌊(l-1)/t⌋>=2,t最大为r/2.注意到⌊(l-1)/t⌋的取值最多为√1e9=1e4.5个,所以用整除分块进行枚举即可.
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
int L, R;
void solve() {
cin >> L >> R;
--L;
int ans = 0;
for (int l = 1, r; l <= R / 2; l = r + 1) {
if (L / l == 0) r = R / 2;
else r = L / (L / l);
int t = R / ((L / l) + 2);
if (t < l) {
continue;
}
int _l = min(l, t), _r = min(r, t);
ans += max(0ll, _r - _l + 1);
}
cout << ans << "\n";
}
signed main() {
IOS;
int T;
cin >> T;
while (T--) solve();
}
F.
题意:给定n个数a[1~n],求出三元组(i,j,k)的个数,满足gcd(min(a[i],a[j],a[k]),max(a[i],a[j],a[k])=1.
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
void solve() {
}
const int N = 5e5 + 10;
int n, a[N], v[N], c[N];
vector<int> fac[N];
int cnt, mu[N], vis[N], primes[N];
void get_mu(int n) {
cnt = 0; mu[1] = 1;
for (int i = 2; i <= n; ++i) {
if (vis[i] == 0) {
primes[++cnt] = i;
mu[i] = -1;
}
for (int j = 1; j <= cnt && i * primes[j] <= n; ++j) {
vis[i * primes[j]] = 1;
if (i % primes[j] == 0) {
break;
}
mu[i * primes[j]] = -mu[i];
}
}
}
signed main() {
IOS;
get_mu(N - 10);
cin >> n;
int mx = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
mx = max(mx, a[i]);
}
for (int i = 1; i <= mx; ++i) {
for (int j = i; j <= mx; j += i) {
fac[j].emplace_back(i);
}
}
sort(a + 1, a + 1 + n);
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 0, y = 0;
for (int d : fac[a[i]]) {
x += mu[d] * c[d], y += mu[d] * v[d];
}
ans += x * (i - 1) - y;
for (int d : fac[a[i]]) {
++c[d], v[d] += i;
}
}
cout << ans << "\n";
}