codeforces AIM Tech Round 3 (Div. 2)D. Recover the String

D. Recover the String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For each string s consisting of characters '0' and '1' one can define four integers a00a01a10 and a11, where axy is the number ofsubsequences of length 2 of the string s equal to the sequence {x, y}.

In these problem you are given four integers a00a01a10a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than1 000 000.

Input

The only line of the input contains four non-negative integers a00a01a10 and a11. Each of them doesn't exceed 109.

Output

If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000.

Examples
input
1 2 3 4
output
Impossible
input
1 2 2 1
output
0110                                                                                                                             
题意:要你跟据00 01 10 11四种序列的个数构造一个01串(如果能)。
题解:可以跟据00 01 的数量得出0和1的数量,因为它们都应该符合n*(n-1)/2的形式,01 和 10数目之和为a11*a00,所以可以这么构造:
先把左边填满1右边填满0,然后交换1和0,这样10序列减少与01序列增加相等,总和保持不变;
wa点在于特殊情况,注意不符合构造前提,和有0的情况
代码
#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
char s[MAXN];
int main()
{
	int a,b,c,d,n1=0,n0=0;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	if(!b&&!c&&a&&d)
	{
		printf("Impossible");
		return 0;
	}
	if(!a&&!b&&!c&&!d)
	{
		printf("1");
		return 0;
	}
	for(int i=1;i*(i-1)<=2*a;i++)
	{
		if(i*(i-1)==2*a)
		n0=i;
	}
	for(int i=1;i*(i-1)<=2*d;i++)
	{
		if(i*(i-1)==2*d)
		n1=i;
	}
	if(!b&&!c)
	{
		if(d!=0&&n1!=0)
		for(int i=0;i<n1;i++)
		printf("1");
		else if(a!=0&&n0!=0)
		for(int i=0;i<n0;i++)
		printf("0");
		else
		printf("Impossible");
		return 0;
	}
	if(!n1||!n0||n0*n1!=b+c)
	{
		printf("Impossible");
		return 0;
	}
	int m=b/n0;int n=b%n0;
	int l=n==0?n1-m:n1-m-1;
	for(int i=0;i<l;i++)
	printf("1");
	int flag=0;
	for(int i=0;i<n0;i++)
	{
		if(i==n&&n!=0&&!flag)
		{
			printf("1");
			i--;
			flag=1;
		}
		else
		printf("0");
	}
	for(int i=0;i<m;i++)
	printf("1");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值