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的更多相关文章

  1. 「CodeForces 581D」Three Logos

    BUPT 2017 Summer Training (for 16) #3A 题意 给你三个矩形,需要不重叠不留空地组成一个正方形.不存在输出-1,否则输出边长和这个正方形(A,B,C表示三个不同矩形 ...

  2. 「CodeForces - 50C 」Happy Farm 5 (几何)

    BUPT 2017 summer training (16) #2B 题意 有一些二维直角坐标系上的整数坐标的点,找出严格包含这些点的只能八个方向走出来步数最少的路径,输出最少步数. 题解 这题要求严 ...

  3. 「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位置插 ...

  4. 「CodeForces - 717E」Paint it really, really dark gray (dfs)

    BUPT 2017 summer training (for 16) #1H 题意 每个节点是黑色or白色,经过一个节点就会改变它的颜色,一开始在1节点.求一条路径使得所有点变成黑色. 题解 dfs时 ...

  5. 「CodeForces 546B」Soldier and Badges 解题报告

    CF546B Soldier and Badges 题意翻译 给 n 个数,每次操作可以将一个数 +1,要使这 n 个数都不相同, 求最少要加多少? \(1 \le n \le 3000\) 感谢@凉 ...

  6. 「Codeforces 79D」Password

    Description 有一个 01 序列 \(a_1,a_2,\cdots,a_n\),初始时全为 \(0\). 给定 \(m\) 个长度,分别为 \(l_1\sim l_m\). 每次可以选择一个 ...

  7. 「Codeforces 468C」Hack it!

    Description 定义 \(f(x)\) 表示 \(x\) 的各个数位之和.现在要求 \(\sum_{i=l}^rf(i)\bmod a\). 显然 ans=solve(l,r)%a; if(a ...

  8. 「Codeforces 724F」Uniformly Branched Trees

    题目大意 如果两棵树可以通过重标号后变为完全相同,那么它们就是同构的. 将中间节点定义为度数大于 \(1\) 的节点.计算由 \(n\) 个节点,其中所有的中间节点度数都为 \(d\) 的互不同构的树 ...

  9. 「codeforces - 1284G」Seollal

    给定 \(n\times m\) 的网格图,有些格子有障碍,无障碍且相邻的格子之间连边形成图.保证 \((1, 1)\) 无障碍,保证无障碍格子连通. 将网格图黑白染色,相邻格子颜色不同,\((1, ...

随机推荐

  1. @codechef - TREEPATH@ Decompose the Tree

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵无根树,每个节点上都写了一个整数. 你的任务就是统计有多 ...

  2. sqlserver 序号重新计算

    DBCC CHECKIDENT('leshua_TradeData',NORESEED) DBCC CHECKIDENT('表名',NORESEED)

  3. 谷歌BERT预训练源码解析(一):训练数据生成

    目录预训练源码结构简介输入输出源码解析参数主函数创建训练实例下一句预测&实例生成随机遮蔽输出结果一览预训练源码结构简介关于BERT,简单来说,它是一个基于Transformer架构,结合遮蔽词 ...

  4. [kuangbin带你飞]专题九 连通图D - Network POJ - 3694

    这道题其实也非常简单,只是在求割边及其个数的情况下,每次往里面加入新的边,并再次计算割边的个数. 我们用tarjan可以求出原图的桥以及个数,当然我们不能暴力加边,然后求解,那么如何求呢??? 其实非 ...

  5. H3C 单播与广播

  6. php页面最大执行时间 set_time_limit函数不起作用

      作者: default|标签:PHP set_time_limit 执行时间|2017-3-21 15:03   set_time_limit 不生效或者无效解决方法 <?php globa ...

  7. python3在pycharm中为什么导入random模块不能用? TypeError: 'module' object is not callable

    新手学python求大神指导,也用sys导入了random.py的路径,仍然不行. 刚刚排错貌似找到了问题的原因...那是因为我在pycharm中新建的python文件名就是random,所以当前目录 ...

  8. Hex编码

    编码原理 Hex编码就是把一个8位的字节数据用两个十六进制数展示出来,编码时,将8位二进制码重新分组成两个4位的字节,其中一个字节的低4位是原字节的高四位,另一个字节的低4位是原数据的低4位,高4位都 ...

  9. linux 使用 ioctl 参数

    在看 scull 驱动的 ioctl 代码之前, 我们需要涉及的另一点是如何使用这个额外的参数. 如果它是一个整数, 就容易: 它可以直接使用. 如果它是一个指针, 但是, 必须小心些. 当用一个指针 ...

  10. Cannot destructure property `createHash` of 'undefined' or 'null'(next服务端渲染引入next-less错误).

    next中引入@zeit/next-less因next版本过低(webpack4之前的版本)无法执行next-less内置的mini-css-extract-plugin mini-css-extra ...