FZU 2037 Maximum Value Problem【递推】
2016-05-04 21:49
441 查看
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,007and the expected number with 6 digits of precision, round half up in a single line.
Sample Input
22
3
Sample Output
Case 1: 3 1.500000Case 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
,ff
);
}
}
相关文章推荐
- FZU 2125 简单的等式
- FZU 2148: Moon Game 四边形的月亮
- 杭电2037今年暑假不AC
- FZU - 2148 Moon Game
- FZU 2037
- FZU - 2110 Star
- FZU 2102 Solve equation
- FZU 2104 C - Floor problem
- FZU 2111 Min Number
- FZU Problem 2054 拳王
- FZU 1759 Super A^B mod C (快速幂+指数循环节)
- FZU 1752 A^B mod C (快速幂)
- FZU 2203 单纵大法好 (二分+贪心)
- HDU 2108 字符串递归
- FZU 2082 树链剖分 区间最大和
- FZU - 2115 多项式积分
- Fzu 2036 Log Calculator(JAVA 浮点数输出精度控制)
- FZU 1056 扫雷游戏
- acm fzu 2108 Mod problem
- Problem-2037 今年暑假不AC