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(规律题)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. ACM_送气球(规律题)

    送气球 Time Limit: 2000/1000ms (Java/Others) Problem Description: 为了奖励近段时间辛苦刷题的ACMer,会长决定给正在机房刷题的他们送气球. ...

  4. hdoj--1005--Number Sequence(规律题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  6. 动态规划法(八)最大子数组问题(maximum subarray problem)

    问题简介   本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...

  7. Codeforces - 规律题 [占坑]

    发现自己容易被卡水题,需要强行苟一下规律题 CF上并没有对应的tag,所以本题集大部分对应百毒搜索按顺序刷 本题集侧重于找规律的过程(不然做这些垃圾题有什么用) Codeforces - 1008C ...

  8. 回溯法——最大团问题(Maximum Clique Problem, MCP)

    概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...

  9. 贪心 FZU 2013 A short problem

    题目传送门 /* 题意:取长度不小于m的序列使得和最大 贪心:先来一个前缀和,只要长度不小于m,从m开始,更新起点k最小值和ans最大值 */ #include <cstdio> #inc ...

随机推荐

  1. Ajax中关于xmlhttp.readyState的值及解释

    xmlhttp.readyState的值及解释:0:请求未初始化(还没有调用 open()).1:请求已经建立,但是还没有发送(还没有调用 send()).2:请求已发送,正在处理中(通常现在可以从响 ...

  2. GitHub 热点速览 Vol.31:在?跑个 GitHub 评分如何?

    摘要:个性化的 GitHub README 自从 7 月上线之后一直风靡在各大技术平台,当中最有意思的莫过于代表你技术的 GitHub Readme Stats 了,除了能显示你提交的 pr.comm ...

  3. mongodb 4.0副本集搭建

    近期有同学问mongodb副本集难不难部署,我的回答是不难,很快,几分钟搞定,比mysql MHA简单的不止一点半点. 那么到底如何部署呢?请看下文. 1.  准备工作 1.1 下载软件 选择版本并下 ...

  4. Python 为什么能支持任意的真值判断?

    本文出自"Python为什么"系列,请查看全部文章 Python 在涉及真值判断(Truth Value Testing)时,语法很简便. 比如,在判断某个对象是否不为 None ...

  5. java 启动Tomcat报错:The specified JRE installation does not exist

    启动TomCat服务报错: The specified JRE installation does not exist 解决方法: Eclipse:window->perferences-> ...

  6. Scss 定义内层class的简单写法

    如果定义样式的时候,内层样式名称和外层保持一致的话可以简写如下 如果一个样式下有相关的其他样式可以使用 &-xxx 来简写, <template> <div class=&q ...

  7. 同步博客到cnblogs平台

    缘由 最最开始在csdn写博客,广告太多,平台暗调资源积分,退:后来使用githubpage+jeklly搭建静态博客,感觉不错,回归到安静的敲打环境.emmmm,由于是静态博客项目,虽能最大化自定义 ...

  8. 《JVM G1源码分析和调优》读书笔记

    GC的相关算法与JVM的垃圾收集器 GC的相关算法 分代管理 复制算法 标记清除 标记压缩 JVM垃圾收集器 P242 表11-1 不同类型垃圾回收期比较 串行收集器 Serial. Serial G ...

  9. 关于Differentiated Services Field (DS Field)的诸多疑问

    Differentiated Services Field (DS Field) 先上疑问截图: 这是用wireshark抓包时协议树的某一项的展开结果:IPV4 header.其中有一项如下: 大家 ...

  10. Typescript node starter 1.Express Typescript

    启动项目 Express 是一个nodejs框架,用于构建Web后端应用程序.它非常的灵活,你可以用你喜欢的方式去使用他.在这个系列文章里,记录了我使用typescript express去构建一个w ...