You are viewing a single comment's thread. Return to all comments →
result = (summation from L to R) - (R - L); = ((R(R + 1)/ 2) - ((L - 1)(L)/2)) - (R - L); = (R(R-1)/2) - (L(L - 1)/ 2) + L; = L + ((R(R-1) - L(L-1))/2);
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int q; unsigned long L, R; unsigned long result = 0; scanf("%d", &q); while(q > 0) { scanf("%ld %ld", &L, &R); if(L >= 3 && R > 3 && L < R) result = L + (((R * (R - 1)) - (L * (L - 1))) / 2); printf("%ld\n", result); q--; } return 0; }
Only the testcase 0, 1, 2 are passing.. All other testcases are failing.. Please let us know the usecases..
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #228: Minkowski Sums
You are viewing a single comment's thread. Return to all comments →
result = (summation from L to R) - (R - L); = ((R(R + 1)/ 2) - ((L - 1)(L)/2)) - (R - L); = (R(R-1)/2) - (L(L - 1)/ 2) + L; = L + ((R(R-1) - L(L-1))/2);
Only the testcase 0, 1, 2 are passing.. All other testcases are failing.. Please let us know the usecases..