ie_fauxpas's blog

By ie_fauxpas13 months 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??

 
 
 
 

 
»
13 months ago, # |
  Vote: I like it +1 Vote: I do not like it

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

  •  
    »
    »
    13 months 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.

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

      Yeah, I tried %llu first and then it told me to use %I64d, which I guess refers to a 64 bit number?

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

    Thankyou! That was driving me crazy! It's too slow though, I need to work out a faster way to solve it.

 
»
13 months 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 :)

  •  
    »
    »
    13 months 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!