結果

問題 No.3120 Lower Nim
ユーザー Yu_212
提出日時 2025-04-19 10:18:46
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 4,277 bytes
コンパイル時間 4,143 ms
コンパイル使用メモリ 277,008 KB
実行使用メモリ 26,228 KB
平均クエリ数 2685.91
最終ジャッジ日時 2025-04-19 10:19:00
合計ジャッジ時間 11,281 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int iinf = 1e9;
const ll inf = 1e18;
template<ll mod>
struct Mint {
    using M=Mint; ll v;
    M& put(ll x) { v=(x<mod)?x:x-mod; return *this; }
    Mint(ll x=0) { put(x%mod+mod); }
    M operator+(M m) {return M().put(v+m.v);}
    M operator-(M m) {return M().put(v+mod-m.v);}
    M operator*(M m) {return M().put(v*m.v%mod);}
    M operator/(M m) {return M().put(v*m.inv().v%mod);}
    M operator+=(M m) { return put(v+m.v); }
    M operator-=(M m) { return put(v+mod-m.v); }
    M operator*=(M m) { return put(v*m.v%mod); }
    M operator/=(M m) { return put(v*m.inv().v%mod); }
    bool operator==(M m) { return v==m.v; }
    M pow(ll m) const {
        M x=v, res=1;
        while (m) {
            if (m&1) res=res*x;
            x=x*x; m>>=1;
        }
        return res;
    }
    M inv() { return pow(mod-2); }
};
template<ll mod>
ostream&operator<<(ostream&o,Mint<mod>v){return o<<v.v;}


template<typename T>
ostream& operator<<(ostream &o, vector<T> v) {
    for (int i = 0; i < v.size(); i++)
        o << v[i] << (i+1<v.size()?" ":"");
    return o;
}
template <typename T>
struct SegTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T ti;
    vector<T> dat;
    SegTree() {}
    SegTree(F f, T ti,int num) : f(f), ti(ti) {
        n = max(__bit_ceil(num), 1);
        dat.assign(n << 1, ti);
    }
    SegTree(F f,T ti,vector<T>&v):SegTree(f,ti,v.size()){
        for (int i = 0; i < v.size(); i++)
            dat[n + i] = v[i];
        for(int i=n-1;i;i--) dat[i]=f(dat[i*2], dat[i*2+1]);
    }
    void set_val(int k, T x) {
        dat[k += n] = x;
        while(k >>= 1) dat[k] = f(dat[k*2], dat[k*2+1]);
    }
    T query(int a, int b) {
        if (a >= b) return ti;
        T vl = ti, vr = ti;
        for (int l=a+n, r=b+n; l<r; l>>=1, r>>=1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};
struct Edge { int to; ll cost; };

const int MOD = 998244353;

// a^e mod
ll modpow(ll a, ll e=MOD-2){
    ll r=1;
    while(e){
        if(e&1) r=r*a%MOD;
        a=a*a%MOD;
        e>>=1;
    }
    return r;
}

using mint = Mint<MOD>;
mint sm(mint n) {
    return n * (n+1) / 2;
}
mint sm(mint l, mint r) {
    return sm(r) - sm(l);
}

ll naive(ll n, ll a) {
    ll ans = 0;
    for (ll i = 1; i <= n; i++) {
        ll x = n;
        while (x > i) {
            if (x % a == 0  && x / a >= i) {
                x /= a;
                ans++;
            } else {
                x--;
                ans++;
            }
        }
    }
    return ans;
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<int> c(20);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 20; j++) {
            c[j] += a[i] >> j & 1;
        }
    }
    bool first = false;
    for (int i = 0; i < 20; i++) {
        if (c[i] % 2 == 1) {
            first = true;
        }
    }
    auto play = [&](int i, int x) {
        cout << i+1 << " " << x<<endl;
        a[i]-=x;
        int ret;
        cin >> ret;
        if (ret == 1) {
            return true;
        }
        if (ret == -1) return true;
        assert(ret == 0);
        int ji, jx;
        cin >> ji >> jx;
        ji--;
        a[ji] -= jx;
        cin >> ret;
        if (ret == -1) return true;
        assert(ret == 0);
        return false;
    };
    if (first) {
        cout << "First" << endl;
    } else {
        cout << "Second" << endl;
        {
            int ji, jx;
            cin >> ji >> jx;
            ji--;
            a[ji] -= jx;
            int ret;
            cin >> ret;
            if (ret == -1) return 0;
            assert(ret == 0);
        }
    }
    while (true) {
        for (int i = 0; i <= 20; i++) {
            assert(i < 20);
            int c = 0;
            int use = -1;
            for (int j = 0; j < n; j++) {
                c += a[j]>>i&1;
                if (a[j]>=(1<<i)) use = j;
            }
            if (c % 2 == 0) continue;
            assert(use != -1);
            if (play(use, 1<<i)) return 0;
            break;
        }
    }
    return 0;
}
0