結果

問題 No.3478 XOR-Folding Primes
コンテスト
ユーザー ぽえ
提出日時 2026-03-18 23:28:25
言語 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
結果
WA  
実行時間 -
コード長 1,631 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,841 ms
コンパイル使用メモリ 340,080 KB
実行使用メモリ 91,464 KB
最終ジャッジ日時 2026-03-20 20:50:43
合計ジャッジ時間 4,996 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other AC * 1 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/modint.hpp>
using mint = atcoder::modint998244353;
using ll = long long;
const int M = 10000000;
using Mat = array<array<mint,2>,2>;

Mat mul(const Mat &a, const Mat &b){
    Mat c;
    c[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0];
    c[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1];
    c[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0];
    c[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1];
    return c;
}

Mat poe(Mat a, ll n){
    Mat r;
    r[0][0]=1; r[0][1]=0;
    r[1][0]=0; r[1][1]=1;
    while(n){
        if(n & 1) r = mul(r, a);
        a = mul(a, a);
        n >>= 1;
    }
    return r;
}

vector<char> isprime(M+1, true);
vector<int> prime_cnt(M+1), good_cnt(M+1);

void solve() {
    int n, m; cin >> n >> m;
    if(n == 1) {
        cout << mint(prime_cnt[m]).val() << '\n';
        return;
    }
    int c = good_cnt[m];
    mint cm = c;
    Mat a;
    a[0][0]=0; a[0][1]=1;
    a[1][0]=cm; a[1][1]=1;
    Mat P = poe(a, n-1);
    mint dp2 = P[0][0] + P[0][1]*cm; 
    mint dpg = P[1][0] + P[1][1]*cm;
    mint ans = dp2 + dpg;
    cout << ans.val() << '\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    isprime[0] = isprime[1] = false;
    for(ll p=2; (ll)p*p<=M; ++p) if(isprime[p]) for(ll q=p*p; q<=M; q+=p) isprime[q]=false;
    for(int x=1; x<=M; ++x){
        prime_cnt[x] = prime_cnt[x-1] + isprime[x];
        good_cnt[x] = good_cnt[x-1];
        if(3<=x && isprime[x]){
            int q = x ^ 2;
            if (q<=M && isprime[q]) ++good_cnt[x];
        }
    }
    int T; cin >> T;
    while (T--) solve();
    return 0;
}
0