Dreamoon and Stairs CodeForces - 476A
题目描述
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.
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.
大致题意
给你一个台阶数n和一个整数m,每次你可以走一个台阶或者两个。问你最少需要走几次恰好到达n台阶处,且这个数是m的倍数,如果不存在则输出-1
思路
一次走两步可以分成两次走一步,所以先尽量都用两步的走,看所需的次数是否是m的倍数,如果不是再慢慢的将两步拆分成一步,以此类推。
下面是代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<sstream>
#include<ctime>
using namespace std;
int dix[4]={0,1,0,-1};
int diy[4]={1,0,-1,0};
int main()
{
int n,m,k,flag=0,s;
cin>>n>>m;
if(n%2==0)
{
s=k=n/2;
while(k>=0)
{
if(s%m==0&&s>=m)
{
cout<<s;
flag=1;
break;
}
s++;
k--;
}
}
else
{
k=n/2;
s=k+1;
while(k>=0)
{
if(s%m==0&&s>=m)
{
cout<<s;
flag=1;
break;
}
s++;
k--;
}
}
if(flag==0)
cout<<-1;
return 0;
}