http://codeforces.com/gym/101933/problem/J
Jumbled String
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output
Recall that a subsequence of a string is any string obtained by removing some subset of characters from the string, for instance "string", "sing", "i" and "sg" are all subsequences of "string". If the same subsequence can be obtained in exactly tt different ways, by removing different subsets of characters, we say that the subsequence occurs tt times.
Jingfei wants to create a nonempty bit string that has the following properties:
- the subsequence 00 occurs aa times,
- the subsequence 01 occurs bb times,
- the subsequence 10 occurs cc times, and
- the subsequence 11 occurs dd times.
However, Jingfei does not know how to create such a string – or whether it is even possible. Could you help her?
Input
The input consists of a single line with four integers aa, bb, cc, and dd (0≤a,b,c,d≤1090≤a,b,c,d≤109).
Output
Output a bit string that satisfies the given requirements. If there are several solutions, output any one of them. If there are no solutions, output "impossible".
Examples
input
Copy
3 4 2 1
output
Copy
01001
input
Copy
5 0 0 5
output
Copy
impossible
#include<bits/stdc++.h>
using namespace std;
using lom=long long;
lom a,b,c,d;
lom e[50000],f[100000];
lom fuck(){
lom sum=0;
for(lom i=2;;i++){
sum+=i-1;
e[i]=sum;
if(sum>1e9) return i;
}
}
int main(){
lom s=fuck(),x,y;
bool c1=0,c2=0;
cin>>a>>b>>c>>d;
for(lom i=1;i<s;i++){
if(a==e[i]){
c1=true;
x=i;
}
if(d==e[i]){
c2=true;
y=i;
}
}
lom t=0,k=0;
if(a==0&&b==0&&c==0&&d==0){
cout<<0<<endl;
}
else if(c1==0){
cout<<"impossible"<<endl;
}
else if(c2==0){
printf("impossible\n");
}
else if(a==0&&d==0){
if(b==1&&c==0){
cout<<0<<1<<endl;
}
else if(c==1&&b==0){
cout<<10<<endl;
}
else printf("impossible\n");
}
else if(b==0&&c==0){
if(a==0){
for(lom i=1;i<=y;i++) cout<<1;
cout<<endl;
}else if(d==0){
for(lom i=1; i<=x; i++) cout<<0;
cout<<endl;
}
else cout<<"impossible"<<endl;
}
else if(c1&&c2&&(b+c==x*y)){
for(lom i=0;i<x+y;i++){
if(i<x) f[i]=0;
else f[i]=1;
}
for(int i=x;i<x+y;i++){
if(t==c) break;
if(t+x<=c){
f[k]=1;
f[i]=0;
k++;
t=t+x;
}
else{
f[k+x-c+t]=1;
f[i]=0;
break;
}
}
for(lom i=0;i<x+y;i++) cout<<f[i];
cout<<endl;
}
else cout<<"impossible"<<endl;
return 0;
}