codeforces 709D D. Recover the String(构造)
题目链接:
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串满足要求;
思路:
可以算出0和1的个数,每交换对0和1,那么01和10的个数就会一个加1和一个减1,所以01和10的和的值不变,构造就是1全在左边,0在右边,然后交换的次数就是01的个数了;然后输出来就好了;不过这题的wa点就是0和1的个数在0和1的这两种情况,反正我就是各种特判;
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=1e5+10; const int maxn=1e3+20; const double eps=1e-12; int check(int x) { int l=1,r=1000005; if(x==0)return 0; while(l<=r) { int mid=(l+r)>>1; if((LL)mid*(mid-1)<(LL)2*x)l=mid+1; else r=mid-1; } return l; } int main() { int a,b,c,d; read(a);read(b);read(c);read(d); int x=check(a),y=check(d); if(x==0&&(b||c))x=1; if(y==0&&(b||c))y=1; if((LL)x*(x-1)!=(LL)2*a||(LL)y*(y-1)!=(LL)2*d||(LL)x*y!=(LL)b+c)cout<<"Impossible\n"; else { if(x==0&&y==0)printf("0"); else if(x==0) { for(int i=1;i<=y;i++)printf("1"); } else if(y==0) { for(int i=1;i<=x;i++)printf("0"); } else { int t=b/y,f=y-b%y; for(int i=1;i<=t;i++)printf("0"),x--; for(int i=1;i<=f;i++)printf("1"),y--; if(x>0){printf("0");x--;} for(int i=1;i<=y;i++)printf("1"); for(int i=1;i<=x;i++)printf("0"); } } return 0; }