結果
| 問題 | No.3478 XOR-Folding Primes |
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2026-03-18 23:43:50 |
| 言語 | C++23(gnu拡張) (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 144 ms / 4,000 ms |
| コード長 | 1,688 bytes |
| 記録 | |
| コンパイル時間 | 2,055 ms |
| コンパイル使用メモリ | 342,320 KB |
| 実行使用メモリ | 91,312 KB |
| 最終ジャッジ日時 | 2026-03-20 20:50:47 |
| 合計ジャッジ時間 | 4,107 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 8 |
ソースコード
#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] - (isprime[m] && isprime[m ^ 2] && (m ^ 2) > m ? 1 : 0);
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;
}