結果

問題 No.2849 Birthday Donuts
ユーザー ecottea
提出日時 2024-06-11 20:42:21
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 18,499 bytes
コンパイル時間 25,289 ms
コンパイル使用メモリ 358,544 KB
最終ジャッジ日時 2025-02-21 21:10:07
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 1
other AC * 1 WA * 1 RE * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

// QCFium
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#ifndef HIDDEN_IN_VS //
//
#define _CRT_SECURE_NO_WARNINGS
//
#include <bits/stdc++.h>
using namespace std;
//
using ll = long long; using ull = unsigned long long; // -2^63 2^63 = 9 * 10^18int -2^31 2^31 = 2 * 10^9
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
//
const double PI = acos(-1);
int DX[4] = {1, 0, -1, 0}; // 4
int DY[4] = {0, 1, 0, -1};
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
//
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
//
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 n-1
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s t
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s t
#define repe(v, a) for(const auto& v : (a)) // a
#define repea(v, a) for(auto& v : (a)) // a
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d
#define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} //
#define EXIT(a) {cout << (a) << endl; exit(0);} //
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) //
//
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // true
    
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // true
    
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // mod
//
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif //
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // Visual Studio
#include "local.hpp"
#else // gcc
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE MLE
#endif
//
/*
* Div_mul_transform<T>(int n) : O(n log(log n))
* n
*
* divisor_zeta(vT& a) : O(n log(log n))
* A[i] = Σ_(j | i) a[j] A
*
* divisor_mobius(vT& A) : O(n log(log n))
* A[i] = Σ_(j | i) a[j] a
*
* vT lcm_convolution(vT a, vT b) : O(n log(log n))
* c[k] = Σ_(LCM(i, j) = k) a[i] b[j] c
* c[n]
*
* multiple_zeta(vT& a) : O(n log(log n))
* A[i] = Σ_(i | j) a[j] A
*
* multiple_mobius(vT& A) : O(n log(log n))
* A[i] = Σ_(i | j) a[j] a
*
* vT gcd_convolution(vT a, vT b) : O(n log(log n))
* c[k] = Σ_(GCD(i, j) = k) a[i] b[j] c
*
* 1-indexed a[0], b[0] 使
*/
template <typename T>
class Div_mul_transform {
// : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5
vi ps; //
public:
// n
Div_mul_transform(int n) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
// is_prime[i] : i
vb is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
int i = 2;
// √n i
for (; i <= n / i; i++) if (is_prime[i]) {
ps.push_back(i);
for (int j = i * i; j <= n; j += i) is_prime[j] = false;
}
// √n i
for (; i <= n; i++) if (is_prime[i]) ps.push_back(i);
}
Div_mul_transform() {}
// A[i] = Σ_(j | i) a[j] A
void divisor_zeta(vector<T>& a) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
//n = 8
// A[1] = a[1]
// A[2] = a[1] + a[2]
// A[3] = a[1] + a[3]
// A[4] = a[1] + a[2] + a[4]
// A[5] = a[1] + a[5]
// A[6] = a[1] + a[2] + a[3] + a[6]
// A[7] = a[1] + a[7]
// A[8] = a[1] + a[2] + a[4] + a[8]
//
// a[1..n] α(s) = Σ_i a[i] i^(-s)
// α(s) ζ(s) = Σ_i i^(-s)
int n = sz(a) - 1;
//
repe(p, ps) repi(i, 1, n / p) a[p * i] += a[i];
}
// A[i] = Σ_(j | i) a[j] a
void divisor_mobius(vector<T>& A) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
//n = 8
// a[1] = A[1]
// a[2] = -A[1] + A[2]
// a[3] = -A[1] + A[3]
// a[4] = - A[2] + A[4]
// a[5] = -A[1] + A[5]
// a[6] = A[1] - A[2] - A[3] + A[6]
// a[7] = -A[1] + A[7]
// a[8] = - A[4] + A[8]
int n = sz(A) - 1;
//
repe(p, ps) repir(i, n / p, 1) A[p * i] -= A[i];
}
// c[k] = Σ_(LCM(i, j) = k) a[i] b[j] c
vector<T> lcm_convolution(vector<T> a, vector<T> b) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
int n = sz(a) - 1;
// max LCM max
divisor_zeta(a); divisor_zeta(b);
repi(i, 1, n) a[i] *= b[i];
divisor_mobius(a);
return a;
}
// A[i] = Σ_(i | j) a[j] A
void multiple_zeta(vector<T>& a) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
//n = 8
// A[1] = a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8]
// A[2] = a[2] + a[4] + a[6] + a[8]
// A[3] = a[3] + a[6]
// A[4] = a[4] + a[8]
// A[5] = a[5]
// A[6] = a[6]
// A[7] = a[7]
// A[8] = a[8]
//
// a[1..n] α(s) = Σ_i a[i] i^(-s)
// α(s) ζ(-s) = Σ_i i^s
int n = sz(a) - 1;
//
repe(p, ps) repir(i, n / p, 1) a[i] += a[p * i];
}
// A[i] = Σ_(i | j) a[j] a
void multiple_mobius(vector<T>& A) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
//n = 8
// a[1] = A[1] - A[2] - A[3] - A[5] + A[6] - a[7]
// a[2] = A[2] - A[4] - A[6]
// a[3] = A[3] - A[6]
// a[4] = A[4] - A[8]
// a[5] = A[5]
// a[6] = A[6]
// a[7] = A[7]
// a[8] = A[8]
int n = sz(A) - 1;
//
repe(p, ps) repi(i, 1, n / p) A[i] -= A[p * i];
}
// c[k] = Σ_(GCD(i, j) = k) a[i] b[j] c
vector<T> gcd_convolution(vector<T> a, vector<T> b) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
int n = sz(a) - 1;
// min GCD min
multiple_zeta(a); multiple_zeta(b);
repi(i, 1, n) a[i] *= b[i];
multiple_mobius(a);
return a;
}
};
//O(n log(log n))
/*
* i∈[1..n] φ(i)
*
*
*/
vl euler_phi(int n) {
// : https://maspypy.com/%E6%95%B0%E5%AD%A6
        -%E7%95%B3%E3%81%BF%E8%BE%BC%E3%81%BF%E5%85%A5%E9%96%80%EF%BC%9Adirichlet%E7%A9%8D%E3%81%A8%E3%82%BC%E3%83%BC%E3%82%BF%E5%A4%89%E6%8F%9B%E3%8
        3%BB%E3%83%A1%E3%83%93%E3%82%A6
// verify : https://yukicoder.me/problems/no/2249
//
// i d GCD(i, x) = d x∈[1..i]
// x GCD(i/d, y) = 1 y∈[1..i/d] x = y d
// φ(i/d)
// d
// i = Σ_(d|i) φ(i/d)
// ⇔ i = Σ_(d|i) φ(d)
// φ a[i] = i
vl a(n + 1);
repi(i, 1, n) a[i] = i;
// int
Div_mul_transform<ll> dt(n);
dt.divisor_mobius(a);
return a;
}
//
/*
* Interval_set<T>(T L = -INFL, T R = INFL) : O(1)
* [L..R)
*
* int size() : O(1)
*
*
* pTT get(T x) : O(log n)
* x [l..r) {-1, -1}
*
* pTT get_right(T x) : O(log n)
* x < l [l..r) {R+1, R+1}
*
* pTT get_left(T x) : O(log n)
* r ≦ x [l..r) {L-1, L-1}
*
* insert(T l, T r) : O(log n)
* [l..r)
*
* erase(T l, T r) : O(log n)
* [l..r)
*
* vector<pTT> get_all_intervals() : O(n)
* [l..r)
*/
template <class T>
class Interval_set {
// L, R : [L..R)
T L, R;
// l_to_r[l] : l [l..r) r
map<T, T> l_to_r;
public:
// [L..R)
Interval_set(T L = -(T)INFL, T R = (T)INFL) : L(L), R(R) {
// verify : https://yukicoder.me/problems/no/2292
l_to_r[L - 1] = L - 1;
l_to_r[R + 1] = R + 1; //
}
//
int size() const {
return sz(l_to_r);
}
// x [l..r) {-1, -1}
pair<T, T> get(T x) const {
// verify : https://yukicoder.me/problems/no/2292
Assert(L <= x && x < R);
auto it = prev(l_to_r.upper_bound(x));
return it->second <= x ? make_pair(T(-1), T(-1)) : pair<T, T>(*it);
}
// x < l [l..r) {R+1, R+1}
pair<T, T> get_right(T x) const {
// verify : https://atcoder.jp/contests/code-festival-2015-qualb/tasks/codefestival_2015_qualB_d
Assert(L <= x && x < R);
auto it = l_to_r.upper_bound(x);
return *it;
}
// r ≦ x [l..r) {L-1, L-1}
pair<T, T> get_left(T x) const {
Assert(L <= x && x < R);
auto it = prev(l_to_r.lower_bound(x));
return it->second <= x ? *it : *prev(it);
}
// [l..r)
void insert(T l, T r) {
// verify : https://yukicoder.me/problems/no/2292
chmax(l, L); chmin(r, R);
if (l >= r) return;
auto it = l_to_r.lower_bound(l);
// [l..r)
auto pit = prev(it);
if (l <= pit->second) {
//if (l < pit->second) { //
l = pit->first;
it = pit;
}
while (true) {
if (r < it->first) break;
//if (r <= it->first) break; //
// [l..r)
if (r <= it->second) {
r = it->second;
l_to_r.erase(it);
break;
}
it = l_to_r.erase(it);
}
l_to_r[l] = r;
}
// [l..r)
void erase(T l, T r) {
// verify : https://yukicoder.me/problems/no/2292
chmax(l, L); chmin(r, R);
if (l >= r) return;
auto it = l_to_r.lower_bound(l);
// [l..r)
auto pit = prev(it);
if (l < pit->second) {
// [l..r)
if (r < pit->second) l_to_r[r] = pit->second;
pit->second = l;
}
while (true) {
if (r <= it->first) break;
// [l..r)
if (r < it->second) {
T nr = it->second;
l_to_r.erase(it);
l_to_r[r] = nr;
break;
}
it = l_to_r.erase(it);
}
}
// [l..r)
vector<pair<T, T>> get_all_intervals() {
// verify : https://atcoder.jp/contests/abc254/tasks/abc254_g
vector<pair<T, T>> res;
res.reserve(sz(l_to_r) - 2);
repe(lr, l_to_r) {
if (lr.first == L - 1 || lr.first == R + 1) continue;
res.push_back(lr);
}
return res;
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Interval_set& IS) {
repe(p, IS.l_to_r) {
if (p.first == IS.L - 1 || p.first == IS.R + 1) continue;
os << "[" << p.first << "," << p.second << ") ";
}
return os;
}
#endif
};
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
int n = (int)2e5 + 10;
auto phi = euler_phi(n);
phi[1] = 0;
vl acc(n + 2);
repi(i, 0, n) acc[i + 1] = acc[i] + phi[i];
int q;
cin >> q;
rep(hoge, q) {
int l, r;
cin >> l >> r;
l--;
Interval_set I(0, n);
//
{
using T = int;
T n1 = l, n2 = r;
T n_max = max(n1, n2);
T m = (T)(sqrt(n_max) + 1e-12);
vector<tuple<T, T, T, T>> res;
// q i 1 i
int i = 1;
for (; n_max / i > m; i++) {
int q1 = n1 / i;
int q2 = n2 / i;
if (q1 < q2) {
I.insert(q1 + 1, q2 + 1);
}
}
// (q1, q2)
T q1 = n1 / i, q2 = n2 / i;
while (q1 > 0 || q2 > 0) {
// [il1..ir1) : n1/i = q1 i
T il1 = n1 / (q1 + 1) + 1, ir1 = (q1 > 0 ? n1 / q1 + 1 : (T)INFL);
// [il2..ir2) : n2/i = q2 i
T il2 = n2 / (q2 + 1) + 1, ir2 = (q2 > 0 ? n2 / q2 + 1 : (T)INFL);
//
T il = max(il1, il2), ir = min(ir1, ir2);
if (il < ir) {
if (q1 < q2) {
I.insert(q1 + 1, q2 + 1);
}
}
if (ir1 < ir2) q1--;
else q2--;
}
}
// set 使
auto lrs = I.get_all_intervals();
ll res = 0;
for (auto [l, r] : lrs) {
res += acc[r] - acc[l];
}
cout << res << "\n";
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0