For each string s consisting of characters ‘0’ and ‘1’ one can define four integers a00, 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
这场打的……岂一个惨字了得……
这题除了手残没别的问题……,,。。
意思是给出四个参数
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的数量,定义为
c0
和
c1
。
则一定满足
c0(c0−1)2==a00
c1(c1−1)2==a11
需要特判 a00 或 a11 为0的情况,此时0或1可以有1个或者没有。
先考虑一般情况,需要搞定的就是确定0和1的摆放顺序。
假定把1全放在右边,0全放在左边,那么
a01=0 a10=c0c1
然后我们在随意移动1,可以发现不管移动哪几个位置,不管当前状态如何,在合法移动的情况下,把1向右移动,如果右边是1不会影响,是0的话,一定会导致
a01++ a10−−
。
换句话说,如果能构出合法的01串的话,
a01和a10
的加和是固定的,为
c0∗c1
那么在上面等式成立的状态下,只要保证两个中的一个参数满足要求,另一个就一定满足要求。
那就按要求摆,用一个变量k表示剩余未摆的1的个数,那么如果当前位置放0, a01 就会增加k,看一下是否超出当前需要的01的个数,然后贪心摆就好。
至于 a00 或 a11 为0的情况,特殊搞一下就行了。BUT!赛时就WA在这了……手癌写反了。。。。。。。。。。。。。
代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define VI vector<int>
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;
char str[1111111];
int main()
{
//fread("in.in");
//fread("out.out");
int a,b,c,d;
int len = 0;
scanf("%d%d%d%d",&a,&b,&c,&d);
int k,r;
k = r = 1;
//找到c0 c1
while(k*(k-1)/2 < a) ++k;
while(r*(r-1)/2 < d) ++r;
if(k*(k-1)/2 != a || r*(r-1)/2 != d)
{
puts("Impossible");
return 0;
}
bool f = 1;
//a00 a11均为0
if(a == 0 && d == 0)
{
if(b == 0 && c == 0) puts("0");
else if(b == 0 && c == 1) puts("10");
else if(b == 1 && c == 0) puts("01");
else puts("Impossible");
return 0;
}
else if(a == 0)//a00为0 串中可有1个0或者没有
{
if(b == 0 && c == 0)
{
while(r--) str[len++] = '1';
}
else if(b+c == r)//10+01 == 1的个数 构成1111011这种
{
while(c--) str[len++] = '1';
str[len++] = '0';
while(b--) str[len++] = '1';
}
else f = 0;
}
else if(d == 0)//a11为0 串中可有1个1或者没有
{
if(b == 0 && c == 0)
{
while(k--) str[len++] = '0';
}
else if(b+c == k)//01+10 == 0的个数 构成0000100这种
{
while(b--) str[len++] = '0';
str[len++] = '1';
while(c--) str[len++] = '0';
}
else f = 0;
}
else
{
if(b+c == k*r)//a01+a10 == c0*c1
{
while(b)//优先构a01 (先构10也行
{
while(r > b)//剩余1的个数 > 剩余需要01的个数
{
str[len++] = '1';
--r;
}
str[len++] = '0';
--k;
b -= r;
}
//此时01已构完
while(r--) str[len++] = '1';
while(k--) str[len++] = '0';
}
else f = 0;
}
if(!f)
{
printf("Impossible");
return 0;
}
str[len] = 0;
puts(str);
return 0;
}