invinciblerm's blog

By invinciblerm, history, 8 years ago, In English

I have written the following code for http://mirror.codeforces.com/problemset/problem/552/C

include<bits/stdc++.h>

using namespace std; typedef long long ll;

int main() { int i,j,k,l,m,n,w,f=0,t; cin>>w>>m;

while(m)
{
    i=m%w;
    m=m/w;
    if(f==1)
    i++;
    f=0;
    if(i==0 || i==1 || i==w)
    {  
    }
    else if(i==w-1)
    {
       f=1;
    }
    else
    {
       cout<<"NO";
       return 0;
    }
}
cout<<"YES";
return 0;

} Image link http://mirror.codeforces.com/predownloaded/57/61/5761486604e2ca57814ada50f20e298c3a8c764b.png

Test Case is: 116 Time: 15 ms, memory: 2020 KB Verdict: WRONG_ANSWER Input 10 899 Participant's output NO Jury's answer YES Checker comment wrong answer expected YES, found NO

****

This code is working fine on my DEV C++ Compiler but is giving WA on test case 116 on codeforces , but I have tried that test case on my machine and its showing the right output.

Can anyone explain the reason behind this strange behaviour?

  • Vote: I like it
  • +9
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by invinciblerm (previous revision, new revision, compare).

»
8 years ago, # |
  Vote: I like it +10 Vote: I do not like it

Your integer f hadn't been initialized before you checked whether f==1 on line 14. A local variable has a random value before you initialize it in C++.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have initialized it now but still getting WA on test case 116, Don't know why?

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Let's take a look at the test case (10 899). When you're in the while-loop second time, f==1, so you increase i by one, which makes i become 10. In this case, you should carry 1 to m (or making f=1). It's like when you're doing 899+1 you need to carry 1 to the left twice.

      • »
        »
        »
        »
        8 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        That is why I have written if(i==0 || i==1 || i==w) {
        } else if(i==w-1) { f=1; } instead of adding 1 to m , i will just be equal to w and that can be a part of our solution it will be a power of w.

        • »
          »
          »
          »
          »
          8 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          In that test case, we put 899, 1, 100 on one side, and 1000 on the other. However, what your code does is put 899,1,100 on one side, but considering there's only 800 on that side since m==8 the third time you enter the while-loop. Please think about what m means in your code.

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by invinciblerm (previous revision, new revision, compare).