Problem 2037 Maximum Value Problem

Accept: 257    Submit: 797
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

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

 Source

2011年全国大学生程序设计邀请赛(福州)

题目大意:根据题干给出的序列长度和题干给出的代码,求出序列全排列的加和数和加和数/n的值。

思路:暴力打表+递推找规律。

对于第一个要输出的数据:

1 1

2 3

3 11

4 50

5 174

6 1764

7 13068

然后就是观察+耐心,发现输入2输出的3是1*2+1!来的,输入3输出的11是3*3+2来的,输入4输出的50是4*11+3!来的,输入5输出的174是5*50+4!来的。然后就开开心心的敲上了代码,至于后边的double数据呢?要怎样处理呢?如果求余再相除那么结果就会有偏差。然后还是选择了打表+继续找规律,发现输出的后边的值是:

1.000000

1.500000

1.833333

2.083333

2.283333

2.450000

然后再是观察+耐心,发现其规律是这样滴:

1.50000000=1.00000+0.5000000;

1.873333333=1.5000000+0.3333333;

2.0833333=1.87333333+0.25000000;

那么也就是+1/2、1/3、1/4、1/5.....................

这样嘞,规律就彻底找到嘞。

AC代码:

#include <stdio.h>
#include<string.h>
#define mod 1000000007
#define maxs 1000005
#define ll __int64
ll  f[maxs];
double ff[maxs];void init()
{f[1]=1,ff[1]=1;;ll tmp=1;for(int i=2; i<=1000005; i++){f[i]=(f[i-1]*i+tmp)%mod;tmp=(tmp*i)%mod;ff[i]=ff[i-1]+1.0/(i*1.0);}
}
int main()
{init();int t;int kase=0;scanf("%d",&t);while(t--){int n;scanf("%d",&n);printf("Case %d: %I64d %.6lf\n",++kase,f[n],ff[n]);}
}

FZU 2037 Maximum Value Problem【递推】相关推荐

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

  2. 【CodeForces 332B --- Maximum Absurdity】递推

    [CodeForces 332B --- Maximum Absurdity]递推 题目来源:点击进入[CodeForces 332B - Maximum Absurdity] Description ...

  3. D - Maximum Value Problem FZU - 2037

    D - Maximum Value Problem FZU - 2037 题意: 这个序列[1,3,2,4],maxx=0.如果将maxx赋值为最大值需要3次,第一次为maxx=1,第二次maxx=3 ...

  4. uva10401Injured Queen Problem(递推)

    题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...

  5. UVA11069 A Graph Problem【递推】

    Given an undirected graph of the following form with n nodes, 1 ≤ n ≤ 76:     Your task is to calcul ...

  6. 第十一届山东省大学生程序设计竞赛 L. Construction of 5G Base Stations(概率期望,递推前缀和优化)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 第十一届山东省大学生程序设计竞赛 L. Construction of 5G Base Station ...

  7. 0x02.基本算法 — 递推与递归

    目录 一.递推与递归 二.分治 三.模拟计算机实现递归 四.相应习题: 0.AcWing 92. 递归实现指数型枚举(递归/循环+位运算) 1.AcWing 93. 递归实现组合型枚举 2.AcWin ...

  8. HDU2045 不容易系列之(3)—— LELE的RPG难题(递推)

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

  9. ACM训练赛--递推专题

    1001: Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be ...

  10. HDU2085:核反应堆(递推)

    Problem Description 某核反应堆有两类事件发生: 高能质点碰击核子时,质点被吸收,放出3个高能质点和1个低能质点: 低能质点碰击核子时,质点被吸收,放出2个高能质点和1个低能质点. ...

最新文章

  1. 表单之input标签
  2. python 播放 wav 文件
  3. 理财周报,致歉还远远不够
  4. C++中的sta::atomic<bool>和auto类型
  5. 简单团队-爬虫豆瓣top250-项目总结
  6. ASP.NET AJAX 宝典系列(2) 在Visual Studio 2005 Web Application 项目中使用AJAX
  7. 数据挖掘之关联分析七(非频繁模式)
  8. Linux 内核源代码的目录结构
  9. 英特尔傲腾内存linux,英特尔傲腾内存怎么样?intel傲腾内存优点和缺点你知道吗?...
  10. 【算法系列之四】柱状图储水
  11. Qt容器类(总结)(新发现的QQueue和QStack,注意全都是泛型)
  12. leetcode题库1314-- 矩阵区域和
  13. 离线身份证OCR识别
  14. 小萝贝控机大师工具推荐(一款在PC就能控制手机界面的工具)
  15. firefox 3.0 beta 1 试用与下载
  16. 【Java工具类】中文转换成汉语拼音工具-pinyin4j
  17. Flex 连接 FMS,测试代码。
  18. 怎样查看主板支持服务器内存,怎么看主板支持内存频率?看主板支持内存频率方法...
  19. 专注于IBM公司愈百年来工程项目开发与管理
  20. 测试开发之Python核心笔记(15):迭代器与生成器

热门文章

  1. IIC,RS485,RS232各种协议手册更新中
  2. Windows bat批处理常用指令,常用指令及语法总结
  3. U深度-重装电脑系统
  4. 零基础用阿里云服务器搭建网站的步骤
  5. 体育新闻html模板,HTML5响应式体育新闻资讯类门户网站模板源码+自适应移动端...
  6. Scrapy框架以及scrapy-redis实现分布式爬虫
  7. vue 使用ace编辑器
  8. Ubuntu 20.04 更换阿里源
  9. 美女老师,大数据直播,老师做了什么,让全场尴尬。
  10. CAN总线与CANOPEN协议