B. Recover the String
1 second
256 megabytes
standard input
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.
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
思路:根据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的更多相关文章
- codeforces 709D D. Recover the String(构造)
题目链接: D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input s ...
- Recover the String
Recover the String 题目链接:http://codeforces.com/contest/709/problem/D 构造 这题乍一看很难构造,但是如果知道了整个字符串中'0'和'1 ...
- 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 ...
- 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 ...
- 【CodeForces】708 B. Recover the String 数学构造
[题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...
- 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 ...
- CF708B Recover the String 构造
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 an ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- CodeForces 708B Recover the String
构造. 根据$a[0][0]$可以求得$0$的个数$p$,根据$a[1][1]$可以求得$1$的个数$q$. 如果找不到$p$或$q$,那么就无解. 每一个$0$放到序列中的任何一个位置,假设和前面的 ...
随机推荐
- Docker Swarm的命令
初始化swarm manager并制定网卡地址docker swarm init --advertise-addr 192.168.10.117 强制删除集群docker swarm leave -- ...
- (转载)SQL Server 2008 连接JDBC详细图文教程
点评:SQL Server 2008是目前windows上使用最多的sql数据库,2008的安装机制是基于framework重写的,特点是非常耗时间SQL Server 2008是目前windows上 ...
- 前端2 — CSS — 更新完毕
1.CSS是什么? 指:Cascading Style Sheet --- 层叠样式表 CSS 即:美化网页( 在HTML不是说过W3C规定网页为三种标准嘛,结构层HTML已经玩了,而这个CSS就是 ...
- 学习java的第五天
一.今日收获 1.java程序设计完全手册第一章节的小总结 2.完成了部分例题验证解答. 二.今日难题 1.java语言与c++语言的不同点. 2.有例题验证不出来 三.明日目标 1.复习java程序 ...
- Scala和Java的List集合互相转换
import java.util import scala.collection.mutable /** * 集合互相转换 */ object ScalaToJava { def main(args: ...
- HTTP 之 options预请求
一.HTTP一共有八种常见请求方法 get:参数在url上,浏览器长度有限制,不安全 post:参数不可见,长度不受限制 put:上传最新内容到指定位置 delete:删除请求的url所表示的资源 h ...
- Ecshop 后台管理员密码忘记了吧~!~!~!
方法1:把下面的代码保存为文件 mima.php <?php define('IN_ECS', true); require(dirname(__FILE__) . '/includes/ini ...
- java poi导出多sheet页
/** * @Title: exportExcel * @Description: 导出Excel的方法 * @param workbook * @param sheetNum (sheet的位置,0 ...
- final&static
final 1.final修饰类,那么该类不能有子类,那么也就没有子类重写父类的方法,也就没有多态 2.final修饰成员变量,那么成员变量要么显式赋值(用第一种),要么在构造方法中赋值 无论哪一种, ...
- Linux学习 - 系统命令sudo权限
1 功能 root把超级用执行的命令赋予普通用户执行 2 使用 visudo 或 vim /etc/sudoers 说明: root 用户名 ALL=(ALL) 被管理主机的地址=(可使用的身份) A ...