现在的位置: 首页 > 综合 > 正文

Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs

2018年04月29日 ⁄ 综合 ⁄ 共 1300字 ⁄ 字号 评论关闭
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 nm (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;
}

抱歉!评论已关闭.