highAsAKite
highAsAKite

Reputation: 31

Addition and substracting operation of arrays (Big int)

I'm having trouble with working with big integers (10^8 and 10^16 (The summation of the numbers between the interval)). My current code is not being AC. It's getting a 80/100 acceptance, I guess because of limit overflow? Are there any other ways to avoid the array operations? If not, how do I solve the following problem with array addition and subtraction?

Problem: Tasnim comes to visit Dinajpur which is very famous for litchi production. Today, she comes to a litchi orchard and notices a strange thing. The orchard owner placed the litchis into some different sizes of buckets arranged in a row in such a way that every next bucket contains exactly one more litchi than the previous one. She also learns that the smallest bucket contains exactly M litchis and the largest one contains exactly N litchis. Tasnim is very curious to know the total number of litchis that all the buckets have. But, she is not so expert in mathematics. Moreover, she also doesn’t know how many buckets are there. So, she is asking for your help. Now, your task is to help Tasnim to determine the number of buckets and also the total number of litchis that the buckets contain.

Let me give you an example, the smallest bucket has exactly 5 litchis and the largest one has exactly 11. So, there are 5 + 6 + 7 + 8 + 9 + 10 + 11 = 56 litchis in total in 7 buckets.

Input There will be multiple test cases in each input file where each test case contains two integers, M and N (0 < M ≤ N ≤ 108), denoting the number of litchis in the smallest and largest bucket respectively.

The input file will be terminated when M and N both equal to zero (i.e. M is 0 and N is 0) and each input file will contain at most 100 test cases.

Output For each case, print the number of buckets and the total number of litchis separated by a space. Don’t forget to print a blank line for each test case. My code:

#include <stdio.h>
#include<string.h>
int main(){

  long unsigned int m,n,i=0,num,x=0, total;

    while(x<100){
    scanf("%lu %lu",&m,&n);
    if(m==0 && n==0){
    break;
    }
    else{
        num=(n+1)-m;
        total=(num*(m+n))/2;
        printf("%lu %lu\n",num,total);
    }
    x++;
    }
    return 0;
}```

Upvotes: 0

Views: 46

Answers (1)

user3629249
user3629249

Reputation: 16540

the following proposed code:

  1. cleanly compiles
  2. makes use of 64 bit unsigned integers
  3. performs the desired functionality
  4. documents why each header file is included
  5. follows the axiom: only one statement per line and (at most) one variable declaration per statement.

And now, the proposed code:

#include <stdio.h>    // scanf(), printf()
#include <stdint.h>   // uint64_t
#include <inttypes.h> // SCNu64, PRNu64

int main( void )
{
    uint64_t m;
    uint64_t n;
    uint64_t num;
    uint64_t total;

    while( scanf( "%" SCNu64 "%" SCNu64, &m, &n ) == 2 )
    {
        if( m==0 && n==0)
        {
            break;
        }

        num=(n+1)-m;
        total=(num*(m+n))/2;
        printf("%" PRIu64 "%" PRIu64 "\n\n", num, total );
    }
}

a typical run of the program, with comments:

10 1000     << entered data for 1 test case
991 500455  << program output
            << blank line between test cases
0 0         << terminating entered data

Upvotes: 1

Related Questions