cf 709D 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

题意:

意思是给出四个参数,需要构造一个01串
a00表示构造的01串的子串为00的数量 
a01表示构造的01串的子串为01的数量 
a10表示构造的01串的子串为10的数量 
a11表示构造的01串的子串为11的数量 


譬如01010 a00=2  a01=3  a10=3  a11=1
输出四个参数严格等于给定参数的01串,不存在则输出Impossible。



题解:

这是道构造题,首先可以通过a00和a11计算出该串中0的个数和1的个数。

不难想到,由a00和a11可以确定0和1的数量,定义为c0和c1。 
则一定满足 
c0(c0−1)2==a00 
c1(c1−1)2==a11


注意此处若0和1的个数都大于2那么个数唯一,若a00为0,那么有可能有1个0,也有可能没有0,遇到这种情况那么还需要判断a01和a10,若a01和a10都为0,那么0的个数为0,否则0的个数为1,判断1的个数时也一样。


判断完0和1的个数后,如果其中有一个个数为0,那么需要特判。


如果个数都不为0,且a01和a10个数满足要求是,此时就肯定存在01串,那么先将01串写成前半部分全是1,后半部分全是0,此时a01个数为0,然后对于后半部分的第一个0,如果向前与前面的1交换一次,那么a01个数加一,对每个0进行交换,直到a01个数满足要求。



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1000000+10;
int s[maxn];
typedef long long ll;
int main()
{
	ll a,b,c,d;
	while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d))
	{
		memset(s,0,sizeof(s));
		ll num0,num1;
		if(d==0&&b==0&&c==0)
		num1=0;
		else num1=1;
		if(a==0&&b==0&&c==0)
		num0=0;
		else num0=1;
		while(num0*(num0-1)/2<a)
		num0++;
		if(num0*(num0-1)/2!=a)
		{
			puts("Impossible");
			continue;
		}
		while(num1*(num1-1)/2<d)
		num1++;
		if(num1*(num1-1)/2!=d)
		{
			puts("Impossible");
			continue;
		}
		if(num0*num1<b||num0*num1<c)
		{
			puts("Impossible");
			continue;
		}
		if(num0*num1!=b+c)
		{
			puts("Impossible");
			continue;
		}
		if(num1==0||num0==0)
		{
			if(num1)
			for(int i=0;i<num1;i++) printf("1");
			else if(num0)
			{
				for(int i=0;i<num0;i++) printf("0");
			}
			else
			printf("0");
			printf("\n");
			continue;
		}
		for(int i=1;i<=num1;i++) s[i]=1;
		int pos0=num1+1,pos1=1;
		bool flag=false;
		int tmp=b;
		while(tmp>=num1)
		{
			swap(s[pos0],s[pos1]);
			pos0++,pos1++;
			tmp-=num1;
		}
		if(tmp>0)
		swap(s[pos0],s[pos0-tmp]);
		for(int i=1;i<=num1+num0;i++) printf("%d",s[i]);
		printf("\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值