CodeForces - 268B(数学)

Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in a certain order to open the lock. When you push some button, it either stays pressed into the lock (that means that you’ve guessed correctly and pushed the button that goes next in the sequence), or all pressed buttons return to the initial position. When all buttons are pressed into the lock at once, the lock opens.

Consider an example with three buttons. Let’s say that the opening sequence is: {2, 3, 1}. If you first press buttons 1 or 3, the buttons unpress immediately. If you first press button 2, it stays pressed. If you press 1 after 2, all buttons unpress. If you press 3 after 2, buttons 3 and 2 stay pressed. As soon as you’ve got two pressed buttons, you only need to press button 1 to open the lock.

Manao doesn’t know the opening sequence. But he is really smart and he is going to act in the optimal way. Calculate the number of times he’s got to push a button in order to open the lock in the worst-case scenario.

Input
A single line contains integer n (1 ≤ n ≤ 2000) — the number of buttons the lock has.

Output
In a single line print the number of times Manao has to push a button in the worst-case scenario.

Examples
Input
2
Output
3
Input
3
Output
7
Note
Consider the first test sample. Manao can fail his first push and push the wrong button. In this case he will already be able to guess the right one with his second push. And his third push will push the second right button. Thus, in the worst-case scenario he will only need 3 pushes.

题解:要按一定顺序按按钮,在按过程中有一个按错所有按钮都会回到初始位,所有按钮都按进去就解锁了,求最多按的次数。
如第二个例子:第一次最多按错2次按钮即2乘1(第三次自然是对的),那按错两次后就要从头按了,剩下两个按钮可以有一次按错,即按第一个按钮是对的,按第二个按钮是错的,这个过程循环一次,所有是1乘2,那现在顺序已经出来了,在按三次,解锁,所以总的次数是:sum=12+21+3;那变成4个呢,sum=13+22+31+4;所以n个的时候是sum=1(n-1)+2*(n-2)…+(n-1)*1+n;

ac代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
#include<iomanip>
using namespace std;
int main()
{
	int n,sum;
	while (cin >> n)
	{
		sum = 0;
		for (int i = 1; i < n; i++)
			sum += (n - i)*i;
		cout << sum + n << endl;
	}


}

猜你喜欢

转载自blog.csdn.net/weixin_43965698/article/details/87289240