結果

問題 No.3478 XOR-Folding Primes
コンテスト
ユーザー detteiuu
提出日時 2026-03-20 23:21:07
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 991 ms / 4,000 ms
コード長 2,982 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,590 ms
コンパイル使用メモリ 385,420 KB
実行使用メモリ 205,148 KB
最終ジャッジ日時 2026-03-20 23:21:20
合計ジャッジ時間 9,201 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

#define pass (void)0
#define INF (1<<30)-1
#define INFLL (1LL<<60)-1
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define YesNo(cond) cout << ((cond) ? "Yes\n" : "No\n")
#define YESNO(cond) cout << ((cond) ? "YES\n" : "NO\n")

using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;

template <typename T> void print(const T& value) { cout << value << "\n"; }
template <typename T> void print(const vector<T>& vec) { for (auto& v : vec) cout << v << " "; cout << "\n"; }
template <typename T> void input(vector<T>& vec) { for (auto& v : vec) cin >> v; };
template <typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }

#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using vm = vector<mint>;

vector<bool> eratosthenes(int n) {
    vector<bool> sieve(n + 1, true);

    for (int i = 0; i * 1LL * i <= n; i++) {
        if (i < 2) {
            sieve[i] = false;
        } else if (sieve[i]) {
            for (int j = 2; j <= n / i; j++) {
                sieve[i * j] = false;
            }
        }
    }
    return sieve;
}

ll MOD = 998244353;

vvl matrix(vvl a, vvl b) {
    vvl ans(sz(a), vl(sz(b[0]), 0));
    rep (i, sz(a)) {
        rep (j, sz(b[0])) {
            rep (k, sz(b)) {
                ans[i][j] += a[i][k]*b[k][j]%MOD;
                ans[i][j] %= MOD;
            }
        }
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);

    vector<bool> E = eratosthenes(10000000);
    vl cum(1, 0);
    vl cum2(1, 0);
    rep2 (i, 1, 10000001) {
        cum.push_back(cum[sz(cum)-1]);
        cum2.push_back(cum2[sz(cum2)-1]);
        if (E[i] && E[i^2] && (i^2) < i) cum[sz(cum)-1] ++;
        if (E[i]) cum2[sz(cum2)-1] ++;
    }

    int T;
    cin >> T;
    while (T--) {
        ll N, M;
        cin >> N >> M;
        if (N == 1) {
            print(cum2[M]);
            continue;
        }
        vvl dp(2, vl(2, 0));
        ll C = cum[M];
        vvl ansM(2, vl(1, 0));
        ansM[0][0] = 1;
        ansM[1][0] = C*2%MOD;
        dp[0][1] = 1;
        dp[1][0] = C*2%MOD;
        dp[1][1] = 1;
        rep (i, 30) {
            if (1<<i & (N-1)) ansM = matrix(dp, ansM);
            dp = matrix(dp, dp);
        }
        
        print((ansM[0][0]+ansM[1][0])%MOD);
    }
}
0