寒假9.2

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.
可以将玩家每一层的按钮划分开来,能得到每一层得到的数为x+=1+j*(n-j)这一公式。

#include<iostream>
using namespace std;
int main()
{
    int n,x=0;
    cin >> n;
    for (int j = 1; j <= n; j++)
    {
        x += 1 + j * (n - j);
    }
    cout << x;
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 8,734评论 0 2
  • 减肥第二天,今天晚上没吃饭,晚上买的水果还没吃,和店长爬楼梯,两圈三十三层 西安现在下大雨,我会一直坚持的。
    爱心小星星阅读 1,937评论 0 0
  • 凡事都有偶然的凑巧,结果却又如宿命的必然。 这句话有点儿宿命论的意思,说的是我们遇到的事情往...
    兮兮小豆阅读 2,454评论 0 0
  • IntelliJ IDEA 设置 JVM 运行参数 https://blog.csdn.net/kl2897811...
    天下不喵阅读 3,552评论 0 0
  • 一切的相遇,都是久别重逢,你永远不会知道,你无意之间撞见的一个陌生人,他是如何帮助你开启一个新世界。 ...
    薄暮初阳阅读 4,886评论 4 20