codeforces 708B (数学构造)

For each string s consisting of characters '0' and '1' one can define four integersa00a01a10 and a11, where axy is the number of subsequences 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 than 1 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.

Example
Input
1 2 3 4
Output
Impossible
Input
1 2 2 1
Output

0110



题意:已知 a,b,c,d. 构造不大于1e6的字符串,使得其中子序列 00 ,01,10,11 的数量 = a,b,c,d。


已知,知道 00 11,的数目 可以知道 0,1的数量 x*(x-1) =a 快速求得正整数解得方法是 开方+1,然后相乘验证一下是否有解。

关于 01和10的数量,假设把0全放左边,1全放右边,得出01的数目就是 cnt1*cnt2 每次把1左移一位,10数量增加,01数量减少


#include <bits/stdc++.h>
using namespace std;

int num[1000010];

int main()
{
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	int cnt1=sqrt(2*a)+1;
	int cnt2=sqrt(2*d)+1;
	if(cnt1*(cnt1-1)!=2*a||cnt2*(cnt2-1)!=2*d)
	{
		puts("Impossible");
		return 0;
	}
	memset(num,0,sizeof(num));
	if(a+b+c+d==0)
	{
		puts("0");
		return 0;
	}
	if(a+b+c==0)
	{
		for(int i=1;i<=cnt2;i++)
			printf("1");
		return 0;
	}
	if(a&&b+c+d==0)
	{
		for(int i=1;i<=cnt1;i++)
			printf("0");
		return 0;
	}
	if(cnt1*cnt2!=b+c)
	{
		puts("Impossible");
		return 0;
	}
	int h=0;
	while(c)
	{
		if(c>=cnt1)
		{
			c-=cnt1;
			num[++h]=1;
		}
		else
		{
			++h;
			num[h+cnt1-c]=1;
			c=0;
			break;
		}
	}
	for(int i=1;i<=cnt2-h;i++)
		num[cnt1+cnt2+1-i]=1;
	for(int i=1;i<=cnt1+cnt2;i++)
		printf("%d",num[i] );
	puts("");
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值