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

99 篇文章 0 订阅
84 篇文章 5 订阅
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 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  - 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)

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值