标签:style io ar color os sp for on div
2 1 2 2 4
样例输出
5
29
//代码如下
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int n;
double a,b,i,sum;
while(cin>>n)
{
while(n--)
{
cin>>a>>b;
sum=0;
if(a>b) swap(a,b);
for(i=a;i<=b;i++)
sum+=i*i;
printf("%.lf\n",sum);//此处不能用cout,因为数较大,不能完整的输出
}
}
return 0;
}
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
inline LL get_sum(LL x)
{
return x * (x + 1) * (2 * x + 1) / 6;//从1到x的平方的和的公式
}
int main()
{
int T;
LL L, R;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld",&L, &R);
if(L > R) swap(L, R);
printf("%lld\n", get_sum(R) - get_sum(L - 1));
}
return 0;
}
标签:style io ar color os sp for on div
原文地址:http://blog.csdn.net/phytn/article/details/41675277