268B. Buttons

B. Buttons
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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个按钮的锁,必须全部按照顺序按下才能解开。途中有一次按错,则按钮全部复原。问最坏的情况下,总共需要按多少次按钮。

  假设需要解开a个按钮,则在求得第一个正确的按钮后,其后面的步骤和解开a-1个按钮的最坏情况是一致的。则解开n个按钮得锁所需得步骤有:

F(n)= F(1)+1+2+3+4+...+n-1 = ∑(n^2/2-n/2+1) = n(n^2+5)/6 。注意数据类型。

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

int main()  
{  
       int n;
       scanf("%d",&n);
       printf("%I64d",(long long)n*(n*n+5)/6);
       
       return 0;  
}  







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在MainActivity.java中实现以下代码: ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.subject_menu: showSubjectMenu(); return true; case R.id.hobby_menu: showHobbyMenu(); return true; default: return super.onOptionsItemSelected(item); } } // 显示科目菜单 private void showSubjectMenu() { PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.subject_menu)); popupMenu.getMenuInflater().inflate(R.menu.subject_menu, popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.java_subject: Toast.makeText(MainActivity.this, "Java语言", Toast.LENGTH_SHORT).show(); return true; case R.id.python_subject: Toast.makeText(MainActivity.this, "Python语言", Toast.LENGTH_SHORT).show(); return true; case R.id.android_subject: Toast.makeText(MainActivity.this, "Android", Toast.LENGTH_SHORT).show(); return true; default: return false; } } }); popupMenu.show(); } // 显示爱好菜单 private void showHobbyMenu() { PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.hobby_menu)); popupMenu.getMenuInflater().inflate(R.menu.hobby_menu, popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.sports_hobby: Toast.makeText(MainActivity.this, "运动", Toast.LENGTH_SHORT).show(); return true; case R.id.study_hobby: Toast.makeText(MainActivity.this, "学习", Toast.LENGTH_SHORT).show(); return true; case R.id.read_hobby: Toast.makeText(MainActivity.this, "看书", Toast.LENGTH_SHORT).show(); return true; case R.id.climb_hobby: Toast.makeText(MainActivity.this, "爬山", Toast.LENGTH_SHORT).show(); return true; default: return false; } } }); popupMenu.show(); } } ``` 其中,onCreateOptionsMenu方法用于加载菜单布局文件,onOptionsItemSelected方法用于处理菜单项点击事件。showSubjectMenu方法和showHobbyMenu方法分别用于弹出科目菜单和爱好菜单,并且为每个菜单项设置点击事件,使用Toast提示菜单项的内容。 接下来,在res/menu文件夹下,创建以下三个菜单布局文件: 1. main_menu.xml(主菜单) ```xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/subject_menu" android:title="科目" android:icon="@drawable/ic_menu_subject" android:showAsAction="always" /> <item android:id="@+id/hobby_menu" android:title="爱好" android:icon="@drawable/ic_menu_hobby" android:showAsAction="always" /> </menu> ``` 2. subject_menu.xml(科目菜单) ```xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/java_subject" android:title="Java语言" android:icon="@drawable/ic_menu_java" /> <item android:id="@+id/python_subject" android:title="Python语言" android:icon="@drawable/ic_menu_python" /> <item android:id="@+id/android_subject" android:title="Android" android:icon="@drawable/ic_menu_android" /> </menu> ``` 3. hobby_menu.xml(爱好菜单) ```xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/sports_hobby" android:title="运动" android:icon="@drawable/ic_menu_sports" /> <item android:id="@+id/study_hobby" android:title="学习" android:icon="@drawable/ic_menu_study" /> <item android:id="@+id/read_hobby" android:title="看书" android:icon="@drawable/ic_menu_read" /> <item android:id="@+id/climb_hobby" android:title="爬山" android:icon="@drawable/ic_menu_climb" /> </menu> ``` 其中,ic_menu_subject、ic_menu_hobby、ic_menu_java、ic_menu_python、ic_menu_android、ic_menu_sports、ic_menu_study、ic_menu_read、ic_menu_climb是自定义的菜单图标,可以根据实际情况进行修改。 最终实现效果如下图所示: ![menu_demo](https://user-images.githubusercontent.com/1649961/132389334-0b5e6c0f-1b8d-4d1c-9e64-268b0e81f3cf.gif)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值