B. Recover the String
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

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

思路:根据a[0]和a[3],我们可以解出0,1的个数x1,x2,开始先按照前面全是0后面全是1来排列这个时候我们可以知道01的个数就是x1*x2,然后我们发现当0向右移位的时候,没超过一个1的时候01个数少1,10个数多1,那么我们知道,01和10的个数之和就是x1*x2,这样可以判断是否有解,然后就是到底咋移,因为可以一个一个增加,所以贪心,每次移一个,然后移动具体看代码;

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<set>
8 #include<math.h>
9 #include<vector>
10 using namespace std;
11 typedef long long LL;
12 LL a[5];
13 short int ans[1000005];
14 bool check(LL n);
15 int main(void)
16 {
17 while(scanf("%I64d",&a[0])!=EOF)
18 {
19 int i,j;
20 for(i = 1; i < 4; i++)
21 {
22 scanf("%I64d",&a[i]);
23 }
24 LL x1 = 1+8*a[0];
25 LL x2 = 1+8*a[3];
26 bool fla1 = check(x1);
27 bool fla2 = check(x2);
28 if(!fla1||!fla2)
29 {
30 //printf("1\n");
31 printf("Impossible\n");
32 }
33 else
34 {
35 if(a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0)
36 {
37 printf("1\n");
38 }
39 else if(a[0]!=0&&a[1]==0&a[2]==0&&a[3]==0)
40 {
41 LL cnt = 1+sqrt(1+8*a[0]);
42 cnt/=2;
43 for(i = 0; i <cnt ; i++)
44 printf("0");
45 printf("\n");
46 }
47 else if(a[0]==0&&a[1]==0&&a[2]==0&&a[3]!=0)
48 {
49 LL cnt = sqrt(1+8*a[3])+1;
50 cnt/=2;
51 for(i = 0 ; i < cnt ; i++)
52 printf("1");
53 printf("\n");
54 }
55 else
56 {
57 x1 = 1 + sqrt(x1);
58 x1/=2;
59 x2 = 1 + sqrt(x2);
60 x2/=2;
61 fill(ans,ans+1000005,1);
62 LL sum1 = x1*x2;
63 LL sum2 = 0;
64 if(sum1 != a[1]+a[2])
65 printf("Impossible\n");
66 else
67 {
68 LL ac = a[2];
69 LL ak = ac/x2;
70 LL tt = ac%x2;
71 LL nn = x1 + x2;
72 if(tt) ak++;
73 for(i = 0; i < (x1-ak); i++)
74 {
75 printf("0");
76 }
77 if(tt == 0)
78 {
79 for(i = 0; i < x2 ; i++)
80 printf("1");
81 }
82 else
83 {
84 for(i = 0; i < x2 ; i++)
85 {
86 if(i == tt)
87 printf("0");
88 else printf("1");
89 }
90 printf("1");
91 }
92 if(tt)ak--;
93 for(i = 0 ; i < ak ; i++)
94 {
95 printf("0");
96 }
97 printf("\n");
98 }
99 }
100 }
101 }
102 return 0;
103 }
104 bool check(LL n)
105 {
106 LL ap = sqrt(1.0*n);
107 if(ap*ap == n)
108 return true;
109 return false ;
110 }

B. Recover the String的更多相关文章

  1. codeforces 709D D. Recover the String(构造)

    题目链接: D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. Recover the String

    Recover the String 题目链接:http://codeforces.com/contest/709/problem/D 构造 这题乍一看很难构造,但是如果知道了整个字符串中'0'和'1 ...

  3. AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)

    D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. AIM Tech Round 3 (Div. 1) B. Recover the String 构造

    B. Recover the String 题目连接: http://www.codeforces.com/contest/708/problem/B Description For each str ...

  5. 【CodeForces】708 B. Recover the String 数学构造

    [题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...

  6. codeforces708b// Recover the String //AIM Tech Round 3 (Div. 1)

    题意:有一个01组成的串,告知所有长度为2的子序列中,即00,01,10,11,的个数a,b,c,d.输出一种可能的串. 先求串中0,1的数目x,y. 首先,如果00的个数a不是0的话,设串中有x个0 ...

  7. CF708B Recover the String 构造

    For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 an ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. CodeForces 708B Recover the String

    构造. 根据$a[0][0]$可以求得$0$的个数$p$,根据$a[1][1]$可以求得$1$的个数$q$. 如果找不到$p$或$q$,那么就无解. 每一个$0$放到序列中的任何一个位置,假设和前面的 ...

随机推荐

  1. 使用Rainbond实现离线环境软件交付

    一.离线交付的痛点 在传统行业,如政府.能源.军工.公安.工业.交通等行业,为了防止数据泄露和运行安全考虑,一般情况下网络会采取内外网隔离的策略,以防范不必要的风险,毕竟在安全防护方面,网络物理隔离是 ...

  2. 业务逻辑审批流、审批流、业务、逻辑、面向对象、工作方式【c#】

    ------需求分析:--------1.先按照实际线下流程说这是什么事情,实际要干什么.2.再转换为面向对象-页面的操作流程,演示demo3.再与相关人员沟通是否可行需要什么地方修正.4.最终:线上 ...

  3. 学习java的第十七天

    一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...

  4. 日常Java 2021/11/3

    java网络编程 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来.java.net包中J2SE的APl包含有类和接口,它们提供低层次的通信细节.你可以直接使用这些类和接口, ...

  5. django中的filter(), all(), get()

    1. 类名.objects中的get(), filter(), all() 的区别 结论: (1)all()返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使 ...

  6. 商业爬虫学习笔记day7-------解析方法之bs4

    一.Beautiful Soup 1.简介 Beautiful Soup 是python的一个库,最主要的功能是从网页抓取数据.其特点如下(这三个特点正是bs强大的原因,来自官方手册) a. Beau ...

  7. 如何从 100 亿 URL 中找出相同的 URL?

    题目描述 给定 a.b 两个文件,各存放 50 亿个 URL,每个 URL 各占 64B,内存限制是 4G.请找出 a.b 两个文件共同的 URL. 解答思路 每个 URL 占 64B,那么 50 亿 ...

  8. RecyclerView实现侧滑删除、置顶、滑动

    1.首先在build.gradle里添加 compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.1' 2.设置recyclerView的item布 ...

  9. error信息

    /opt/hadoop/src/contrib/eclipse-plugin/build.xml:61: warning: 'includeantruntime' was not set, defau ...

  10. NSURLSession下载文件-代理

    - 3.1 涉及知识点(1)创建NSURLSession对象,设置代理(默认配置) ```objc //1.创建NSURLSession,并设置代理 /* 第一个参数:session对象的全局配置设置 ...