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:
-
the subsequence
00
occurs a times, -
the subsequence
01
occurs b times, -
the subsequence
10
occurs c times, and -
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 a, b, c, 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
题意:输出字符串满足有a个00,b个01,c个10,d个11。有多个则任意输出一个。没有则输出impossible。
思路:首先想到通过a和d来判断0和1的数量,n0 = sqrt(2*a)+1;n1 = sqrt(2*d)+1;然后判断n0*(n0-1)和2*a相不相等。如果a和d合理,则要判断n0*n1和(b+c)是否相等,不相等则不行,因为b+c一定是一个固定的值并且是n0*n1。如果存在字符串则可以先想象在n0个0里面插入n1个1;既然随便输出一个序列,则可以是f个在最前面的1和1个在m位之后的1和e个在最后的1;e = b/n0; m = b%n0;f = n1-e;if(m) f--;
比赛的时候前几次交题因为没有考虑到有a或者d为0时0和1的数量可以为0或者1的情况;一开始求n0的公式也有点问题,导致wa了很多遍,并且找样例浪费了很多时间。
代码是比赛时交的,所以很丑。。。。
#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
int main()
{
// freopen("in.txt", "r", stdin);
int a,b,c,d;
scanf("%d%d%d%d",&a,&c,&b,&d);
if(a==0&&d==0)
{
if(b+c>1)
{
printf("impossible\n");
return 0;
}
if(b+c==0)
{
printf("1\n");
return 0;
}
if(c==1) printf("01\n");
else printf("10\n");
return 0;
}
int n0,n1;
bool ok = true;
if(a==0||d==0)
{
if(a==0)
{
if(c+b==0)
n0=0;
else n0 = 1;
n1 = sqrt(2*d)+1;
if(n1*(n1-1)!=2*d) ok = false;
}
if(d==0)
{
if(c+b==0)
n1=0;
else n1 = 1;
n0 = sqrt(2*a)+1;
if(n0*(n0-1)!=2*a) ok = false;
}
if(n0*n1!=(b+c)) ok = false;
}
else
{
n0 = sqrt(2*a)+1;
n1 = sqrt(2*d)+1;
// cout<<n0<<" "<<n1<<endl;
if(n0*(n0-1)!=2*a) ok = false;
if(n1*(n1-1)!=2*d) ok = false;
if(n0*n1!=(b+c)) ok = false;
}
if(!ok||(!n0&&!n1))
{
printf("impossible\n");
return 0;
}
if(n0==0)
{
for(int i = 0; i<n1; i++) printf("1");
printf("\n");
return 0;
}
if(n1==0)
{
for(int i = 0; i<n0; i++) printf("0");
printf("\n");
return 0;
}
int f,e,m;
e = c/n0;
m = c%n0;
f = n1-e;
if(m) f--;
// cout<<e<<" "<<m<<" "<<f<<endl;
for(int i = 0; i<f; i++)
printf("1");
int j = 0;
if(m)
{
for(j = 0; j<m; j++)
printf("0");
printf("1");
for(; j<n0; j++) printf("0");
}
else
{
for(; j<n0; j++) printf("0");
}
for(int i = 0; i<e; i++) printf("1");
printf("\n");
return 0;
}
/**********************************************************************
Problem: 1416
User: multi2019team22
Language: C++
Result: AC
Time:12 ms
Memory:2036 kb
**********************************************************************/