Submission #389545

# Submission time Handle Problem Language Result Execution time Memory
389545 2021-04-14T07:17:06 Z wiwiho Binary Subsequences (info1cup17_binary) C++14
43 / 100
185 ms 128328 KB
#include <bits/stdc++.h>

#define mp make_pair
#define F first
#define S second
#define eb emplace_back
#define printv(a, b) { \
    for(auto pv : a) b << pv << " "; \
    b << "\n"; \
}

using namespace std;

typedef long long ll;

using pii = pair<int, int>;

const ll MOD = 1000000007;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int sz = 2002;
    vector<vector<ll>> dp(sz + 1, vector<ll>(sz + 1));
    vector<vector<int>> len(sz + 1, vector<int>(sz + 1, -1));
    vector<vector<int>> f(sz + 1, vector<int>(sz + 1, -1));
    dp[1][1] = 1;
    for(int sum = 2; sum <= sz; sum++){
        for(int i = 1; i < sum; i++){
            int j = sum - i;
            if(dp[i][j] == 0) continue;
            
            //add 0
            dp[i][i + j] += dp[i][j];
            dp[i][i + j] %= MOD;
            if(len[i][i + j] == -1 || len[i][j] + 1 < len[i][i + j]){
                len[i][i + j] = len[i][j] + 1;
                f[i][i + j] = 0;
            }

            //add 1
            dp[i + j][j] += dp[i][j];
            dp[i + j][j] %= MOD;
            if(len[i + j][j] == -1 || len[i][j] + 1 < len[i + j][j]){
                len[i + j][j] = len[i][j] + 1;
                f[i + j][j] = 1;
            }
        }
    }

    vector<ll> ans(sz + 1), mn(sz + 1, -1);
    vector<pii> mnp(sz + 1);
    for(int i = 1; i <= sz; i++){
        for(int j = 1; i + j <= sz; j++){
            if(dp[i][j] == 0) continue;
            ans[i + j - 2] += dp[i][j];
            ans[i + j - 2] %= MOD;
            //if(i + j - 2 == 8) cerr << i << " " << j << " " << len[i][j] << " " << mn[i + j - 2] << "\n";
            if(mn[i + j - 2] == -1 || len[i][j] < mn[i + j - 2]){
                mn[i + j - 2] = len[i][j];
                mnp[i + j - 2] = mp(i, j);
            }
        }
    }

    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        cout << ans[n] << "\n";

        vector<int> s;
        int x = mnp[n].F, y = mnp[n].S;
        while(f[x][y] != -1){
//            cerr << x << " " << y << " " << f[x][y] << " " << len[x][y] << "\n";
            s.eb(f[x][y]);
            if(f[x][y] == 0) y -= x;
            else x -= y;
        }
        reverse(s.begin(), s.end());
        printv(s, cout);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 120 ms 63428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 185 ms 128280 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 183 ms 128328 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -