結果

問題 No.2512 Mountain Sequences
コンテスト
ユーザー Tehom
提出日時 2026-10-31 00:34:47
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 660 ms / 3,000 ms
+ 485µs
コード長 1,922 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,905 ms
コンパイル使用メモリ 388,348 KB
実行使用メモリ 18,312 KB
最終ジャッジ日時 2026-10-31 00:35:06
合計ジャッジ時間 18,813 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

template <typename T> bool chmin(T &a,T b){if(a>b){a=b;return true;} return false;}
template <typename T> bool chmax(T &a,T b){if(a<b){a=b;return true;} return false;}

// https://github.com/Tehom-math/kyopro-library

ll mod=998244353;
// ll mod=1000000007;
using mint=modint998244353;
vector<mint> fac,inv,finv;
void COMinit(int k){
  fac.resize(k),inv.resize(k),finv.resize(k);
  fac[0]=fac[1]=1;
  inv[1]=1;
  finv[0]=finv[1]=1;
  rep(i,2,k){
    fac[i]=fac[i-1]*i;
    inv[i]=-(mod/i)*inv[mod%i];
    finv[i]=finv[i-1]*inv[i];
  }
  return;
}

mint COM(ll n,ll k){
  if(n<k || n<0 || k<0) return 0;
  assert(n<fac.size());
  return (fac[n]*finv[k])*finv[n-k];
}

struct query{
  int n,m,id;
};

void solve(){
  COMinit(1e6);
  int q; cin >> q;
  vector<query> qr(q);
  rep(i,0,q){
    int n,m; cin >> n >> m;
    qr[i]={n,m,i};
  }
  int w=max(1,(int)sqrt(q));
  sort(all(qr),[&](auto a,auto b){
    if(a.n/w == b.n/w) return a.m<b.m;
    return a.n/w<b.n/w;
  });

  auto [n,m,id]=qr[0];
  mint now=0;
  vector<mint> ans(q);
  {
    rep(j,1,m+1) now+=COM(2*j-2,n-1);
    ans[id]=now;
  }
  mint inv2=mint(2).inv();
  rep(i,1,q){
    auto [N,M,ID]=qr[i];
    while(n<N){
      now=(COM(2*m,n+1)-now)*inv2;
      n++;
    }
    while(n>N){
      now=COM(2*m,n)-2*now;
      n--;
    }
    while(m<M){
      m++;
      now+=COM(2*m-2,n-1);
    }
    while(m>M){
      now-=COM(2*m-2,n-1);
      m--;
    }
    n=N,m=M,id=ID;
    ans[id]=now;
  }
  rep(i,0,q){
    cout << ans[i].val() << "\n";
  }
  
  return;
}
  
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int T=1;
  // cin >> T;
  rep(_,0,T){
    solve();
  }
} 
0