CodeForces 708B Recover the String

构造。

根据$a[0][0]$可以求得$0$的个数$p$,根据$a[1][1]$可以求得$1$的个数$q$。 如果找不到$p$或$q$,那么就无解。

每一个$0$放到序列中的任何一个位置,假设和前面的$1$产生了$x$对$10$,和后面的$1$产生了$y$对$01$,那么$x+y$一定等于$q$。

也就是说如果$p*q$不等于$a[0][1]+a[1][0]$,那么就无解了。否则只要将$1$一个一个放进序列中,凑成$1$前面的$0$的个数总和是$a[0][1]$就可以了。

上面的方法对一般的情况都是成立的,对于三种特殊情况需要特判一下:$[1].$$0$ $0$ $0$ $0$   $[2].$$0$ $0$ $0$ $X$   $[3].$$X$ $0$ $0$ $0$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
}

LL p=-1,q=-1,a[2][2];
int pos[1000010];

int main()
{
    for(int i=0;i<2;i++) for(int j=0;j<2;j++) scanf("%lld",&a[i][j]);

    for(LL i=1;i*(i-1)<=2*a[0][0];i++)
        { if(i*(i-1)==2*a[0][0]) { p=i; break;} }

    for(LL i=1;i*(i-1)<=2*a[1][1];i++)
        { if(i*(i-1)==2*a[1][1]) { q=i; break;} }

    if(p==-1||q==-1) { printf("Impossible\n"); return 0; }

    if(a[0][0]==0&&a[0][1]==0&&a[1][0]==0&&a[1][1]==0) 
        { printf("0\n"); return 0; }

    if(a[0][0]==0&&a[0][1]==0&&a[1][0]==0&&a[1][1]!=0)
        {for(int i=1;i<=q;i++) printf("1"); printf("\n"); return 0;}

    if(a[0][0]!=0&&a[0][1]==0&&a[1][0]==0&&a[1][1]==0)
        { for(int i=1;i<=p;i++) printf("0"); printf("\n"); return 0; }

    if(p==-1||q==-1) printf("Impossible\n");
    else if(p+q>1000000) printf("Impossible\n");
    else if(p*q!=a[0][1]+a[1][0]) printf("Impossible\n");
    else
    {
        for(int i=1;i<=p;i++)
        {
            if(q>=a[1][0]) pos[a[1][0]]++,a[1][0]=0;
            else pos[q]++,a[1][0]=a[1][0]-q;
        }
        for(int i=1;i<=pos[0];i++)printf("0");
        for(int i=1;i<=q;i++)
        {
            printf("1");
            for(int j=1;j<=pos[i];j++) printf("0");
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2016-08-31 20:54  Fighting_Heart  阅读(264)  评论(0编辑  收藏  举报