結果

問題 No.2132 1 or X Game
ユーザー milanis48663220milanis48663220
提出日時 2022-11-25 22:10:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 584 ms / 2,000 ms
コード長 3,485 bytes
コンパイル時間 1,344 ms
コンパイル使用メモリ 127,840 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 03:20:06
合計ジャッジ時間 8,449 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 469 ms
6,816 KB
testcase_02 AC 584 ms
6,940 KB
testcase_03 AC 444 ms
6,940 KB
testcase_04 AC 525 ms
6,944 KB
testcase_05 AC 411 ms
6,940 KB
testcase_06 AC 411 ms
6,940 KB
testcase_07 AC 475 ms
6,940 KB
testcase_08 AC 459 ms
6,940 KB
testcase_09 AC 408 ms
6,944 KB
testcase_10 AC 456 ms
6,940 KB
testcase_11 AC 452 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

const ll MOD = 998244353;
using mint = atcoder::modint998244353;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

#define N_MAX 200002
mint inv[N_MAX],fac[N_MAX],finv[N_MAX];

void init(){
    fac[0]=1;fac[1]=1;
    finv[0]=1;finv[1]=1;
    inv[1]=1;
    for(int i=2;i<N_MAX;i++){
        inv[i]=mint(MOD)-inv[MOD%i]*(MOD/i);
        fac[i]=fac[i-1]*(ll) i;
        finv[i]=finv[i-1]*inv[i];
    }
}

mint comb(ll n, ll r){
    if(n < r){
        return mint(0);
    }else{
        return fac[n]*finv[r]*finv[n-r];
    }
}

int naive(int n, int x){
    auto dp = vec3d(n+1, 2, 2, false);
    int ans = 0;
    for(int i = 0; i <= n; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 2; k++){
                if(i >= 1){
                    int j_nx = k, k_nx = 0;
                    if(!dp[i-1][j_nx][k_nx]) dp[i][j][k] = true;
                }
                if(i >= x && j == 0){
                    int j_nx = k, k_nx = 1;
                    if(!dp[i-x][j_nx][k_nx]) dp[i][j][k] = true;
                }
            }
        }
        if(dp[i][0][0]) {
            // cout << i << endl;
            ans++;
        }
    }
    return ans;
}

const ll inf = 2e18;

ll solve(ll n, ll x){
    if(x%2 == 1){
        return (n+1)/2;
    }
    ll m = x/2;
    auto kth = [&](ll k){
        if(k <= m) return 2*k-1;
        ll base = m*2-1;
        ll rem = k-m;
        ll cycle = rem/(m+2);
        ll tail = rem%(m+2);
        ll ans = base + cycle*(2*m+3);
        if(tail >= 1){
            ans += 1+2*(tail-1);
        }
        return ans;
    };
    ll l = 0, r = inf;
    while(r-l > 1){
        ll k = (l+r)/2;
        if(kth(k) > n) r = k;
        else l = k;
    }
    // debug_value(kth(10))
    return l;
}

void test(int n){
    for(int x = 1; x <= 2*n; x++){
        if(solve(n, x) != naive(n, x)){
            cout << n << ' ' << x << ' ' << solve(n, x) << ' ' << naive(n, x) << endl;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    // naive(100, 8);
    // int n; cin >> n;
    // test(n);
    // return 0;
    int t; cin >> t;
    while(t--) {
        ll n, x; cin >> n >> x;
        cout << mint(solve(n, x)) << endl;
    }
}
0