【题目】B. Recover the String

【题意】找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在。数字<=10^9,答案<=10^6。

【算法】数学构造

【题解】首先由a1和a4易得0的数量x0和1的数量x1。

容易发现01和10关系密切,令1的位置为b1...bx1,则:

{0,1} (b1-1)+(b2-2)+(b3-3)+...+(bx1-x1)=a2

{1,0} (x0-b1+1)+(x0-b2+1)+...+(x0-bx1+x1)=a3

两式相加,得x0*x1=a2+a3。

因此,只要满足表达式x0*x1=a2+a3,串就一定存在,此时只要随便构造b[]使得{0,1}数量符合,那么{1,0}数量就一定随之符合。

注意特殊处理0的情况。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define ll long long
#define lowbit(x) x&-x
using namespace std;
int read(){
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a<b?b:a;}
int ab(int x){return x>?x:-x;}
//int MO(int x){return x>=MOD?x-MOD:x;}
//void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
/*------------------------------------------------------------*/
const int inf=0x3f3f3f3f; int n,a1,a2,a3,a4,x0,x1,b[],c[];
void p(){printf("Impossible");exit();}
int main(){
scanf("%d%d%d%d",&a1,&a2,&a3,&a4);
for(int i=;i>=;i--){
if(1ll*i*(i-)/==a1)x0=i;
if(1ll*i*(i-)/==a4)x1=i;
}
if(!x0||!x1)p();
if(a2+a3==){
if(x0==){
for(int i=;i<=x1;i++)printf("");
return ;
}
if(x1==){
for(int i=;i<=x0;i++)printf("");
return ;
}
}
if(x0*x1!=a2+a3)p();
for(int i=;i<=x1;i++)b[i]=i;
for(int i=x1;i>=;i--){
if(a2>x0)b[i]=x0+i,a2-=x0;else{b[i]=a2+i;break;}
}
for(int i=;i<=x1;i++)c[b[i]]=;
for(int i=;i<=x1+x0;i++)printf("%d",c[i]);
return ;
}

【CodeForces】708 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. 【codeforces 709D】Recover the String

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

  3. Codeforces 715A. Plus and Square Root[数学构造]

    A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. codeforces 509 D. Restoring Numbers(数学+构造)

    题目链接:http://codeforces.com/problemset/problem/509/D 题意:题目给出公式w[i][j]= (a[i] + b[j])% k; 给出w,要求是否存在这样 ...

  5. 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 ...

  6. Recover the String

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

  7. 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 ...

  8. CodeForces 297C Splitting the Uniqueness (脑补构造题)

    题意 Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指 ...

  9. 浮点数运算结果不精确,以及用String来构造BigDecimal进行浮点数精确计算

    1.浮点数运算结果不精确 先看如下代码 System.out.println(1.0 - 0.8); System.out.println(0.2 + 0.1); System.out.println ...

随机推荐

  1. 使用kdump内核调试工具遇到的问题及解决

    修改linux内核代码或者内核模块的时候,搞不好就会造成linux死机崩溃,crash死机后/var/log/kern.log里面不会有任何异常信息记录.这时候kdump就会派上用场了,网上kdump ...

  2. Windows下基于http的git服务器搭建-gitstack

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Windows下基于http的git服务器搭建-gitstack     本文地址:http: ...

  3. django amdin页面下拉列表的外键增加条件过滤

    class MenuList(admin.ModelAdmin): list_display = ('menuId', 'menuName', 'menuDesc', 'menuType', 'sor ...

  4. 微信小程序测试的策略和注意事项

    一.测试前准备(环境搭建) 1.前端页面 微信Web开发者工具安装.授权测试用的微信号可预览和调试小程序...可参考此文: 微信Web开发者工具-下载.安装和使用图解 2.管理后台 配置内网测试服务器 ...

  5. Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/

    转自:http://blog.csdn.net/zzq123686/article/details/77454066 出现错误信息: Reading package lists... Done E:  ...

  6. 规则引擎之easyRules

    规则引擎听起来是蛮高深的一个词语,但透过现象看本质,Martin Fowler 有如下言: You can build a simple rules engine yourself. All you ...

  7. CDN加速-内容分发网络

    内容分发网络 (互联网技术) 编辑 CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输 ...

  8. jmeter 配置元件之计数器Counter

    用jmeter生成数据 我用过几种以下几种方法 1.CSV Data Set Config  参数化 2.${_Random} ${_Random}是jmeter函数助手里面自带的一个函数,作用是返回 ...

  9. 对ViewModel自定义约束

    有时候我们常要对一些属性进行自定义的约束,可以这么做 using ListSys.Entity; using System; using System.Collections; using Syste ...

  10. 第99天:CSS3中透视perspective

    CSS3中透视perspective 透视原理: 近大远小 . 浏览器透视:把近大远小的所有图像,透视在屏幕上. 理解浏览器的坐标系:浏览器平面为 Z=0的平面,坐标原点默认为图片的中心,可以通过更改 ...