Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs
2014-10-13 12:37
309 查看
A. Dreamoon and Stairs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Dreamoon wants to climb up a stair of n 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 steps making him climb to the top of the stairs that satisfies his condition?
Input
The single line contains two space separated integers n, m (0 < n ≤ 10000, 1 < m ≤ 10).
Output
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.
Sample test(s)
input
output
input
output
Note
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.
简单的数学题,做为A题,在神犇眼里就是1分钟的事情,可是每当我还没加载出来CF,room里的神犇已经把A题A掉了,,,
题目大意:输入一个台阶的总数n,然后在自定义一个整数m,并且规定每次只能上一节或者两节台阶,如果上完n阶台阶所走的步数是m的整数倍,那么就输出一个最小的步数,如果满足不了上面的条件,那么就输出‘-1’。。。。。
算法:贪心思想,一开始假想每次都走2步(O(∩_∩)O哈哈~,贪心吧),然后判断这个步数能不能把m整除,如果可以的话,就输出了,如果不行的话,就从后面开始把2拆成两个1,逐步判断,直到得到最小的步数为止。。。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Dreamoon wants to climb up a stair of n 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 steps making him climb to the top of the stairs that satisfies his condition?
Input
The single line contains two space separated integers n, m (0 < n ≤ 10000, 1 < m ≤ 10).
Output
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.
Sample test(s)
input
10 2
output
6
input
3 5
output
-1
Note
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.
简单的数学题,做为A题,在神犇眼里就是1分钟的事情,可是每当我还没加载出来CF,room里的神犇已经把A题A掉了,,,
题目大意:输入一个台阶的总数n,然后在自定义一个整数m,并且规定每次只能上一节或者两节台阶,如果上完n阶台阶所走的步数是m的整数倍,那么就输出一个最小的步数,如果满足不了上面的条件,那么就输出‘-1’。。。。。
算法:贪心思想,一开始假想每次都走2步(O(∩_∩)O哈哈~,贪心吧),然后判断这个步数能不能把m整除,如果可以的话,就输出了,如果不行的话,就从后面开始把2拆成两个1,逐步判断,直到得到最小的步数为止。。。
# include<cstdio> # include<iostream> using namespace std; int main(void) { int n,m;cin>>n>>m; int cn1 = n/2; int cn2 = n%2; while ( (cn1+cn2)%m!=0&&cn1!=0 ) { cn1--; cn2+=2; } if ( (cn1+cn2)%m!=0 ) cout<<"-1"<<endl; else cout<<cn1+cn2<<endl; return 0; }
相关文章推荐
- Codeforces Round #272 (Div. 2)-A. Dreamoon and Stairs
- Codeforces Round #272 (Div. 2)A. Dreamoon and Stairs 水题
- Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题
- 【Codeforces Round 272 (Div 2)B】【暴力dfs or 组合数】Dreamoon and WiFi 问号填加减方案数使得最后恰好增量为aim
- 【Codeforces Round 272 (Div 2)C】【暴力】Dreamoon and Sums 所有除b结果是模b结果[1,a]倍数字之和
- 【CODEFORCES】 A. Dreamoon and Stairs
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp,组合数学
- Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs
- Codeforces Round #272 (Div. 2)B. Dreamoon and WiFi
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (超几何分布)
- Codeforces Round #272 (Div. 2) Dreamoon and WiFi 暴力
- Codeforces Round #394 (Div. 2)(A. Dasha and Stairs,B. Dasha and friends,C. Dasha and Password)
- Codeforces Round #272 (Div. 2) B Dreamoon and WiFi(简单DP)
- Codeforces Round #272 (Div. 1) Problem C. Dreamoon and Strings
- Dreamoon and Stairs上台阶
- Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums 数学
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp
- Codeforces Round #272 (Div. 2) C Dreamoon and Sums(数学)