ie_fauxpas's blog

By ie_fauxpas, 12 years ago, In English

I've spent two days now on this problem — it should be easy but I keep getting test 16 incorrect and I don't know why!

My code is pretty simple and straightforward:

int main(int argv, char* argc[]) {
    
        double m, n, a;
        while (scanf("%lf %lf %lf", &n, &m, &a) != EOF) {
        
            // Initiaslise tiles counters as 1 because we will
            // always have at least 1 tile.
            long long horiz = 1;
            long long vert  = 1;
        
            while (a < n) {
            
                n -= a;
                horiz ++;
            }
        
            while (a < m) {
            
                m -= a;
                vert ++;            
            }  
      
            printf("%d\n", (horiz * vert));
        }
  
return EXIT_SUCCESS;
}

Yet test 16 (m=1000000000, n=1000000000, n=192) returns a negative number! I get the following:

wrong answer 1st numbers differ — expected: '27126743055556', found: '-270385980'

Arrrgh why??

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
12 years ago, # |
  Vote: I like it +1 Vote: I do not like it

You should output long long numbers with %lld, not %d.

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

    Though there are some troubles with compilers here, so input/output for long long numbers with specifier "%lld" is not recommended/supported. Also, I'm not sure that your algo is correct.

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

I don'know why are you using double, just use ll or llu(read with %I64d or with cin) and i think its so sloow algorithm :)

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

    Yes, it's too slow to pass all the tests! I couldn't think of any other way to solve it though, I am only a first year comp sci student!

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      It's just a little bit of math. Think of a way to how many times number can fit in another.

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it +10 Vote: I do not like it

        Seriously. After 3 years?

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it +6 Vote: I do not like it

        Wow! Thanks for the reply. I had completely forgotten about this challenge. I have almost finished my Comp. Sci. degree now, and you have inspired me to write a much more efficient algorithm to solve this problem :)