CF1080B Margarite and the best present (#数学)

题意翻译

题目描述

小女孩Margarita是信息竞赛的忠实粉丝,她对数组和数列问题尤其感兴趣。

最近,她提出了一个长度为10^9109的数列:

a_1=-1a1​=−1

a_2=2a2​=2

a_3=-3a3​=−3

a_4=4a4​=4

a_5=-5a5​=−5

\cdots⋯

同样的,它也可以被表示成下面这个样子:a_i=i*(-1)^i,1\le i \le 10^9ai​=i∗(−1)i,1≤i≤109Margarita提出了qq个询问,每一个询问包含两个整数:ll和rr。对于每一个询问,你需要回答\sum_{i=l}^{r}a_i∑i=lr​ai​的值,即数列中第ll项至第rr项的和。

输入格式:

第一行包含一个整数qq,表示询问的组数。

接下来qq行每行包含两个整数ll和rr,表示每一组询问。

输出格式:

输出qq行,表示每个询问的答案。

说明:

第一个询问中,你需要回答第11项至第33项的和,a_1+a_2+a_3=-1+2-3=-2a1​+a2​+a3​=−1+2−3=−2。

第二个询问中,你需要回答第22项至第55项的和,a_2+a_3+a_4+a_5=2-3+4-5=-2a2​+a3​+a4​+a5​=2−3+4−5=−2。

第三个询问中,你需要回答第55项至第55项的和,a_5=-5a5​=−5。

第四个询问中,你需要回答第44项至第44项的和,a_4=4a4​=4。

第五个询问中,你需要回答第22项至第33项的和,a_2+a_3=2-3=-1a2​+a3​=2−3=−1。

题目描述

Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.

Recently, she was presented with an array aa of the size of 10^9109 elements that is filled as follows:

  • a_1 = -1a1​=−1
  • a_2 = 2a2​=2
  • a_3 = -3a3​=−3
  • a_4 = 4a4​=4
  • a_5 = -5a5​=−5
  • And so on ...

That is, the value of the ii -th element of the array aa is calculated using the formula a_i = i \cdot (-1)^iai​=i⋅(−1)i .

She immediately came up with qq queries on this array. Each query is described with two numbers: ll and rr. The answer to a query is the sum of all the elements of the array at positions from ll to rr inclusive.

Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.

Help her find the answers!

输入输出格式

输入格式:

The first line contains a single integer qq ( 1 \le q \le 10^31≤q≤103 ) — the number of the queries.

Each of the next qq lines contains two integers ll and rr ( 1 \le l \le r \le 10^91≤l≤r≤109 ) — the descriptions of the queries.

输出格式:

Print q lines, each containing one number — the answer to the query.

输入输出样例

输入样例#1

5
1 3
2 5
5 5
4 4
2 3

输出样例#1

-2
-2
-5
4
-1

说明

In the first query, you need to find the sum of the elements of the array from position 11 to position 33 . The sum is equal to a_1 + a_2 + a_3 = -1 + 2 -3 = -2a1​+a2​+a3​=−1+2−3=−2 .

In the second query, you need to find the sum of the elements of the array from position 22 to position 55. The sum is equal to a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2a2​+a3​+a4​+a5​=2−3+4−5=−2 .

In the third query, you need to find the sum of the elements of the array from position 55 to position 55 . The sum is equal to a_5 = -5a5​=−5 .

In the fourth query, you need to find the sum of the elements of the array from position 44 to position 44 . The sum is equal to a_4 = 4a4​=4 .

In the fifth query, you need to find the sum of the elements of the array from position 22 to position 33 . The sum is equal to a_2 + a_3 = 2 - 3 = -1a2​+a3​=2−3=−1 .


思路

数学推论题,将 l、r 分成奇奇、偶偶、奇偶、偶奇 4 种情况分别讨论找规律即可。

#include <stdio.h>
#include <iostream>
using namespace std;
int l,r,n,s;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	register int i;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		s=0;
		cin>>l>>r;
		l-=1;
		if(l%2==0)
			l/=2;
		else
			l=-(l+1)/2;
		if(r%2==0)
			r/=2;
		else
			r=-(r+1)/2;
		cout<<r-l<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值