結果

問題 No.1770 N言っちゃダメゲーム (6)
ユーザー milanis48663220milanis48663220
提出日時 2023-08-11 12:00:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 3,705 bytes
コンパイル時間 1,471 ms
コンパイル使用メモリ 129,000 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2024-04-29 04:25:52
合計ジャッジ時間 3,219 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 13 ms
10,240 KB
testcase_31 AC 4 ms
5,376 KB
testcase_32 AC 15 ms
10,112 KB
testcase_33 AC 12 ms
9,216 KB
testcase_34 AC 10 ms
7,808 KB
testcase_35 AC 15 ms
10,880 KB
testcase_36 AC 14 ms
10,496 KB
testcase_37 AC 16 ms
11,904 KB
testcase_38 AC 18 ms
11,776 KB
testcase_39 AC 22 ms
14,848 KB
testcase_40 AC 17 ms
11,776 KB
testcase_41 AC 21 ms
14,848 KB
testcase_42 AC 18 ms
11,776 KB
testcase_43 AC 13 ms
9,216 KB
testcase_44 AC 12 ms
8,832 KB
testcase_45 AC 13 ms
9,088 KB
testcase_46 AC 4 ms
5,376 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>

#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;
}

vector<int> naive(int n, int k){
    auto win = vec2d(n, k+1, false);
    for(int rem = 1; rem < n; rem++){
        for(int pre = 0; pre <= k; pre++){
            bool ok = false;
            for(int cur = 1; cur <= k; cur++){
                if(cur == pre) continue;
                if(cur > rem) continue;
                if(!win[rem-cur][cur]) ok = true; 
            }
            win[rem][pre] = ok;
        }
        // cout << rem << ":";
        // for(int pre = 1; pre <= k; pre++) cout << win[rem][pre] << ' ';
        // cout << endl;
    }
    vector<int> ans;
    for(int cur = 1; cur <= k; cur++){
        if(!win[n-cur-1][cur]) ans.push_back(cur);
    }
    // for(int rem = 0; rem < n; rem++) cout << win[rem][rem%(k+1)] << ' ';
    // cout << endl;
    return ans;
}

vector<int> solve(int n, int k){
    if(k == 1) return {1};
  
    vector<bool> win(n);
    vector<vector<int>> v(k+1);
    vector<bool> all_lose(n);
    vector<int> lose_idx(n, -1);
    int prev_lose = 0;
    all_lose[0] = true;
    for(int x = 0; x <= k; x++) v[(x*2)%(k+1)].push_back(x);
    auto judge_win = [&](int rem, int pre){
        if(all_lose[rem]) return false;
        return pre != lose_idx[rem];
    };
    for(int rem = 1; rem < n; rem++){
        if(rem == prev_lose) {
            all_lose[rem] = true;
            continue;
        }
        if(prev_lose+k+1 == rem){
            if(k%2 == 0){
                // 負ける
                all_lose[rem] = true;
                prev_lose = rem;
                continue;
            }
            int x = (k+1)/2;
            if(win[rem]){
                lose_idx[rem] = x;
                if(rem+x < n) {
                    win[rem+x] = true;
                }
                prev_lose = rem+1;
            }else{
                all_lose[rem] = true;
                prev_lose = rem;
            }
        }
        int r = rem-prev_lose;
        if(win[rem]){

        }else{
            lose_idx[rem] = r;
            if(rem+r < n){
                win[rem+r] = true;
            }
        }     
    }
    
    vector<int> ans;
    for(int cur = 1; cur <= k; cur++){
        if(!judge_win(n-cur-1, cur)) ans.push_back(cur);
    }
    // print_vector(win);
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k; cin >> n >> k;
    // print_vector(naive(n, k));
    auto ans = solve(n, k);
    if(ans.empty()){
        cout << 0 << endl;
    }else{
        print_vector(ans, '\n');
    }
}
0