Блог пользователя invinciblerm

Автор invinciblerm, история, 8 лет назад, По-английски

I have written the following code for http://codeforces.net/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://codeforces.net/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?

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
8 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      8 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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