Jumbled String (Kattis - jumbledstring)(思维题)
Problem
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 t different ways, by removing different subsets of characters, we say that the subsequence occurs t times.
Jingfei wants to create a nonempty bit string that has the following properties:
the subsequence 00 occurs a times,
the subsequence 01 occurs b times,
the subsequence 10 occurs c times, and
the subsequence 11 occurs d 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 a, b, c, and d (0≤a,b,c,d≤1e9).
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”.
Sample Input 1 | Sample Output 1 |
---|---|
3 4 2 1 |
01001 |
Sample Input 2 | Sample Output 2 |
---|---|
5 0 0 5 |
impossible |
题解 :不需要考虑00、11,根据a个00和d个11来算出来需要的0的个数x,1的个数y。
找答案字符串时,凑01,这样10也满足条件。
设q = b / y,w = b % y。这样先输出时,输出q个0,输出y-w个1,这样就保证了有q*(y-w)个01,如果w == 0,表示刚好能够用上所有的1来组成01,再把剩余的输出1和0就可以了。但是如果q != x,即不需要把0全部输出,那样q*(y-w)> b,所以把0剩余提前到1前面,eg:现在有3个0,3个1,我们只需要7个01,按着这种想法,如果输出001110(代码如果没考虑)或者是000111,这样就会多答案或者少答案,所以应该是001101,所以要判断一下余下的,把0调前一个,让正好能够是7个01就可以了,相应的最后的输出0个数要-1。
其次加上特判,a、b、c、d分别为0和部分为0的时候的特判。(自己好菜。。%队友)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll a,b,c,d,abc;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
if(a==0&&b==0&&c==0&&d==0)
{
printf("0\n");
return 0;
}
ll x,y,i;
x = (1 + sqrt(1+8*a)) / 2;
if(x*(x-1)/2 != a)
{
printf("impossible\n");
return 0;
}
y = (1 + sqrt(1 + 8*d)) / 2;
if(y*(y-1)/2 != d)
{
printf("impossible\n");
return 0;
}
if(a == 0&&d == 0)
{
if(b == 1&&c == 0)
{
printf("0");
printf("1\n");
}
else if(c == 1&&b==0)
{
printf("1");
printf("0\n");
}
else printf("impossible\n");
return 0;
}
if(b==0&&c==0)
{
if(a==0)
{
for(i=1;i<=y;i++) printf("1");
printf("\n");
}
else if(d==0)
{
for(i=1;i<=x;i++) printf("0");
printf("\n");
}
else printf("impossible\n");
return 0;
}
if(x*y != b+c)
{
printf("impossible\n");
return 0;
}
ll j,q,w;
q = b / y;
w = b % y;
for(i=1;i<=q;i++) printf("0");
for(i=1;i<=y-w;i++) printf("1");
if(q!=x)
printf("0");
for(i=1;i<=w;i++) printf("1");
for(i=1;i<=x-q-1;i++) printf("0");
return 0;
}
Jumbled String (Kattis - jumbledstring)(思维题)的更多相关文章
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- CodeForces - 631C ——(思维题)
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- hdu5325 树的思维题
pid=5325">http://acm.hdu.edu.cn/showproblem.php? pid=5325 Problem Description Bobo has a tre ...
- 【交互 细节题 思维题】cf1064E. Dwarves, Hats and Extrasensory Abilities
第一次做交互真有趣……:挺好的细节思维题 This is an interactive problem. In good old times dwarves tried to develop extr ...
- 思维题+set URAL 1718 Rejudge
题目传送门 /* 题意:数据加10组,再删掉第6组数据,问rejudge后最少最多几个作者收到邮件 思维题:当错在6时结果是不一定,错在7时是一定改变,因为会变成6 思路没错,但用结构题排序一直WA, ...
- 思维题 UVA 10881 Piotr's Ants
题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...
- 洛谷 P4749 - [CERC2017]Kitchen Knobs(差分转换+dp,思维题)
题面传送门 一道挺有意思的思维题. 首先有一个 obvious 的结论,就是对于每个炉子,要么转到哪里都符合条件,要么存在唯一的最大值.对于转到哪儿都符合条件的炉子我们 duck 不必考虑它,故我们只 ...
随机推荐
- js 根据 数组条件 简单查询的方法临时保存
let array = [{ date: '2016-05-02', name: 'Ethan', status: 'success', total: '81' }, { date: '2016-05 ...
- win7+cuda+anaconda python+tensorflow-gpu+keras安装成功版本匹配汇总
win7+cuda+anaconda python+tensorflow-gpu+keras安装成功版本匹配汇总 2019-09-20 15:06:03 wyx100 阅读数 38更多 分类专栏: M ...
- 怎样理解script标签的defer属性和async属性
如果script标签是引用的外部js文件, 那就会有一个下载js文件这一过程, 为了不因为这个下载过程而阻塞页面解析与渲染, 我们需要一种机制来解决这一问题, 方法之一就是使用 defer和async ...
- Autofac 使用经验
慢慢总结 基础使用样例,在 Application_Start 中直接使用 //autofac注册 var builder = new ContainerBuilder(); //注册Controll ...
- c#基于TCP/IP、CIP协议的欧姆龙PLC通信
一.关于CIP协议 CIP通信是Common Industrial Protocl(CIP)的简称,它是一个点到点的面向对象协议,能够实现工业器件(传感器,执行器)之间的连接,和高等级的控制器之间的连 ...
- springboot启动流程(四)application配置文件加载过程
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 触发监听器加载配置文件 在上一篇文章中,我们看到了Environment对象的创建方法.同时也 ...
- Oracle学习笔记:update的字段中包括单引号
平时update的时候直接更改字段内的值,例如: update table_temp set name = 'Hider' where id = 100; 但更新后的值中包括单引号,则不能按以上方式进 ...
- stm32 窗口看门狗 WWDG
窗口看门狗WWDG其实和独立看门狗类似,它是一个7位递减计数器不断的往下递减计数,当减到一个固定值0x40时还不喂狗的话,产生一个MCU复位,这个值叫窗口的下限,是固定的值,不能改变 窗口看门狗(WW ...
- Input system (输入子系统)
Input system (输入子系统) 以前写一些输入设备(键盘,鼠标等)的驱动都是字符设备,混杂设备处理的,linux开源社区的大神门看到了这大量的输入设备如此分散不堪,就想有木有一种机制,可以对 ...
- 【接口自动化】mock
mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法. 1.在测试接口时使用mock #from unittest import mock d ...