码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces Buttons 数学公式构建

时间:2014-07-22 23:03:15      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   width   io   

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.

Sample test(s)
input
2
output
3
input
3
output
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;
}




codeforces Buttons 数学公式构建,码迷,mamicode.com

codeforces Buttons 数学公式构建

标签:style   blog   color   os   width   io   

原文地址:http://blog.csdn.net/kenden23/article/details/24786049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!