「CodeForces 476A」Dreamoon and Stairs
Dreamoon and Stairs
题意翻译
题面 DM小朋友想要上一个有 \(n\) 级台阶的楼梯。他每一步可以上 \(1\) 或 \(2\) 级台阶。假设他走上这个台阶一共用了 \(x\) 步。现在DM想知道 \(x\) 是否可能为 \(m\) 的倍数。如果可能,输出 \(x\) 的最小值。如果不可能,输出 \(-1\)
输入 两个正整数 \(n,m (n \le 10000,m \le 10)\)
输出 按要求输出 \(x\) 或 \(-1\)
题目描述
Dreamoon wants to climb up a stair of nn steps. He can climb \(1\) or \(2\) steps at each move. Dreamoon wants the number of moves to be a multiple of an integer \(m\) .
What is the minimal number of moves making him climb to the top of the stairs that satisfies his condition?
输入输出格式
输入格式:
The single line contains two space separated integers \(n\) , \(m\) (\(0<n \le 10000,1<m \le 10\)).
输出格式:
Print a single integer — the minimal number of moves being a multiple of \(m\) . If there is no way he can climb satisfying condition print \(-1\) instead.
输入输出样例
输入样例#1:
10 2
输出样例#1:
6
输入样例#2:
3 5
输出样例#2:
-1
说明
For the first sample, Dreamoon could climb in 6 moves with following sequence of steps: {2, 2, 2, 2, 1, 1}.
For the second sample, there are only three valid sequence of steps {2, 1}, {1, 2}, {1, 1, 1} with 2, 2, and 3 steps respectively. All these numbers are not multiples of 5.
一句话题意
给定一个n级的台阶,开始时在第0级,每次可以向上爬1级或2级,问最少要爬多次才能爬到顶,而且爬的次数是m的倍数。
思路
很明显,爬完这个台阶的最多步数是n(每次爬1层),最少步数 \(\frac{n - 1}2 + 1\) (等价于 \(\frac n2 + n \% 2\)) (每次爬2层,如果层数是奇数,那再爬1层),并且在( \(\frac{n - 1}2 + 1\) ) ~ n 之间都可以到达。
所以只要选取( \(\frac{n - 1}2 + 1\) ) ~ n 之间最小的能被m整除的数即可。
当然,这道题还可以用DP解决,那比较费时间,比较费空间,也比较难调试(谁愿意呢),所以这里不再赘述。
代码
下面给出两种写法——
写法一
比较保险的O(\(\frac nm\))算法(我模拟赛时就用这种的)
#include<cstdio>
using namespace std;
int n, m;
int ans;
int main(){
scanf( "%d%d", &n, &m );
while( ans < n / 2 + n % 2 ) ans += m;
if ( ans > n ) ans = -1;
printf( "%d\n", ans );
return 0;
}
写法二
比较有风险的O(1)算法(就怕出错,有hack数据的请联系我)
#include<cstdio>
using namespace std;
int n, m;
int ans;
int main(){
scanf( "%d%d", &n, &m );
i if ( n == 0 ) printf( "0\n" );
ans = ( ( n / 2 + n % 2 - 1 ) / m + 1 ) * m;
if ( ans == 0 || ans > n ) ans = -1;
printf( "%d\n", ans );
return 0;
}
「CodeForces 476A」Dreamoon and Stairs的更多相关文章
- 「CodeForces 581D」Three Logos
BUPT 2017 Summer Training (for 16) #3A 题意 给你三个矩形,需要不重叠不留空地组成一个正方形.不存在输出-1,否则输出边长和这个正方形(A,B,C表示三个不同矩形 ...
- 「CodeForces - 50C 」Happy Farm 5 (几何)
BUPT 2017 summer training (16) #2B 题意 有一些二维直角坐标系上的整数坐标的点,找出严格包含这些点的只能八个方向走出来步数最少的路径,输出最少步数. 题解 这题要求严 ...
- 「CodeForces - 598B」Queries on a String
BUPT 2017 summer training (for 16) #1I 题意 字符串s(1 ≤ |s| ≤ 10 000),有m(1 ≤ m ≤ 300)次操作,每次给l,r,k,代表将r位置插 ...
- 「CodeForces - 717E」Paint it really, really dark gray (dfs)
BUPT 2017 summer training (for 16) #1H 题意 每个节点是黑色or白色,经过一个节点就会改变它的颜色,一开始在1节点.求一条路径使得所有点变成黑色. 题解 dfs时 ...
- 「CodeForces 546B」Soldier and Badges 解题报告
CF546B Soldier and Badges 题意翻译 给 n 个数,每次操作可以将一个数 +1,要使这 n 个数都不相同, 求最少要加多少? \(1 \le n \le 3000\) 感谢@凉 ...
- 「Codeforces 79D」Password
Description 有一个 01 序列 \(a_1,a_2,\cdots,a_n\),初始时全为 \(0\). 给定 \(m\) 个长度,分别为 \(l_1\sim l_m\). 每次可以选择一个 ...
- 「Codeforces 468C」Hack it!
Description 定义 \(f(x)\) 表示 \(x\) 的各个数位之和.现在要求 \(\sum_{i=l}^rf(i)\bmod a\). 显然 ans=solve(l,r)%a; if(a ...
- 「Codeforces 724F」Uniformly Branched Trees
题目大意 如果两棵树可以通过重标号后变为完全相同,那么它们就是同构的. 将中间节点定义为度数大于 \(1\) 的节点.计算由 \(n\) 个节点,其中所有的中间节点度数都为 \(d\) 的互不同构的树 ...
- 「codeforces - 1284G」Seollal
给定 \(n\times m\) 的网格图,有些格子有障碍,无障碍且相邻的格子之间连边形成图.保证 \((1, 1)\) 无障碍,保证无障碍格子连通. 将网格图黑白染色,相邻格子颜色不同,\((1, ...
随机推荐
- 当flex遇到white-space: nowrap; 排版就飞了的神奇问题 吐血
在做一个移动端排班的时候需要某一行的字超出的部分用省略号代替 然后写着发现后面排版乱了 HTML结构如下 我想让第二个span的内容加省略号 css正确的代码如下 .list { width: 1 ...
- uva 11806 Cheerleaders (容斥)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- LRJ-Example-06-02-Uva514
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<stack> using namespace std; + ...
- 获取checkbox返回值
<div class="checkbox"> <label> <input type="checkbox" value=" ...
- ifram子页面与父页面的方法相互调用
parent.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- 最小生成树prim、
过年那几天确实没好好学习.在老家闲着也是闲着.可是就是没看书. 回来这几天又一直在弄个人博客.买域名云服务器备案什么的- -. 麻烦死了呢. 在腾讯花1块钱备案了一个网站www.goodgoodstu ...
- java分配内存空间
分配内存空间 数组名=new 数据类型[数组长度]: new关键字用来实现为数组或对象分配内存 (堆内存) 数组具有固定的长度.获取数组的长度: 数组名.length 定义数组+分配内存空间 数据类型 ...
- python3在pycharm中为什么导入random模块不能用? TypeError: 'module' object is not callable
新手学python求大神指导,也用sys导入了random.py的路径,仍然不行. 刚刚排错貌似找到了问题的原因...那是因为我在pycharm中新建的python文件名就是random,所以当前目录 ...
- C# 操作XML学习笔记
1. Customers.xml <?xml version="1.0" encoding="utf-8"?> <cust:customers ...
- 符合阿里巴巴代码规范的checkstyle检测文件
一.安装与简介 eclipse和idea都有对应的插件,找到插件安装界面.搜索checkstyle,点击安装后,重启IDE即可.(网上有很多安装教程,就不重复制造轮子了) 二.导入配置文件 在chec ...