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

A. Dreamoon and Stairs

题目连接:

http://www.codeforces.com/contest/476/problem/A

Description

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 moves 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 Input

10 2

Sample Output

6

Hint

题意

有一个长度为n个阶梯,你要爬到顶,你可以一次爬一格,也可以一次爬两格

问你最少爬多少次,才能使得你爬到顶,而且你爬的次数恰好是m的倍数

题解:

数据范围很小,直接暴力爬就好了

代码

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int ans = 1e9;
    for(int i=0;i<=n;i++)
    {
        if((n-i)%2)continue;
        int step=i+(n-i)/2;
        if(step%m==0)ans=min(ans,step);
    }
    if(ans==1e9)cout<<"-1"<<endl;
    else cout<<ans<<endl;
}
posted @ 2016-08-21 21:13  qscqesze  阅读(340)  评论(0编辑  收藏  举报