Caramelio's blog

By Caramelio, 4 months ago, In English,

Matryoshka Dolls This is the problem I am stuck on. I wrote the source code in C++, which is down below. It failed on the 2nd test.

#include<iostream>
using namespace std;

int main(){
    int s, x;
    cin >> s >> x;
    cout << (s-x)/x << endl;
}

If you have some time to spare, please help me. Thank you!!

 
 
 
 
  • Vote: I like it
  • -11
  • Vote: I do not like it

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

What you did was coming up with an equation, although I have not judged it but from reading the problem statements I think what you should be doing is count how many times can S be divided by X until there is no remainder left and the that count shall be the answer.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I am terribly sorry, but I couldn't understand your advice. Could you specify that a little bit more, please? I'm so sorry. My ability to understand is probably extremely low.

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It is something like this:

      while(s) { s/=x' cnt++; }

      You are checking how many times you can divide the size with x or rather say you are checking the number of dolls that can be nested.

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

As mentioned, the idea is so simple. The statement of problem clearly says what you need to do. Initially you have size S, next one should be S/X according to the statement, Then the next one is (S/X)/X, So what you need to calculate is logarithm S to the base X. This can be done using while loop.

Code
»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

You are finding (s/x)-1. But for each time, you should make s=/x and increase the answer(it should be 1 at the beginning). Because it says the doll <= the doll holds that doll/x.