codeforces268 B. Buttons【找规律】

B. Buttons

time limit per test1 second
memory limit per test256 megabytes

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.

题意: 给你n个按钮,只有按照某种特定的顺序依次按下才能打开锁,否则只能重新按,也就是说如果有3个按钮,顺序为{2,1,3},我第一次按的 2,第二次按得3,由于第二次按得顺序不对,所以之前按的2也会被弹上来,下次按1时,还得再按一次2,让你求最换情况下按多少次,才能打开

分析: 我们这个可以找规律(反正我是),一共n个按钮,首先我们先找第一个正确顺序的按钮,就是最后一次才对,一共试了n次,然后我们枚举第一次按钮,考虑只按了两个按钮,最坏情况就是剩余的都按了一遍最后才找到倒数第二的,这时,第一个按钮多按了 n*(n-1)/2次,然后在考虑倒数第三个,多按了(n-1)*(n-2)/2次,这样一次循环即可

参考代码

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
    int n;cin>>n;
    ll res = 0;
    for(int i = n;i >= 1;i--) 
        res += i*(i - 1) /2;
    cout<<res+n<<endl;
    return 0;
}
  • 如有错误或遗漏,请私聊下UP,thx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值