codeforces708BRecover the String+数学构造

来源:互联网 发布:sql删除表数据 编辑:程序博客网 时间:2024/06/07 08:09

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.
Input

The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn’t exceed 109.
Output

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.
Examples
Input

1 2 3 4

Output

Impossible

Input

1 2 2 1

Output

0110

让你还原一个01串,使得任意选2个,为“11”的个数有x11个,为“10”的个数有x10个,为“01”的个数有x01个,为“00”的个数有x00个。。。
首先可以由x11算出原串中1的个数,由x00算出原串中0的个数,那么n1*n0==x01+x01,等等一些列的条件判断出imposssible.注意0 0 0 0 的时候也成立为0,同理可以想到如果x00==0那么n0不一定就是0,也可以是1,所以直接判断x01和x10是否为0就可以判断出n0的个数。
之后知道n0,n1后,再来想构造。假设i位要填数字,它的后边要填m个1,那么我们想后边的x01的个数,如果x01

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<iostream>using namespace std;#define LL long longint getnumber(int x){    if(x==0) return 0;    int p=sqrt(2*x);    while(p*(p+1)<2*x) p++;    if(2*x==p*(p+1)) return p+1;    else return -1;}int main(){    int x00,x01,x10,x11;    //cout<<getnumber(4950);    scanf("%d %d %d %d",&x00,&x01,&x10,&x11);    int number0=getnumber(x00);    int number1=getnumber(x11);    if(x00==0&&x01==0&&x10==0&&x11==0){        printf("0\n");        return 0;    }    if(number0==0&&(x01!=0||x10!=0)) number0++;    if(number1==0&&(x01!=0||x10!=0)) number1++;    if(number0==-1||number1==-1||number0*number1!=x10+x01||number0+number1==0){        printf("Impossible\n");        return 0;    }    int n=number0+number1;    for(int i=0;i<n;i++){        if(x01>=number1){            printf("0");            x01-=number1;            number0--;        }        else{            printf("1");            x10-=number0;            number1--;        }    }    printf("\n");    return 0;}/*0 0 0 01 2 0 06 0 0 00 0 0 6*/
0 0