FZU - 2037 -Maximum Value Problem(规律题)
Let’s start with a very classical problem. Given an array a[1…n] of positive numbers, if the value of each element in the array is distinct, how to find the maximum element in this array? You may write down the following pseudo code to solve this problem:
function find_max(a[1…n])
max=0;
for each v from a
if(max<v)
max=v;
return max;
However, our problem would not be so easy. As we know, the sentence ‘max=v’ would be executed when and only when a larger element is found while we traverse the array. You may easily count the number of execution of the sentence ‘max=v’ for a given array a[1…n].
Now, this is your task. For all permutations of a[1…n], including a[1…n] itself, please calculate the total number of the execution of the sentence ‘max=v’. For example, for the array [1, 2, 3], all its permutations are [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2] and [3, 2, 1]. For the six permutations, the sentence ‘max=v’ needs to be executed 3, 2, 2, 2, 1 and 1 times respectively. So the total number would be 3+2+2+2+1+1=11 times.
Also, you may need to compute that how many times the sentence ‘max=v’ are expected to be executed when an array a[1…n] is given (Note that all the elements in the array is positive and distinct). When n equals to 3, the number should be 11/6= 1.833333.
Input
The first line of the input contains an integer T(T≤100,000), indicating the number of test cases. In each line of the following T lines, there is a single integer n(n≤1,000,000) representing the length of the array.
Output
For each test case, print a line containing the test case number (beginning with 1), the total number mod 1,000,000,007
and the expected number with 6 digits of precision, round half up in a single line.
Sample Input
2
2
3
Sample Output
Case 1: 3 1.500000
Case 2: 11 1.833333 思路;第n项的交换次数为F[n]=(n-1)!+F[n-1]*n;后面的为res[n]=1.0/n+res[n-1];
预处理一下输出就行了
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
const int mod=1e9+;
typedef long long ll;
using namespace std;
ll f[*maxn];
double res[*maxn];
int main()
{
ll a=;
f[]=;
f[]=;
res[]=;
for(int t=;t<=;t++)
{
f[t]=((a*(t-))%mod+((t)*f[t-])%mod)%mod;
a=(a*(t-))%mod;
res[t]=1.0/t+res[t-];
//printf("%.6f\n",res[t]);
}
int T;
int n;
cin>>T;
int cnt=;
while(T--)
{
scanf("%d",&n); printf("Case %d: %d ",cnt++,f[n]);
printf("%.6f\n",res[n]); }
return ;
}
FZU - 2037 -Maximum Value Problem(规律题)的更多相关文章
- fzu 2037 Maximum Value Problem
http://acm.fzu.edu.cn/problem.php?pid=2037 思路:找规律,找出递推公式f[n]=f[n-1]*n+(n-1)!,另一个的结果也是一个递推,s[n]=s[n-1 ...
- LightOJ1010---Knights in Chessboard (规律题)
Given an m x n chessboard where you want to place chess knights. You have to find the number of maxi ...
- ACM_送气球(规律题)
送气球 Time Limit: 2000/1000ms (Java/Others) Problem Description: 为了奖励近段时间辛苦刷题的ACMer,会长决定给正在机房刷题的他们送气球. ...
- hdoj--1005--Number Sequence(规律题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- 动态规划法(八)最大子数组问题(maximum subarray problem)
问题简介 本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...
- Codeforces - 规律题 [占坑]
发现自己容易被卡水题,需要强行苟一下规律题 CF上并没有对应的tag,所以本题集大部分对应百毒搜索按顺序刷 本题集侧重于找规律的过程(不然做这些垃圾题有什么用) Codeforces - 1008C ...
- 回溯法——最大团问题(Maximum Clique Problem, MCP)
概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...
- 贪心 FZU 2013 A short problem
题目传送门 /* 题意:取长度不小于m的序列使得和最大 贪心:先来一个前缀和,只要长度不小于m,从m开始,更新起点k最小值和ans最大值 */ #include <cstdio> #inc ...
随机推荐
- Tomcat访问图片
Tomcat访问图片 配置server.xml 在 节点中添加 <Context docBase="D:/Tomcat/tomcat-8083/imgs" path=&quo ...
- 「MoreThanJava」Day 4:面向对象基础
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- Android SQLite轻量级数据库的删除和查找操作
今天主要是补充昨天的内容,本打算合成一章的,但是毕竟一天一天的内容写习惯了. 就这样继续昨天的,昨天只讲了创建以及增加和查询, 其实用法都差不多,今天学长也是在原有的基础上写的,还顺便融合了Share ...
- 用 Python 制作关不掉的端午安康弹窗
端午节又称端阳节.龙舟节.重午节.龙节.正阳节.天中节等,端午节源自天象崇拜,由上古时代祭龙演变而来,因传说战国时期的楚国诗人屈原在五月五日跳汨罗江自尽,后来人们亦将端午节作为纪念屈原的节日,在端午节 ...
- C语言基础printf()和scanf()函数
在程序的运行中,我们经常会进行一些输入输出的操作,用来实现交互.为此, C语言便给出了 printf() 函数和 scanf() 函数用来实现输入和输出两个动作. 其中,printf()函数用于向控制 ...
- 【JavaScript】windows.open用法详解
windows.open("URL","窗口名称","窗口外观设定");的用法详解 function onNewWindows(redire ...
- 浏览器编年史与UserAgent大乱斗
1993 世界上第一个支持显示图片的浏览器Mosaic诞生 为了区分浏览器是否能显示图片,UserAgent诞生了,Mosaic将自己标志为NCSA_Mosaic/2.0(windows 3.1) 1 ...
- 从0写一个Golang日志处理包
WHY 日志概述 日志几乎是每个实际的软件项目从开发到最后实际运行过程中都必不可少的东西.它对于查看代码运行流程,记录发生的事情等方面都是很重要的. 一个好的日志系统应当能准确地记录需要记录的信息,同 ...
- Python 为什么要在 18 年前引入布尔类型?且与 C、C++ 和 Java 都不同?
花下猫语:在上一篇<Python 为什么能支持任意的真值判断? >文章中,我们分析了 Python 在真值判断时的底层实现,可以看出 Python 在对待布尔值时,采用了比较宽泛的态度.官 ...
- dota2输入法无候选框?
win10自带的微软拼音在dota2中不会显示候选框,或者说是选词框. 这种情况下只能选择其他输入法. 我选择的是搜狗输入法(此随笔添加时的最新版),在dota2中有选词框. 网上有些人说要改兼容性, ...