1453D - Checkpoints

D - Checkpoints

1453D

两个1之间肯定是独立的,卡在不会算期望(

数学真的杀我
为啥不试试递推呢,自信到觉得能嗯算

P n P_n Pn为n个0的期望

p n = 1 2 ( P n − 1 + 1 ) + 1 2 ( P n − 1 + 1 + P n ) p_n = \frac{1}{2}(P_{n -1} + 1) + \frac{1}{2}(P_{n -1}+ 1+P_n) pn=21(Pn1+1)+21(Pn1+1+Pn)

化简: P n = P n − 1 × 2 + 2 P_n = P_{n-1} \times 2 +2 Pn=Pn1×2+2

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll p[100];

inline void init() {
    
    
	p[0] = 2;
	for(int i = 1; i <= 60; ++i) {
    
    
		p[i] = p[i - 1] * 2 + 2;
	}
}

int main() {
    
    
	init();
	//cout << p[60] << endl;
	
	int T;
	cin >> T;
	while(T--) {
    
    
		ll k;
		cin >> k;
		
		if(k & 1) {
    
    
			cout << -1 << endl;
			continue;
		}
		
		vector<int> ans;
		ans.clear();
		
		for(int i = 60; i >= 0; --i) {
    
    
			if(k >= p[i]) {
    
    
				while(k >= p[i]) {
    
    
					ans.push_back(i);
					k -= p[i];
				}
			}
		}
		
		int sum = 0;
		for(auto x : ans) {
    
    
			sum += x + 1;
		}
		
		cout << sum << endl;
		
		for(auto x : ans) {
    
    
			cout << 1 << " ";
			for(int i = 0; i < x; ++i) {
    
    
				cout << 0 << " ";
			}
		}
		
		cout << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39602052/article/details/110679416