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 ...
随机推荐
- 618购物节要到了,Python帮你实现商品有货的微信提醒
时间过的真快,不知不觉一年已过了一半,又快到618购物狂欢节了,剁手党们都希望无论是618购物节还是双11购物节,都能够买到便宜又实惠的商品,但是真心实惠的东西往往是紧俏的,经常会出现无货的情况,我们 ...
- 最适合做C/C++开发的IDE
一.Windows Windows没什么好说的了,铁定vs(下载链接:https://visualstudio.microsoft.com/zh-hans/), 因为如果不用vs,做Qt时会有奇妙的错 ...
- Error:Failed to resolve: com.android.support:recyclerview-v7:28.0.0解决方法
在使用Android Studio的过程中需要添加依赖recyclerview,出现报错: Unable to resolve dependency for ':app@debug/compileCl ...
- Android The layout "activity_main" in layout has no declaration in the base layout folder
报错: The layout "activity_main" in layout has no declaration in the base layout folder; thi ...
- 在GitHub上删除仓库 or 项目,基操!!
创建错误或者想要抛弃某个仓库or项目,点击选择项目,选择Setting页面,左侧方框Option页拉到底: 你就可以看到一个红色的危险域,called Danger Zone,这不禁让我想到了黑子篮球 ...
- Oracle 根据单元格内容不同来判断不同条件
select xxx,xxx from xxx where decode(AAA,0,BBB,1,CCC)='12345' 如果AAA字段等于0则AAA=''12345' 否则如果BBB字段等于1则B ...
- JavaScript 用七种方式教你判断一个变量是否为数组类型
JavaScript 如何判断一个变量是否为数组类型 引言 正文 方法一 方法二 方法三 方法四 方法五 方法六 方法七 结束语 引言 我们如何判断一个变量是否为数组类型呢? 今天来给大家介绍七种方式 ...
- 基于官方Drone-CI 的alpine版本asia亚洲时区构建支持. Drone-CI based alpine Timezone Build
基于官方Drone-CI 的alpine版本最简化添加亚洲时区Dockerfile构建支持. iotd@Github: drone-ci-based-alpine-timezone-build 如添加 ...
- SQL关键字的执行顺序
1.Mysql执行顺序,即在执行时sql按照下面的顺序进行执行: from on join where group by having select distinct union order by 2 ...
- 操作系统-存储管理(3)高速缓存Cache
存储器的组织形式: 数据总是在相邻两层之间复制传送,最小传送单位是定长块,互为副本(不删除) ️指令和数据有时间局部性和空间局部性. 高速缓冲存储器Cache 介于CPU和主存储器间的高速小容量存 ...