SP23976 题解

题目传送门

小学生又双叒叕来写题解啦!

我还是很自豪的,因为我是第一个通过这题的人(其实是因为大家都没做这题啦),而且翻译也是我交的。

这题考的是数学知识,每组测试数据都是可以 \(O(1)\) 过的。

我们可以列个方程试一试。


解:设原序列有 \(y\) 个元素,原序列所有数的和为 \(z\)

\(\begin{cases} z = y \times (a+1)\\ z + x = (y+1) \times a \end{cases}\)

可以解出:

\(\begin{cases} y = a - x\\ z = y \times (a+1) \end{cases}\)

接下来就好算多了。

注意到序列要求元素互不相同且都是正整数

只需让序列的前 \((y-1)\) 个元素取最小,第 \(y\) 项即可取最大。

也就是说,\(a_y =z - \sum\limits_{i=1}^{y-1}i\)

最后送上满分代码:

#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int x, a;
		scanf("%d%d", &x, &a);
		int num = a - x;        //原序列的元素个数。
		LL sum = num * (a+1);   //原序列所有数的和。
		LL t = num * (num - 1) / 2;
		printf("%lld\n", sum - t);
	}
	return 0;
}

首发:2022-03-02 13:35:38

posted @ 2022-08-25 00:19  liangbowen  阅读(13)  评论(0编辑  收藏  举报