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 ofsubsequences 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 than1 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
题意:要你跟据00 01 10 11四种序列的个数构造一个01串(如果能)。
题解:可以跟据00 01 的数量得出0和1的数量,因为它们都应该符合n*(n-1)/2的形式,01 和 10数目之和为a11*a00,所以可以这么构造:
先把左边填满1右边填满0,然后交换1和0,这样10序列减少与01序列增加相等,总和保持不变;
wa点在于特殊情况,注意不符合构造前提,和有0的情况
代码
#include<bits/stdc++.h> #define MAXN 100005 using namespace std; char s[MAXN]; int main() { int a,b,c,d,n1=0,n0=0; scanf("%d%d%d%d",&a,&b,&c,&d); if(!b&&!c&&a&&d) { printf("Impossible"); return 0; } if(!a&&!b&&!c&&!d) { printf("1"); return 0; } for(int i=1;i*(i-1)<=2*a;i++) { if(i*(i-1)==2*a) n0=i; } for(int i=1;i*(i-1)<=2*d;i++) { if(i*(i-1)==2*d) n1=i; } if(!b&&!c) { if(d!=0&&n1!=0) for(int i=0;i<n1;i++) printf("1"); else if(a!=0&&n0!=0) for(int i=0;i<n0;i++) printf("0"); else printf("Impossible"); return 0; } if(!n1||!n0||n0*n1!=b+c) { printf("Impossible"); return 0; } int m=b/n0;int n=b%n0; int l=n==0?n1-m:n1-m-1; for(int i=0;i<l;i++) printf("1"); int flag=0; for(int i=0;i<n0;i++) { if(i==n&&n!=0&&!flag) { printf("1"); i--; flag=1; } else printf("0"); } for(int i=0;i<m;i++) printf("1"); return 0; }