J(2258): Jumbled String

J(2258): Jumbled String

Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 132     Solved: 27     SpecialJudge


Description

Recall that a subsequence of a string is any string obtained by removing some subset of characters from the string, for instance "string", "sing", "i" and "sg" are all subsequences of "string". If the same subsequence can be obtained in exactly t different ways, by removing different subsets of characters, we say that the subsequence occurs t times.

Jingfei wants to create a nonempty bit string that has the following properties:

  1. the subsequence 00 occurs a times,

  2. the subsequence 01 occurs b times,

  3. the subsequence 10 occurs c times, and

  4. the subsequence 11 occurs d times.

However, Jingfei does not know how to create such a string -- or whether it is even possible. Could you help her?

Input

he input consists of a single line with four integers abc, and d (0 ≤ a, b, c, d ≤ 109​).

Output

Output a bit string that satisfies the given requirements. If there are several solutions, output any one of them. If there are no solutions, output "impossible".

Sample Input

3 4 2 1
---------------------
5 0 0 5

Sample Output

01001
---------------------
impossible

Hint

这题是真的恶心,做了我近两个小时。

首先根据题目给的abcd求出0的个数和1的个数。然后对着01串构造就可以了

若a不为0,则0的个数为x;===>x(x-1)/2=a;解这个方程即可。x=(1+sqrt(1+8a))/2.根号里面的1+8a必须是奇数且能被整除,否则就是impossible。

若a为0,考虑b和c,b和c其中一个不为零,则x=1;否则x=0;

 1类似。

求出0和1个数后就是构造了。

对了还有一个前提。如果b+c!=num0*num1也 是impossible。

举3 2 4 1的例子吧====可以求出0的个数num0=3个,1的个数num1=2个

假设先将0排在前面。

00011

根据c的大小将1往前移动。第一个1可以移动num0个位置,从而产生num0个“10"串。

while(c||num0)

 {
                    for(ll i=1;i<=c/num0;i++) printf("1"),num1--;
                    c=c-c/num0*num0;
                    num0--;
                    printf("0");
           }

即可。

最后将剩余的1全部输出。

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
int main()
{
	ll a,b,c,d;
	cin>>a>>b>>c>>d; 
	bool flag=1;
	if(a==0&&d==0&&b==0&&c==0) 
	{
		printf("impossible\n");
		return 0;
	}
	ll num,num1;
	if(a) num=1+8*a;
	else if(b||c)num=1;
	else num=0;
	ll sq=sqrt(num);
	if(sq*sq==num)
	{
		ll num0=(1+sq)/2;
		if(d)	num=1+8*d;
		else if(b||c)num=1;
		else num=0;
		sq=sqrt(num);
		if(sq*sq==num)
		{
			num1=(1+sq)/2;
			ll sum=num0*num1;
		//	printf("\n\nnum0:%lld num1:%lld b+c:%lld sum:%lld\n",num0,num1,b+c,sum);
			if(b+c==sum)
			{
				while(c||num0)
				{
					for(ll i=1;i<=c/num0;i++) printf("1"),num1--;
					c=c-c/num0*num0;
					num0--;
					printf("0");
				}
				while(num1) printf("1"),num1--;
			}
			else flag=0;
		}
		else flag=0;
	}
	else flag=0;
	if(!flag) printf("impossible");
	puts("");
}
/*
28 32 32 28
28 12 52 28

3 4 2 1
0 1 0 0
1 0 0 0
0 0 1 0
0 0 0 1
2 0 0 0
3 0 0 0
0 0 0 3
0 0 0 6
3 2 4 1

0 2 0 1
0 1 0 6
*/

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/88387313