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个按钮,必须要按照顺序按下去才会把一个锁打开,否则一步按错将会重置所有按钮,题目要求的就是在最坏情况下最多按多少次按钮就能把锁打开。考虑两个按钮的情况,如果顺序是先按2再按1,最坏的情况是先按1,发现重置了,再按2,可以,再按1,总共三步锁打开了。

下面拓展到n个情况,设每一轮我们都按下去一个按钮,假设我们都是在最后才按到正确的按钮上

第一轮,我们最多按n个按钮

第i轮(i<=n),我们已经按下去了i-1个按钮,剩下n-i+1个按钮要按,这一步我们需要找到第i个正确的按钮。依次判断剩下的按钮中某个按钮是不是正确,判断第一个按钮我们只需要1次,不过判断第2个按钮我们需要把之前i-1个按钮都按下再加上第2个按钮,剩下的依次类推,把上次按错按钮导致的重置步数加到确认下一个按钮的步数中,直到我们找到正确的按钮,这时需要按 (n-i)*i次。所以,第i轮总共需要按 1+(n-i)*i次按钮。

i从1到n,加起来就是题目中最多按按钮的次数。

 #include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define lowbit(x) x&(-x)
int main()
{
int n,s;
while(cin>>n){
s=;
for(int i=;i<=n;i++)
s+=+(n-i)*i;
cout<<s<<endl;
}
return ;
}

Codeforces 268B - Buttons的更多相关文章

  1. codeforces 520 Two Buttons

    http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...

  2. CodeForces 520B Two Buttons(用BFS)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Codeforces Round B. Buttons

    Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you ...

  4. Codeforces 520B:Two Buttons(思维,好题)

    题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...

  5. Codeforces Round #295 (Div. 2)B - Two Buttons BFS

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )

    B. Two Buttons time limit per test : 2 seconds memory limit per test :256 megabytes input :standard ...

  7. Codeforces Round #295 (Div. 2) B. Two Buttons

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. 【codeforces 520B】Two Buttons

    [题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...

  9. Codeforces Round #295 (Div. 2) B. Two Buttons 520B

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. MongoDB 新建数据库和集合 查询集合

    MongoDB语法是原生ORM,根本不存在sql语句 创建数据库:这里和一般的关系型数据库一样,都要先建立一个自己的数据库空间 新建数据库db1 > use db1 switched to db ...

  2. what's the python之字符编码与文件处理

    用文本编辑器打开一个文件就是把一个文件读入了内存中 ,所以打开文件的操作也是在内存中的,断电即消失,所以若要保存其内容就必须点击保存让其存入硬盘中 python解释器执行py文件的原理 : 第一阶段: ...

  3. 008-Centos 7.x安装 Ambari 2.2.2 + HDP 2.4.2 搭建Hadoop集群

    1.安装环境说明 安装前先安装好 Centos 7.2, jdk-8u91, mysql5.7.13 一共有3台机器,一个是主节点192.168.111.10,两个是从:192.168.111.11, ...

  4. ORM之视图层

    1.request对象 前台POST传来的数据,包装到POST字典中request.POST 前台浏览器窗口携带的数据,包装到GET字典中request.GET 前台请求的方式,request.met ...

  5. 【设计模式】用追MM来解释23种设计模式,转

    创建型模式 1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳和肯德基 ...

  6. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  7. node加密解密 crytpo

    var crypto = require('crypto'); exports.encrypt = function (str, secret) { var cipher = crypto.creat ...

  8. Linux系统查看日志信息总结

    命令: cat tail -f #系统日志文件存放路径: /var/log/message #系统启动后的信息和错误日志 /var/log/secure #与安全相关的日志信息 /var/log/ma ...

  9. python-数据

    python基本数据类型 数字 整型 int 123 浮点型 float 3.2 布尔型 bool True/False 复数 complex 1+1j 组 序列 字符串 str "hell ...

  10. Node.JS 项目打包 JXCore

    哈哈,又回来了 当你开发完成了Node.JS项目,你应该需要打包发行吧 好,JXCore就是干这个的啦! 嗯,可能你会这样来安装 1. curl http://jxcore.com/xil.sh | ...