CF1080B Margarite and the best present

版权声明:欢迎转载欢迎评论! https://blog.csdn.net/rabbit_ZAR/article/details/85254282

题目:Margarite and the best present


思路:

其实我们可以先列个表看看的——


a_i       -1  2 -3  4 -5  6 -7  8 -9 10
sum_i     -1  1 -2  2 -3  3 -4  4 -5  5   //sum是a的前缀和

找个规律——

发现 s u m i = i 1 2 × ( 1 ) i sum_i=\left\lfloor\dfrac{i-1}{2}\right\rfloor × (-1)^i

然后就可以求出 s u m L sum_L s u m R sum_R 了,相减即可。


代码:

#include<bits/stdc++.h>
using namespace std;

#define read(x) scanf("%d",&x)

int main() {
	int T;
	read(T);
	while(T--) {
		int L,R;
		read(L),read(R);
		printf("%d\n",((R+1)/2*((R&1)?-1:1))-((L)/2*((L&1)?1:-1)));
	} 
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/rabbit_ZAR/article/details/85254282