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+5;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
ll f[10*maxn];
double res[10*maxn];
int main()
{
    ll a=1;
    f[0]=0;
    f[1]=1;
    res[1]=1;
    for(int t=2;t<=1000000;t++)
    {
        f[t]=((a*(t-1))%mod+((t)*f[t-1])%mod)%mod;
        a=(a*(t-1))%mod;
        res[t]=1.0/t+res[t-1];
        //printf("%.6f\n",res[t]);
    }
    int T;
    int  n;
    cin>>T;
    int cnt=1;
    while(T--)
    {
      scanf("%d",&n);

      printf("Case %d: %d ",cnt++,f[n]);
      printf("%.6f\n",res[n]);

    }
    return 0;
}


原文地址:https://www.cnblogs.com/Staceyacm/p/10840355.html

时间: 2024-10-07 00:16:39

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]+1/n; 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 1000010 5 #define ll long long 6 using namespace st

ZOJ 3798 Abs Problem(规律题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798 Abs Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Alice and Bob is playing a game, and this time the game is all about the absolute value! Alice has

LightOJ1010---Knights in Chessboard (规律题)

Given an m x n chessboard where you want to place chess knights. You have to find the number of maximum knights that can be placed in the chessboard such that no two knights attack each other. Those who are not familiar with chess knights, note that

hdu5351 MZL&#39;s Border(规律题,java)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 905    Accepted Submission(s): 295 Problem Description As is known to all,

HDU_1098 Ignatius&#39;s puzzle[规律题]

Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal

HDU 4148 Length of S(n)(规律题)

Problem Description http://acm.hdu.edu.cn/showproblem.php?pid=4148 A number sequence is defined as following: S(1)=1, S(2)=11, S(3)=21, S(4)=1211, S(5)=111221, S(6)=312211, -- Now, we need you to calculate the length of S(n). Input The input consists

sdut 2841 Bit Problem (水题)

题目 贴这个题是因为看题解有更简单的方法, 我做的时候是直接算的, 也很简单. 贴一下题解吧: 如果一个整数不等于 0,那么该整数的二进制表示中至少有一位是 1. 这个题结果可以直接输出 x - (x&(x-1)); 因为x-1 之后二进制下,就是最右边的1变成了0, 最右边的1的 右边所有的0变成了1, 不影响最左边. 我的代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4

Geeks Ford-Fulkerson Algorithm for Maximum Flow Problem 最大网络流问题

很久之前就想攻克一下网络流的问题了,一直拖着,一是觉得这部分的内容好像非常高级,二是还有很多其他算法也需要学习,三是觉得先补补相关算法会好点 不过其实这虽然是图论比较高级的内容,但是基础打好了,那么还是不会太难的,而且它的相关算法并不多,熟悉图论之后就可以学习了,就算法不会二分图也可以学习. 这里使用Ford-Fulkerson算法,其实现的方法叫做:Edmonds-Karp Algorithm 其实两者前者是基本算法思想,后者是具体实现方法. 两个图十分清楚: 图1:原图,求这个图的最大网络流

fzu 1753 Another Easy Problem

本题题意为求 t (t<150) 个 c (n,m)  (1<=m<=n<=100000)的最大公因子: 本题的难点为优化.主要有两个优化重点.一是每次对单个素因子进行处理,优化每次的数组清零:二是对求阶乘素因子个数的优化 ei=[N/pi^1]+ [N/pi^2]+ …… + [N/pi^n]  其中[]为取整 ei 为数 N!中pi 因子的个数: #include <iostream>#include <cstring>#include <cmat