Codeforces Round #581(Div. 2)

Codeforces Round #581(Div. 2)

CF 1024 A. BowWow and the Timetable

Solution to a problem : find a power of binary $ 4 $ 1 $ is a $ followed by an even number $ 0 $.

So violence sentenced look like.

Code:

#include <bits/stdc++.h>
	
#define N 110 
	
using namespace std;
	
char s[N];
	
int main() {
	scanf("%s", s + 1);
	int n = strlen(s + 1);
	int now = n / 2;
	int flag = 0;
	for (int i = 2; i <= n; i ++ ) {
		if (s[i] == '1') {
			flag = 1;
		}
	}
	now += flag;
	cout << now << endl ;
	return 0;
}

 CF 1024 B. Mislove Has Lost an Array

Solution: find a few topics requirement is appearing before a power of $ k $ a $ 2 $ each had at least once.

The case of the minimum value, then, is to meet the $ k $ equals $ l $, and the rest are equal to $ 1 $ is the minimum.

The case of the maximum value, then, is to meet the $ k $ equals $ r $, and the rest are equal to $ 2 ^ r $ is the maximum.

Code:

#include <bits/stdc++.h>

using namespace std;

int bin[21];

int main() {
	int n, l, r;
	cin >> n >> l >> r;
	bin[0] = 1;
	for (int i = 1; i <= r; i ++ ) {
		bin[i] = bin[i - 1] << 1;
	}
	int mn, mx;
	mn = bin[l] + n - l - 1;
	mx = (n - r + 2) * bin[r - 1] - 1;
	cout << mn << ' ' << mx << endl ;
	return 0;
}

CF 1024 C. Anna, Svyatoslav and Maps

Solution to a problem : I just have to look at a sample in order to understand the meaning of the questions silly Cock Chinese people ......

It is to look at, can not make out at some point, as the shortest length of the shortest and artwork between these points.

A direct violence a sentence on the line.

Shortest then seek a $ floyd $ enough.

Code:

#include <bits/stdc++.h>
	
#define N 110 
	
using namespace std;
	
char s[N][N];
	
int dis[N][N];
	
int p[1000010 ];
	
int ans[1000010 ];
	
int main() {
	int n;
	cin >> n ;
	for (int i = 1; i <= n; i ++ ) {
		scanf("%s", s[i] + 1);
	}
	memset(dis, 0x3f, sizeof dis);
	for (int i = 1; i <= n; i ++ ) {
		for (int j = 1; j <= n; j ++ ) {
			if (s[i][j] == '1') {
				dis[i][j] = 1;
			}
		}
		dis[i][i] = 0;
	}
	for (int k = 1; k <= n; k ++ ) {
		for (int i = 1; i <= n; i ++ ) {
			for (int j = 1; j <= n; j ++ ) {
				dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
			}
		}
	}
	
	int m;
	cin >> m;
	for (int i = 1; i <= m; i ++ ) {
		scanf("%d", &p[i]);
	}
	ans[ ++ ans[0]] = p[1];
	int pre = p[1];
	for (int i = 2; i <= m; i ++ ) {
		if (pre == p[i - 1]) {
			continue;
		}
		if (dis[pre][p[i - 1]] + 1 != dis[pre][p[i]]) {
			pre = p[i - 1];
			ans[ ++ ans[0]] = pre;
		}
	}
	ans[ ++ ans[0]] = p[m];
	cout << ans[0] << endl ;
	for (int i = 1; i <= ans[0]; i ++ ) {
		printf("%d ", ans[i]);
	}
	puts("");
	return 0;
}

 CF 1024 D2. Kirk and a Binary String (hard version)

Problem solution : OMG immortal question ......

That we find out what kind of strings are not moving:

"10" Obviously.

If you can not move $ s $, then $ 1s0 $ can not move.

So we all can not move the strings are deleted can, the rest is turned into a fine.

Code:

#include <bits/stdc++.h>
	
#define N 100010 
	
using namespace std;
	
char s[N];
	
int main() {
	scanf("%s", s + 1);
	int n = strlen(s + 1);
	int tmp = 0;
	for (int i = n; i; i -- ) {
		if (s[i] == '0') {
			tmp ++ ;
		}
		else if (tmp) {
			tmp -- ;
		}
		else {
			s[i] = '0';
		}
	}
	for (int i = 1; i <= n; i ++ ) {
		putchar(s[i]);
	}
	return 0;
}

 CF 1024 E. Natasha, Sasha and the Prefix Sums

Good question ah

First set $ g [x] [y] $ represents, the number of $ X $ $ Y $ there is a $ $ -1, consisting of all sequences, $ F $ sequence value is equal to the number of $ 0 $.

Obviously when $ x> y $, $ g [x] [y] = 0 $. Otherwise it is $ C (x + y, y) - C (x + y, y + 1) $.

Then set $ f [x] [y] $ express answer.

Transfer with $ g [x] [y] $ and the number of combinations like.

Code:

#include <bits/stdc++.h>
	
#define N 4010 
	
using namespace std;
	
const int mod = 998244853 ;
	
typedef long long ll;
	
int qpow(int x, int y) {
	int ans = 1;
	while (y) {
		if (y & 1) {
			ans = (ll)ans * x % mod;
		}
		y >>= 1;
		x = (ll)x * x % mod;
	}
	return ans;
}
	
int f[N][N], g[N][N];
	
int fac[N], inv[N];
	
inline int C(int x, int y) {
	if (x < y) {
		return 0;
	}
	return (ll)fac[x] * inv[y] % mod * inv[x - y] % mod;
}
	
int main() {
	int n, m;
	cin >> n >> m ;
	fac[0] = 1;
	for (int i = 1; i <= n + m; i ++ ) {
		fac[i] = (ll)fac[i - 1] * i % mod;
	}
	inv[n + m] = qpow(fac[n + m], mod - 2);
	for (int i = n + m - 1; ~i; i -- ) {
		inv[i] = (ll)inv[i + 1] * (i + 1) % mod;
	}
	
	g[0][0] = 1;
	for (int i = 0; i <= n; i ++ ) {
		for (int j = i; j <= m; j ++ ) {
			if (i) {
				g[i][j] = g[i - 1][j];
			}
			if (j) {
				g[i][j] = (g[i][j] + g[i][j - 1]) % mod;
			}
		}
	}
	
	for (int i = 1; i <= n; i ++ ) {
		for (int j = 0; j <= m; j ++ ) {
			f[i][j] = (f[i - 1][j] + C(i + j - 1, j)) % mod;
			if (j) {
				f[i][j] = (((f[i][j] + f[i][j - 1]) % mod + g[i][j - 1] - C(i + j - 1, j - 1)) % mod + mod) % mod;
			}
		}
	}
	cout << f[n][m] << endl ;
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/ShuraK/p/11390948.html