「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, ...
随机推荐
- vue element 中自定义传值
一直以来都不知道如何传自定义的值,一直只会默认的,今天终于找到方法了. 比如这个上传图片的控件,想带当前的index过去,就这样写.其它的类似 :http-request="(file,fi ...
- 不插字段,直接利用OracleSpatial计算
select to_char(regexp_replace(sdo_util.to_gmlgeometry(sdo_geom.sdo_difference( SDO_GEOMETRY ( 2003, ...
- oracle连接多个扫描
如果你对一个列和一组有限的值进行比较, 优化器可能执行多次扫描并对结果进行合并连接. 举例: SELECT * FROM LODGING WHERE MANAGER IN (‘BILL GATES’, ...
- torch.optim优化算法理解之optim.Adam()
torch.optim是一个实现了多种优化算法的包,大多数通用的方法都已支持,提供了丰富的接口调用,未来更多精炼的优化算法也将整合进来. 为了使用torch.optim,需先构造一个优化器对象Opti ...
- Java JDBC学习实战(二): 管理结果集
在我的上一篇博客<Java JDBC学习实战(一): JDBC的基本操作>中,简要介绍了jdbc开发的基本流程,并详细介绍了Statement和PreparedStatement的使用:利 ...
- codeforces 609B
#include<bits/stdc++.h> using namespace std; ]; int main() { memset(num,,sizeof(num)); int n,m ...
- 条件随机场(CRF) - 1 - 简介
声明: 1,本篇为个人对<2012.李航.统计学习方法.pdf>的学习总结,不得用作商用,欢迎转载,但请注明出处(即:本帖地址). 2,由于本人在学习初始时有很多数学知识都已忘记,所以为了 ...
- 2019-3-8-win10-uwp-渲染原理-DirectComposition-渲染
title author date CreateTime categories win10 uwp 渲染原理 DirectComposition 渲染 lindexi 2019-03-08 09:18 ...
- spark mllib docs,MLlib: RDD-based API
MLlib: RDD-based API This page documents sections of the MLlib guide for the RDD-based API (the spar ...
- 【ts】 VSCode自动编译TypeScript终端报错
一.点击终端--运行任务--选择tsc:监视 - tsconfig.json后,终端报出了如下错误:error TS5058: The specified path does not exist 在网 ...