結果

問題 No.1760 Setwise Coprime
ユーザー dekomori_sanaedekomori_sanae
提出日時 2022-02-24 15:48:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 5,293 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 90,396 KB
実行使用メモリ 15,440 KB
最終ジャッジ日時 2023-09-15 08:43:54
合計ジャッジ時間 3,610 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
9,344 KB
testcase_01 AC 19 ms
9,436 KB
testcase_02 AC 19 ms
9,376 KB
testcase_03 AC 18 ms
9,424 KB
testcase_04 AC 18 ms
9,604 KB
testcase_05 AC 19 ms
9,356 KB
testcase_06 AC 19 ms
9,424 KB
testcase_07 AC 19 ms
9,364 KB
testcase_08 AC 19 ms
9,340 KB
testcase_09 AC 19 ms
9,424 KB
testcase_10 AC 18 ms
9,356 KB
testcase_11 AC 19 ms
9,300 KB
testcase_12 AC 19 ms
9,420 KB
testcase_13 AC 18 ms
9,472 KB
testcase_14 AC 19 ms
9,372 KB
testcase_15 AC 19 ms
9,436 KB
testcase_16 AC 19 ms
9,352 KB
testcase_17 AC 19 ms
9,344 KB
testcase_18 AC 19 ms
9,400 KB
testcase_19 AC 19 ms
9,300 KB
testcase_20 AC 19 ms
9,384 KB
testcase_21 AC 50 ms
14,048 KB
testcase_22 AC 46 ms
13,524 KB
testcase_23 AC 54 ms
14,912 KB
testcase_24 AC 34 ms
11,724 KB
testcase_25 AC 50 ms
14,128 KB
testcase_26 AC 42 ms
13,068 KB
testcase_27 AC 29 ms
10,944 KB
testcase_28 AC 23 ms
10,144 KB
testcase_29 AC 55 ms
15,412 KB
testcase_30 AC 55 ms
14,836 KB
testcase_31 AC 31 ms
11,252 KB
testcase_32 AC 53 ms
14,708 KB
testcase_33 AC 42 ms
12,772 KB
testcase_34 AC 56 ms
15,028 KB
testcase_35 AC 48 ms
13,828 KB
testcase_36 AC 59 ms
15,372 KB
testcase_37 AC 59 ms
15,440 KB
testcase_38 AC 60 ms
15,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 998244353;
const ll INF = 1e16;

struct Eratosthenes {

    vl isprime;  // isprime[i] : iが素数なら1
    vl minfactor;  // minfactor[i] : iを割り切る最小の素数
    vl mobius;  // mobius[i] : メビウス関数 μ(i)

    Eratosthenes(ll n) : isprime(n+1, 1), 
                         minfactor(n+1, -1), 
                         mobius(n+1, 1) {
        isprime[1] = 0;
        minfactor[1] = 1;
        exrep(p, 2, n) {
            if(!isprime[p]) {
                continue;
            }
            minfactor[p] = p;
            mobius[p] = -1;
            for(ll q = 2*p; q <= n; q += p) {
                isprime[q] = 0;
                if(minfactor[q] == -1) {
                    minfactor[q] = p;
                }
                if((q / p) % p == 0) {
                    mobius[q] = 0;
                }
                else {
                    mobius[q] = -mobius[q];
                }
            }
        }
    }

    // 高速素因数分解。O(log(n))でnを素因数分解する
    vector<P> factorize(ll n) {  // (素因数, 指数) のvectorを返す
        vector<P> res;
        while(n > 1) {
            ll p = minfactor[n];
            ll i = 0;
            while(minfactor[n] == p) {
                n /= p;
                i++;
            }
            res.emplace_back(make_pair(p, i));
        }   
        return res;
    }

    // 高速約数列挙。O(σ(n))でnの約数を求める。σ(n)はnの約数の個数
    vl divisors(ll n) {
        vl res({1});
        auto pf = factorize(n);
        for(auto p : pf) {
            ll s = res.size();
            rep(i, s) {
                ll x = 1;
                rep(j, p.second) {
                    x *= p.first;
                    res.pb(res[i] * x);
                }
            }
        }
        return res;
    }
};

// エラトステネスの篩。計算量はO(n*loglog(n))
vl Eratosthenes1(ll n) {
    vl isprime(n+1, 1);
    isprime[1] = 0;
    exrep(p, 2, n) {
        if(!isprime[p]) {
            continue;
        }
        for(ll q = 2*p; q <= n; q += p) {
            isprime[q] = 0;
        }
    }
    return isprime;
}

// 約数系高速ゼータ変換。計算量はO(n*loglog(n))
template<class T> void fast_zeta(vector<T> &f) {
    ll n = f.size();
    vl isprime = Eratosthenes1(n);
    exrep(p, 2, n-1) {
        if(!isprime[p]) {
            continue;
        }
        for(ll k = 1; k <= (n - 1) / p; k++) {
            f[k * p] += f[k];
            f[k * p] %= mod;
        }
    }
}   

// 約数系高速メビウス変換。計算量はO(n*loglog(n))
template<class T> void fast_mobius(vector<T> &F) {
    ll n = F.size();
    vl isprime = Eratosthenes1(n);
    exrep(p, 2, n-1) {
        if(!isprime[p]) {
            continue;
        }
        for(ll k = (n - 1) / p; k >= 1; k--) {
            F[k * p] += mod - F[k];
            F[k * p] %= mod; 
        }
    }
}

// LCM畳み込み。計算量はO(n*loglog(n))
template<class T> vector<T> lcm_convolution(const vector<T> &f, const vector<T> &g) {
    ll n = 200010;  // 配列の最大値を設定する
    vector<T> F(n), G(n), H(n);
    rep(i, f.size()) {
        F[i] = f[i];
    }
    rep(i, g.size()) {
        G[i] = g[i];
    }
    fast_zeta(F);
    fast_zeta(G);
    exrep(i, 1, n-1) {
        H[i] = F[i] * G[i] % mod;
    }
    fast_mobius(H);
    return H;
}

const ll MAX = 5;

ll inv[MAX];  // inv[i] : iの逆数のmod

void init() {
    inv[0] = inv[1] = 1;
    for(ll i = 1; i < MAX; i++) {
        if(i >= 2) {
            inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        }
    }
}

// a^n (mod.MOD)を求める。計算量はO(logn)
ll modpow(ll a, ll n, ll MOD = mod) {
    if(n == 0) {
        return 1;
    }
    if(n % 2 == 1) {
        return a * modpow(a, n-1, MOD) % MOD;
    }
    return modpow(a, n/2, MOD) * modpow(a, n/2, MOD) % MOD;
}

int main() {
    ll n;
    cin >> n;

    init();

    Eratosthenes e(n+1);

    ll m = 0;
    exrep(i, 1, n) {
        m += e.mobius[i];
        m %= mod;
    }

    vl a(n+1);
    exrep(i, 1, n) {
        a[i] = e.mobius[i] * modpow(2LL, n / i) % mod;
    }

    ll x = 0;
    exrep(i, 1, n) {
        x += a[i];
        x %= mod;
    }

    vl b = lcm_convolution(a, a);

    ll y = 0;
    exrep(i, 1, n) {
        y += b[i];
        y %= mod;
    }

    ll z = 0;
    exrep(i, 1, n) {
        z += b[i] * modpow(3 * inv[4] % mod, n / i);
        z %= mod;
    }

    ll ans = (m * m + x * x + z + 2*mod - y - 2 * m * x % mod) % mod;

    out(ans);
    re0;
}
0