【Codeforces Round 272 (Div 2)A】【贪心 暴力 水题】Dreamoon and Stairs n个台阶每次走一步或两步是否有步数恰好为m倍数

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 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  - 1instead.

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.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int casenum,casei;
int n,m;
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		int top=n;
		int bot=(n+1)/2;
		bool flag=0;
		for(int i=bot;i<=top;++i)if(i%m==0)
		{
			printf("%d\n",i);
			flag=1;
			break;
		}
		if(!flag)puts("-1");
	}
	return 0;
}
/*
【trick&&吐槽】


【题意】
我们要连续上n(1<=n<=10000)个台阶,我们每次可以走一步或者两步。
问你,是否最后走的总步数,可以是m(1<=m<=10)的倍数。

【类型】
贪心 暴力 水题

【分析】
步数为1或2是很有调整空间的。
我们走n个台阶,最少步数是(n+1)/2,最多步数是n
于是,只要[(n+1)/2,n]内有m倍数的数即可。

数据这么小,暴力吧!

【时间复杂度&&优化】
O(n)or O(10)

*/


你可能感兴趣的:(水题,codeforces,贪心,暴力,题库-CF)