Submission #389361

# Submission time Handle Problem Language Result Execution time Memory
389361 2021-04-14T03:36:56 Z Kevin_Zhang_TW Binary Subsequences (info1cup17_binary) C++17
43 / 100
900 ms 226104 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4", "avx2")

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 2010, MAX_W = 30, inf = MAX_N - 5, p = 1e9 + 7;

ll hs(int a, int b) {
	if (a > b) swap(a, b);
	return 1000000ll * a + b;
}

unordered_map<ll, int> mp;

int cal(int a, int b) {
	if (a + b <= 1) return a + b;

	if (a == b) return -1;

	if (a < b) swap(a, b);

	auto &res = mp[hs(a, b)];

	if (res) return res;

	int need = a / (b + 1);

	int val = cal(a % (b + 1), b);

	if (val == -1) need = -1;
	else need += val;

	return res = need;
}
void gen(int a, int b) {
	vector<int> res;
	while (a + b) {
		if (a < b) {
			res.pb(1);
			b -= a + 1;
		}
		else {
			res.pb(0);
			a -= b + 1;
		}
	}
	for (int i = 0;i < res.size();++i)
		cout << res[i] << " \n"[i+1==res.size()];
}

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);

	mp.reserve(15000003);
	int T;
	cin >> T;

	while (T--) {
		int k;
		cin >> k;

		int cnt = 0;

		int need = inf, ind = -1;

		for (int i = 0;i <= k;++i) {
			int v = cal(i, k - i);
			if (v == -1) continue;

			if (chmin(need, v))
				ind = i;
			++cnt;
		}

		cout << cnt << '\n';

		gen(ind, k - ind);

	}
}

Compilation message

binary.cpp: In function 'void gen(int, int)':
binary.cpp:62:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |  for (int i = 0;i < res.size();++i)
      |                 ~~^~~~~~~~~~~~
binary.cpp:63:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |   cout << res[i] << " \n"[i+1==res.size()];
      |                           ~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 371 ms 151752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 460 ms 157620 KB Output is correct
2 Execution timed out 951 ms 195408 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1100 ms 226104 KB Time limit exceeded
2 Halted 0 ms 0 KB -