Sqare and sum difference

Dev Skill archived problems can be discussed here.

Sqare and sum difference

by arabin » Mon Nov 14, 2016 5:31 am

whats problem in my code? why it doesnt get accepted ?

Code: Select all

#include <stdio.h>

long long calculate(int);

int main() {
   
    int test;
   
    scanf("%d",&test);
    if (test>0&&test<=10000) {
        long long answer[test];
       
        for (int i = 0; i<test; i++) {
           
            int val;
            scanf("%d",&val);
           
            if (val>0&&val<=10000) {
               
                answer[i] = calculate(val);
            }
        }
        for (int i = 0; i<test; i++) {
            printf("%lld\n",answer[i]);
        }
    }
    return 0;
}
long long calculate(int n){
   
   
    long long sum1 = (n*(n+1)*(2*n+1))/6;
   
    long long sum2 = (n*(2+(n-1)))/2;
   
    long long diff = sum2*sum2 - sum1;
   
    return diff;
}
 
Posts: 28
Joined: Sat Aug 06, 2016 11:02 pm

Re: Sqare and sum difference

by devskill » Mon Nov 14, 2016 9:48 pm

Your output do not match in 50% cases. the other 50% it does match. So please check your code properly. It looks like you are having trouble with big cases where the result is big. For smaller results, all seems OK. make sure you are not having overflow.
User avatar
Site Admin
 
Posts: 118
Joined: Tue Feb 02, 2016 10:24 pm

Re: Sqare and sum difference

by tariqul2814 » Wed Nov 16, 2016 12:51 am

My code link: http://ideone.com/9DEP9Q
All the input is correct but it shows W/A. Why??!
 
Posts: 1
Joined: Wed Nov 16, 2016 12:47 am

Re: Sqare and sum difference

by devskill » Fri Nov 18, 2016 9:01 pm

Looks like you got Accepted. Congratulation!
User avatar
Site Admin
 
Posts: 118
Joined: Tue Feb 02, 2016 10:24 pm


Who is online
Users browsing this forum: No registered users and 1 guest
cron