Codeforces Round #262 (Div. 2)

Codeforces Round #262 (Div. 2)

A:水题,直接不断模拟就可以

B:因为s(x)大小最大到1e9,所以数位和最多为81,这样仅仅要枚举s(x),就仅仅要枚举1到81就可以,然后在计算出x,推断是否符合,符合就加进答案

C:二分高度,然后推断的时候for一遍,每次不符合的位置就去浇水,从左往右推一遍就可以

D:构造。假设k >= 5, 那么就能够直接放偶数,奇数,偶数,奇数,因为偶数和偶数+1异或必定为1。所以这样放4个异或和就到最小的0了,然后k = 1,2,4都能够特判到,关键在于k = 3的情况下怎样去构造,事实上仅仅要找到l的最高位,记下这个数字为p。假设3 p不超过r就能够构造了,能够选l, l + p, 3 p构造出来就是0,假设不行。那么肯定选2个异或和为1是最优

代码:

A:

#include <cstdio>
#include <cstring>
#include <cstdlib>

int n, m;

int main() {
	int ans = 0;
	scanf("%d%d", &n, &m);
	while (n >= m) {
		ans += n - n % m;
		int num = n / m;
		n = n % m + num;
 	}
 	printf("%d\n", ans + n);
 	return 0;
}

B:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <iostream>
using namespace std;

typedef long long ll;

ll a, b, c;
set<ll> ans;
ll out[105], on = 0;

ll pow1(ll x, ll k) {
	ll ans = 1;
	for (ll i = 0; i < k; i++)
		ans *= x;
	return ans;
}

bool judge(ll num, ll x) {
ll sum = 0;	
 while (num) {
		sum += num % 10;
		num /= 10;
 }
 return sum == x;
}

int main() {
	scanf("%lld%lld%lld", &a, &b, &c);
	for (ll i = 0; i <= 81; i++) {
 		ll tmp = b * pow1(i, a) + c;
 		if (tmp > 0LL && tmp < 1000000000LL && judge(tmp, i))
 			ans.insert(tmp);
 	}
 	for (set<ll>::iterator it = ans.begin(); it != ans.end(); it++)
 		out[on++] = *it;
 		printf("%lld\n", on);
	for (ll i = 0; i < on - 1; i++)
		printf("%lld ", out[i]);
	if (on)
		printf("%lld\n", out[on - 1]);
	return 0;
}

C:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long ll;
const int N = 100005;
const int INF = 1100000000;

int n, m, w;
int a[N], tmp[N];

bool judge(int h) {
	int cnt = 0;
	memset(tmp, 0, sizeof(tmp));
	for (int i = 0; i < n; i++) {
		if (i) tmp[i] += tmp[i - 1];
		if (a[i] + tmp[i] < h) {
			int add = h - tmp[i] - a[i];
			cnt += add;
			tmp[i] += add;
			tmp[min(i + w, n)] -= add;
		}
		if (cnt > m) return false;
	}
	return true;
}

int solve() {
	int l = 0, r = INF;
	while (l < r) {
		int mid = (l + r) / 2;
		if (judge(mid)) l = mid + 1;
		else r = mid;
 	}
 	return l - 1;
}

int main() {
	scanf("%d%d%d", &n, &m, &w);
 	for (int i = 0; i < n; i++)
  		scanf("%d", &a[i]);
	printf("%d\n", solve());
	return 0;
}

D:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;

ll l, r, ans[10];
int k, an = 0;

void build() {
    if (k == 1) ans[an++] = l;
    else if (k == 2) {
	if (r - l + 1 == 2) {
	    if (l < (l^r)) ans[an++] = l;
	    else {ans[an++] = l; ans[an++] = r;}
	}
	else {
	    if (l%2) l++;
	    ans[an++] = l; ans[an++] = l + 1;
	}
    }
    else if (k == 3) {
	ll p = 1;
	while (p <= l) p *= 2;
	p /= 2;
	if (3 * p <= r) {
	    ans[an++] = l;
	    ans[an++] = l + p;
	    ans[an++] = 3 * p;
	}
	else {
	    if (l%2) l++;
	    ans[an++] = l;
	    ans[an++] = l + 1;
	}
    }
    else if (k == 4) {
	if (r - l + 1 > 4) {
	    if (l % 2) l++;
	    for (an = 0; an < 4; an++)
		ans[an] = l + an;
	}
	else {
	    ll Min = l;
	    ans[an++] = l;
	    for (int i = 1; i < 16; i++) {
		ll sum = 0;
		for (int j = 0; j < 4; j++) {
		    if (i&(1<<j)) sum ^= (l + j);
		}
		if (sum < Min) {
		    Min = sum;
		    an = 0;
		    for (int j = 0; j < 4; j++) {
			if (i&(1<<j))
			    ans[an++] = (l + j);
		    }
		}
	    }
	}
    }
    else {
	if (l % 2) l++;
	for (an = 0; an < 4; an++)
	    ans[an] = l + an;
    }
}

int main() {
    scanf("%lld%lld%d", &l, &r, &k);
    build();
    ll sum = 0;
    for (int i = 0; i < an; i++)
	sum ^= ans[i];
    printf("%lld\n", sum);
    printf("%d\n", an);
    for (int i = 0; i < an - 1; i++)
	printf("%lld ", ans[i]);
    printf("%lld\n", ans[an - 1]);
    return 0;
}


posted on 2017-05-07 17:48  ljbguanli  阅读(96)  评论(0编辑  收藏  举报