「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, ...
随机推荐
- 获取checkbox返回值
<div class="checkbox"> <label> <input type="checkbox" value=" ...
- Vue 路由的嵌套使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python--day26--封装和@property
---恢复内容开始--- @property:修饰过的方法不能传任何参数,把方法伪装成属性,没有这个装饰就像c1.area()这样调用,少了一个括号,没什么用. @name.setter:实现可以修改 ...
- Python--day32--ftp文件传输报错的原因
解决办法:把buffer改小 server.py #实现一个大文件的上传或下载 #配置文件 ip地址 端口号 import json import socket import struct sk = ...
- H3C RIP路由表的更新
- P1051 八皇后问题
题目描述 在国际象棋中,皇后是同时具备象和车的攻击范围的,它可以横竖移动,也可以斜着移动.那么在一个8*8的标准国际象棋棋盘中,我们要放入8个皇后,同时皇后之间无法互相攻击,问有多少种皇后的放置方法. ...
- 【23.68%】【hdu 2871】Memory Control
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- fetch是什么?写一个fetch请求
fetch是web提供的一个可以获取异步资源的api,目前还没有被所有浏览器支持,它提供的api返回的是Promise对象,所以你在了解这个api前首先得了解Promise的用法. 参考链接:http ...
- CF161BDiscounts
CF161B 题目大意;要购买\(n\)件物品,有\(A\)\(B\)两种类型,要求分成\(k\)组,其中如果其中一组含有\(A\)类物品,那么这一组最便宜的一件物品就会半价 怎么分组最小化代价? 我 ...
- 关于MySQL中查询大数据量的情况下分页limit的性能优化
https://blog.csdn.net/weixin_37848710/article/details/80772725