Concept behind this "maths"

Here is the 'supposedly' maths problem that I tried to solve[its still a programming problem too]. I managed to reach the solution after a billion tries with equations, maybe because I got lazy to mathematically prove it on a paper before solving the question but when i reached the solution with these equations i just didn't know what made those equations specifically correct xD, so if someone here is good enough at maths to explain why these equations in the code below made the program work like a charm, that'd be great ^-^


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.

Examples
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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>


#define fl(n)    for(int i = 0; i < n; i++)


#define ll   long long
#define nl   endl
#define init -9e12
#define INF  1e9
#define u    unsigned



using namespace std;

int main()
{

    int n,m; //29 7 ans 21

    cin >> n >> m;

    if(n < m) { cout << -1; return 0; }

    int total_steps;

    if(n % 2 == 0)
    {
        total_steps = n / 2;
    }
    else
    {
        total_steps = (n / 2) + 1;
    }

    while(total_steps % m != 0)
    {
        total_steps++;
    }

    cout << total_steps;

    return 0;
}



Thanks in advance <3 !
Last edited on
you must understand a problem to code it, or you end up with a mess and confused, even when it works.

if you want the least # of moves, you want to take as many 2 step moves as possible. If N is even, for example 100, you need 50 moves of 2 steps.
If n is odd, for example 101, you need 50 2 step moves and 1 one step move.

therefore all you need is
nummoves = N/2 + N%2;

But you have to make it a multiple.

The extra logic does that. More coming

say you have to fit these examples into 7 multiple.

7*7 is 49, so we know that you need at least 7*8 moves, 56.

So now you have to use the above to try to solve for 56.

56 = a+b
and
100 = a*2 + b

2 equations and 2 unknowns... this can be solved:
44 * 2 + 12 = 100
44 + 12 = 56

This is what your code needs to be doing ... finding the multiple, and fitting the solution into it. You need the optimal solution so you can start checking multiples... if 56 hand not worked, you need 7*9. But you didn't want to have to check 7, 14, 21, ... 49 first.




Last edited on
I got it , it's fine now, I was too lazy to completely trace the methods but when I did it was alot clearer , thanks tho ^^
Topic archived. No new replies allowed.