codeforces Problem 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.
数学题
思路
1、若情况最差,第一次则要n-1次能找到正确结果。
2、第二次就得再减掉第一次按下的按钮。但如果按错,前一个按钮就会弹出,所以是2(n-2)次。
找完规律,得到柿子:n+(n-1)+2(n-2)…
代码如下(示例):
#include<iostream>
using namespace std;
int n;
void slove(){
int ans=0;
for(int i=1;i<=n;i++)ans+=1+i*(n-i);
cout<<ans<<endl;
}
int main(){
cin>>n;
slove();
return 0;
}