結果

問題 No.3266 岩井星人は見ずにはいられない
ユーザー umimel
提出日時 2025-09-06 15:01:10
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 4,107 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 164,128 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-09-06 15:01:40
合計ジャッジ時間 3,533 ms
ジャッジサーバーID
(参考情報)
judge / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


void solve(){
    ll N, A; cin >> N >> A;
    string S; cin >> S;
    ll cnt0 = 0; for(int i=0; i<N; i++) if(S[i]=='0') cnt0++;

    // Case1 : cnt1 < cnt0
    if(N < 2*cnt0){
        // 100周は,愚直で計算
        ll ans = 0;
        ll rate = 0;
        for(int i=0; i<100; i++){
            for(int j=0; j<N; j++){
                ans++;
                if(S[j] == '0') rate--;
                if(S[j] == '1' && rate < 0){
                    rate++;
                    A--;
                    if(A == 0){
                        cout << ans << "\n";
                        return;
                    }
                }
            }
        }

        ll cnt1 = N - cnt0;
        if(A%cnt1 == 0){
            ans += N * ((A-cnt1) / cnt1);
            A = cnt1;
            for(int j=0; j<N; j++){
                ans++;
                if(S[j] == '0') rate--;
                if(S[j] == '1' && rate < 0){
                    A--;
                    rate++;
                    if(A == 0){
                        cout << ans << "\n";
                        return;
                    }
                }
            }
        }else{
            ans += N * (A / cnt1);
            A %= cnt1;
            for(int j=0; j<N; j++){
                ans++;
                if(S[j] == '0') rate--;
                if(S[j] == '1' && rate < 0){
                    A--;
                    rate++;
                    if(A == 0){
                        cout << ans << "\n";
                        return;
                    }
                }
            }
        }
    }

    // Case2 : cnt1 >= cnt0
    if(2*cnt0 <= N){
        ll pre_rate = 0L;
        ll rate = 0LL;
        ll ans = 0LL;
        // 100周回して,初期値が収束するのを待つ
        for(int i=0; i<100; i++){
            for(int j=0; j<N; j++){
                ans++;
                if(S[j] == '0') rate--;
                if(S[j] == '1' && rate < 0){
                    rate++;
                    A--;
                    if(A == 0){
                        cout << ans << "\n";
                        return;
                    }
                }
            }
            if(pre_rate == rate) break;
            pre_rate = rate;
        }

        // 1周分のAC数を計算
        ll cnt = 0LL;
        for(int j=0; j<N; j++){
            ans++;
            if(S[j] == '0') rate--;
            if(S[j] == '1' && rate < 0){
                rate++;
                A--;
                cnt++;
                if(A == 0){
                    cout << ans << "\n";
                    return;
                }
            }
        }

        if(A%cnt == 0){
            ans += (A-cnt)/cnt*N;
            A = cnt;
            for(int j=0; j<N; j++){
                ans++;
                if(S[j] == '0') rate--;
                if(S[j] == '1' && rate < 0){
                    A--;
                    rate++;
                    if(A == 0){
                        cout << ans << "\n";
                        return;
                    }
                }
            }
        }else{
            ans += (A / cnt) * N;
            A %= cnt;
            for(int j=0; j<N; j++){
                ans++;
                if(S[j] == '0') rate--;
                if(S[j] == '1' && rate < 0){
                    A--;
                    rate++;
                    if(A == 0){
                        cout << ans << "\n";
                        return;
                    }
                }
            }
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0