Codeforces Round #262

div. 2

A. Vasya and Socks

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	scanf("%d%d", &n, &m);
	int last=0;
	int ans=0;
	while(n)
	{
		n--;
		last++;
		ans++;
		if(last == m)
		{
			last = 0;
			n++;
		}
	}
	printf("%d\n",ans);
	return 0;
}

B. Little Dima and Equation

#include <bits/stdc++.h>
using namespace std;
#define ll __int64
#define inf 1000000000
ll p[100];
int main()
{
	int a, b, c, li=0;
	scanf("%d%d%d", &a, &b, &c);
	for(int i = 1; i <= 81; i++)
	{
		ll si = (ll)b * pow(i*1.0, a) + c;
		int s=0;
		ll sum = si;
		while(sum)
		{
			s += sum%10;
			sum/=10;
		}
		if(si > inf) continue;
		if(s == i)
		{
			li++; 
			p[li] = si;
		}
	}
	printf("%d\n", li);
	for(int i = 1; i <= li; i++)
	{
		if(i!=li) printf("%I64d ", p[i]);
		else printf("%I64d\n", p[i]);
	}
	return 0;
}

C. Present

n盆花,澆水可以澆m天,每次澆連續的w盆

每盆花有一個初始值a

澆一次水a加1

最終要使n盆花的最小值最大

求這個最大值

用折半查找的方法做

根據題目,我們可以知道a的範圍  1<=a<=10^9+10^5

折半查找a

#include <bits/stdc++.h>
using namespace std;
#define inf 100005
#define maxn 1e9+1e5
#define ll __int64
ll p[inf];
ll s[inf];
int n, m, w;
int solve(ll num)
{
	memset(s, 0, sizeof(s));
	ll last = 0;
	ll wi = 0;
	for(int i = 1; i <= n; i++)
	{
		last += s[i];
		if(p[i] +last < num) 
		{
			s[(i+w)>n?n+1:i+w] -= (num-p[i]-last);
			wi += (num-p[i]-last);
			last += (num-p[i]-last);
			if(wi > (ll)m) return 0;
		}
	}
	return 1;
}
int main()
{
	scanf("%d%d%d", &n, &m, &w);
	for(int i = 1; i <= n; i++)
	{
		scanf("%I64d", &p[i]);
	}
	ll l = 1, r = maxn;
	ll ans = 0;
	while(l <= r)
	{
		ll mid = (l+r) >> 1;
		int s = solve(mid);
		if(s == 1)
		{
			l = mid+1;
			ans = max(ans , mid);
		}
		else
		{
			r = mid-1;
		}  
	}
	printf("%I64d\n", ans);
	return 0;
}

D. Little Victor and Set

在l~r中,選出x個組成數列s[ ](1<=x<=k),使得s[1]^s[2]^s[3]^……^s[x]最小

小知識點:(2*x) ^ (2*x+1) = 1

分別討論r-l+1  和  k

比較煩的就是s[]包含的元素只有3個的時候

3個元素的規律應當是這樣子的

1100……000

1011……111

0111……111

#include <bits/stdc++.h>
using namespace std;
#define ll __int64
int main()
{
	ll l, r, k;
	scanf("%I64d%I64d%I64d", &l, &r, &k);
	if(k == 1 || r - l + 1 == 1)
	{
		printf("%I64d\n1\n%I64d\n", l, l);
		return 0;
	}
	if(r - l + 1 == 2)
	{
		if(l&1) 
		{
			ll ans = l^r;
			if(l < ans) printf("%I64d\n1\n%I64d\n", l, l);
			else printf("%I64d\n2\n%I64d %I64d\n", l^r, l, r);
		}
		else printf("1\n2\n%I64d %I64d\n", l, r);
		return 0;
	}
	if(r - l + 1 == 3)
	{
		ll a, b;
		if(l&1) a = l+1, b = r;
		else a = l, b = l+1;
		if(k == 2)
		{
			printf("1\n2\n%I64d %I64d\n", a, b);
			return 0;
		}
		else
		{
			ll ans = l^(l+1);
			ans = ans^r;
			if(ans == 0)
			{
				printf("0\n3\n%I64d %I64d %I64d\n", l, l+1, r);
				return 0;
			}
			else
			{
				printf("1\n2\n%I64d %I64d\n", a, b);
				return 0;
			}
		}
	}
	if(r - l + 1 == 4)
	{
		if(k == 2)
		{
			ll a, b;
			if(l&1) l++;
			printf("1\n2\n%I64d %I64d\n", l, l+1);
			return 0;
		}
		else
		{
			if(l&1 || k == 3)
			{
				ll a = l, b = l+1, c = l+2, d = l+3;
				ll ans = a^b;
				ans = ans^c;
				if(ans == 0)
				{
					printf("0\n3\n%I64d %I64d %I64d\n", a, b, c);
					return 0;
				}
				ans = a^b;
				ans = ans^d;
				if(ans == 0)
				{
					printf("0\n3\n%I64d %I64d %I64d\n", a, b, d);
					return 0;
				}
				ans = a^c;
				ans = ans^d;
				if(ans == 0)
				{
					printf("0\n3\n%I64d %I64d %I64d\n", a, c, d);
					return 0;
				}
				ans = b^c;
				ans = ans^d;
				if(ans == 0)
				{
					printf("0\n3\n%I64d %I64d %I64d\n", b, c, d);
					return 0;
				}
				if(l&1) l++;
				printf("1\n2\n%I64d %I64d\n", l, l+1);
			}
			else printf("0\n4\n%I64d %I64d %I64d %I64d\n", l, l+1, l+2, l+3);
		}
		return 0;
	}
	if(r - l + 1 > 4)
	{
		if(k == 2)
		{
			ll a, b;
			if(l&1) a = l+1, b = l+2;
			else a = l, b = l+1;
			printf("1\n2\n%I64d %I64d", a, b);
			return 0;
		}
		if(k == 3)
		{
			ll x=-1,y,z;
		    for(ll i=3; i<=r; i=i<<1)
			{
		        if((i^(i-1))>=l)
				{
		            x=i;
		            y=i - 1;
		            z=i^(i-1);
		            break;
		        }
		    }
			if(x!=-1)
			{
		        printf("0\n3\n%I64d %I64d %I64d\n",x,y,z);
		    }
		    else 
			{
		        if(l&1) l++;
		        printf("1\n2\n%I64d %I64d\n",l,l+1);
		    }
		    return 0;
		}
		if(k >= 4)
		{
			ll a, b, c, d;
			if(l&1) l++;
			printf("0\n4\n%I64d %I64d %I64d %I64d\n", l, l+1, l+2, l+3);
		}
	}
	return 0;
}

E. Roland and Rose


發佈了52 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章