답안 #389341

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
389341 2021-04-14T03:13:53 Z Kevin_Zhang_TW Binary Subsequences (info1cup17_binary) C++17
82 / 100
900 ms 328 KB
#pragma GCC optimize("Ofast")

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

bool valid(int a, int b) {
	DE(a, b);
	while (a + b > 1) {
		if (a < b) swap(a, b);
		if (a < b + 1) return false;
		a %= b + 1;
	}
	return true;
}

int cal(int a, int b) {
	int res = 0, val = 0;
	while (a + b > 1) {
		if (a < b) swap(a, b);
		res += a / (b + 1);
		a %= b + 1;
	}
	return res + a + b;
}
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);
	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) {
			if (valid(i, k - i)) {
				++cnt;
				if (chmin(need, cal(i, k - i)))
					ind = i;
			}
		}
		cout << cnt << '\n';

		gen(ind, k - ind);

	}
}

Compilation message

binary.cpp: In function 'bool valid(int, int)':
binary.cpp:16:17: warning: statement has no effect [-Wunused-value]
   16 | #define DE(...) 0
      |                 ^
binary.cpp:23:2: note: in expansion of macro 'DE'
   23 |  DE(a, b);
      |  ^~
binary.cpp: In function 'int cal(int, int)':
binary.cpp:33:15: warning: unused variable 'val' [-Wunused-variable]
   33 |  int res = 0, val = 0;
      |               ^~~
binary.cpp: In function 'void gen(int, int)':
binary.cpp:53:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |  for (int i = 0;i < res.size();++i)
      |                 ~~^~~~~~~~~~~~
binary.cpp:54:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   cout << res[i] << " \n"[i+1==res.size()];
      |                           ~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 204 KB Output is correct
2 Correct 100 ms 204 KB Output is correct
3 Correct 94 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 963 ms 292 KB Time limit exceeded
2 Halted 0 ms 0 KB -