首页 > 代码库 > codeforces Buttons 数学公式构建
codeforces Buttons 数学公式构建
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.
A single line contains integer n (1?≤?n?≤?2000) — the number of buttons the lock has.
In a single line print the number of times Manao has to push a button in the worst-case scenario.
2
3
3
7
做多了,居然有感觉了,可以感知到大概公式是什么样的,然后验证它。
本题公式就是:
S = n + (n-1) + (n-2) * 2 + (n - 3) * 3 ...
至于怎么想出来的?
模拟一下他的操作,然后得出数列,最后从数列中总结出规律,然后写出去公式。
#include <iostream> using namespace std; void Buttons() { int n = 0; cin>>n; int ans = n; int c = 1; while (--n) { ans += n * c; c++; } cout<<ans; }