codeforces708BRecover the String+数学构造

本文介绍了一种基于给定的00、01、10、11子序列数量来重构原始01字符串的算法。该算法通过数学推导确定字符串中0和1的数量,并进一步构造出满足条件的字符串。文章提供了详细的实现思路及C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 long
int 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 0

1 2 0 0

6 0 0 0

0 0 0 6
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值