Maximum Value Problem FZU
来源:互联网 发布:提示mac中毒 mackeeper 编辑:程序博客网 时间:2024/06/01 22:10
找规律,找规律,先打表再找规律。个人赛两个半个小时出了这一个题,一个小时打表,一个小时写代码,菜鸡选手
规律就是 F[i] = F[i-1]*n + (n-1)!; 对于概率 n! = (n-1)!*n;
F[i] / n! = F[i-1] / (n-1)! + 1 / n; 递推就可以
所以直接打表就可以求出来。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <queue>#include <math.h>#include <stack>#include <utility>#include <string>#include <sstream>#include <cstdlib>#include <set>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;const int maxn = 1000000 + 10;const int mod = 1000000007;int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};int num[maxn];LL f[maxn];double c[maxn];void init(){f[1] = 1;LL N = 1;c[1] = 1;for(int i = 2;i <= maxn;i++){f[i] = (f[i-1]*i+ N)%mod;N = (N*i)%mod;c[i] = c[i-1] + 1.0/i;}}int main(){ init();int t,n;cin>>t;for(int i = 1;i<=t;i++){cin>>n; printf("Case %d: %I64d %.6lf\n",i,f[n],c[n]);}return 0;}
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.
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.
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.
223
Case 1: 3 1.500000Case 2: 11 1.833333
- Maximum Value Problem FZU
- FZU 2037 Maximum Value Problem
- FZU 2037- Maximum Value Problem(规律题)
- FZU 2037 Maximum Value Problem【递推】
- fzu 2037 Maximum Value Problem(递推)
- fzu 2037 Maximum Value Problem(规律? 递推)
- FZU 2037Maximum Value Problem (打表预处理/找规律)
- Maximum Value Problem FZU 2037 找规律 递推
- FZU2037 Maximum Value Problem 规律 + 递推
- Maximum Value
- Maximum Value
- FZU Problem 2104 Floor problem
- FZU problem 1985 LCP Problem
- fzu problem 1013 rpgame
- FZU Problem 2110 Star
- FZU Problem 2054 拳王
- FZU 1692 Key problem
- FZU 2104 Floor problem
- MyBatis批量增加
- 基于g-sensor的起立坐下的的识别
- android.graphics(一)shapes
- MySql的增删改查
- 最经典的黑客技术入门知识
- Maximum Value Problem FZU
- Java语言基础
- QT5显示中文
- JBPM与Spring整合
- day15笔记
- php面向对象之类的创建
- 机器学习面试题目总结
- 编写一个学生类Students,该类成员变量包括学号no、姓名name、性别sex和年龄age,该类的成员方法有genNo()getName ).
- 共同学习Java源代码-数据结构-HashMap(十)