1 solutions
-
1
解法一:
直接把每个数算下来,然后再加起来
不出意外的AC了解法二:
对于第ai个数,都可以表示成(a1 + d * (i - 1)),而在本题中就是(a1 + (a2 - a1) * (i - 1))
由此可得出a1+a2+...+an=(a2 - a1) * (n - 1) * n / 2 + a1 * n = a2 * (n - 1) * n / 2 - a1 * (n - 3) * n / 2
代码如下:
#include <iostream> #include <cstdio> #include <iostream> long long a1, a2, n; int main() { std::cin >> a1 >> a2 >> n; std::cout << a2 * (n - 1) * n / 2 - a1 * (n - 3) * n / 2; return 0; }
时间复杂度O(1)
- 1
Information
- ID
- 4520
- Time
- 1000ms
- Memory
- 250MiB
- Difficulty
- 1
- Tags
- # Submissions
- 4
- Accepted
- 3
- Uploaded By