Back to the Training Home
Back to the Training Home
Lesson: 31
Problem: 2346
Title: Gauß in Elementary School
Difficulty Level: 1.0
Date: MAR 02, 2009
Site: http://acm.tju.edu.cn/toj/showp2346.html
Very easy problem. Need to know a formula only. You already know it.
n + (n+1) + (n+2) +....m = (n*m)/2;
Solution:
#include<stdio.h>
typedef long long INT;
INT getSum(INT n, INT m) {
INT k = n+m;
INT total = m - n + 1;
INT res = total*k;
return (res)/2;
}
void Cal(INT n, INT m) {
printf("%lld\n",getSum(n,m));
}
int main() {
int kase = 0, k = 1;
INT n, m;
scanf("%d",&kase);
while(kase--) {
scanf("%lld%lld",&n,&m);
printf("Scenario #%d:\n",k++);
Cal(n,m);
printf("\n");
}
return 0;
}
|