Submission #270522

# Submission time Handle Problem Language Result Execution time Memory
270522 2020-08-17T17:25:36 Z s0m30n3 Binary Subsequences (info1cup17_binary) C++14
100 / 100
712 ms 512 KB
#include<bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pb push_back
#define ii pair<int,int>
#define iii pair<ii,int>
#define ppb pop_back
#define st first
#define nd second
#define ll long long
#define inf 1000000000
#define MOD 1000000007 
#define LOG 40
#define EPS 0.000000001
#define MAX 1000005
using namespace std;

int solve_reverse(int fix_0, int fix_1) {

	int steps = 0;
	while(max(fix_0, fix_1) > 0) {
		if(fix_0 == fix_1)
			return inf;
		if(fix_0 > fix_1) {
			steps += fix_0 / (fix_1 + 1);
			fix_0 %= fix_1 + 1;
		}
		else {
			steps += fix_1 / (fix_0 + 1);
			fix_1 %= fix_0 + 1;	
		}
	}
	return steps;

}

void solve() {

	int k, tot = 0, best = inf;
	int dp_fix_0 = -1;
	cin >> k;
	// fix dp[n][0]
	for(int dp = 0; dp <= k; dp++) {
		// check validity
		int steps = solve_reverse(dp, k - dp);
		if(steps != inf) {
			tot++;
			if(steps < best) {
				best = steps;
				dp_fix_0 = dp;
			}
		}
	}
	cout << tot << "\n";
	if(tot == 0) {
		cout << -1 << "\n";
		return ;
	}
	int dp_fix_1 = k - dp_fix_0;
	vector<char> result;
	while(max(dp_fix_0, dp_fix_1) > 0) {
		if(dp_fix_0 > dp_fix_1) {
			dp_fix_0 -= dp_fix_1 + 1;
			result.pb('0');
		}
		else {
			dp_fix_1 -= dp_fix_0 + 1;
			result.pb('1');
		}
	}
	reverse(result.begin(), result.end());
	for(auto c: result) 
		cout << c << " ";
	cout << "\n";

}

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int t;
	cin >> t;
	while(t--)
		solve();

}
# Verdict Execution time Memory Grader output
1 Correct 80 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 384 KB Output is correct
2 Correct 60 ms 384 KB Output is correct
3 Correct 59 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 712 ms 384 KB Output is correct
2 Correct 709 ms 384 KB Output is correct
3 Correct 696 ms 384 KB Output is correct