For each string s consisting of characters '0' and '1' one can define four integersa00, a01, a10 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 a00, a01, a10, a11 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.
The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109.
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.
1 2 3 4
Impossible
1 2 2 1
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("");
}