D. Recover the String
1 second
256 megabytes
standard input
standard output
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 exceed109.
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
比赛时,一直纠结怎么判断Impossible和构造字符串,胡乱交了一发,wa了,好SB。
当然先是利用a00和a11求1和0的字符串个数g1和g0,如果a00或a11等于1,这时候还要根据a10和a01判断a00或者a11是等于1还是0.
在求完a00和a11的个数后,判断合不合理,C(g0,2)=a00, C(g1,2)=a11, C(g1+g0,2) = a00+a01+a10+a11即成立。
然后就是构造字符串了。
设初始的字符串个数为0000001111111......
然后就是贪心的构造字符串了。考虑该输出某一位时,如果在这一位时,a10>=g0,那么我可以把1挪到最前面,输出1,这时候后面剩下的字符串需要构造的a10-=g0,剩余的1的个数g1--。否则直接输出0,后面剩余字符串需要构造a01-=g1,剩余的0的个数g0--.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
int main()
{
ll a00,a01,a10,a11;
scanf("%I64d%I64d%I64d%I64d",&a00,&a01,&a10,&a11);
ll g0,g1;
g0 = (+sqrt(+*a00))/;
g1 = (+sqrt(+*a11))/;
if(a00+a01+a10+a11==)
{
printf("0\n");
return ;
}
if(g0==||g1==)
{
if(g0==)
{
if(a01||a10) g0 = ;
else g0 = ;
}
if(g1==)
{
if(a01||a10) g1 = ;
else g1 = ;
}
}
if(g0*(g0-)!=*a00||g1*(g1-)!=*a11)
{
printf("Impossible\n");
return ;
}
ll gz = g0+g1;
if(gz*(gz-)/!=(a00+a01+a10+a11))
{
printf("Impossible\n");
return ;
}
while(g0+g1)
{
if(a10>=g0)
{
printf("");
a10 -= g0;
g1--;
}
else
{
printf("");
a01 -= g1;
g0--;
}
}
printf("\n");
return ;
}