1492:求和、
Description
对于正整数n,k,我们定义这样一个函数f,它满足如下规律
f(n,k=1)=-1+2-3+4-5+6...n
f(n,k=2)=-1-2+3+4-5-6...n
f(n,k=3)=-1-2-3+4+5+6...n
f(n,k=4)=-1-2-3-4+5+6+7+8...n
现在给出n和k,你的任务就是要计算f(n,k)的值。
Input
首先是一个整数T,表示有T组数据
接下来每组数据是n和k(1<=n,k<=100000000)
Output
打印出f(n,k)的值,每个输出单独占一行
Sample Input
3
1 1
2 1
3 1
Sample Output
-1
1
-2
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,k,count1,count2,T;
cin>>T;
while(T--)
{
cin>>n>>k;
count1=n/(k*2);
long long int result=count1*pow(k,2);
for(int i=(count1*k*2)+1;i<=n;i++)
{
if(i-(count1*k*2)<=k)
result-=i;
else
result+=i;
}
cout<<result<<endl;
}
}