FZU 2037

 

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

223

Sample Output

Case 1: 3 1.500000Case 2: 11 1.833333



 第一个答案公式f(n) = (n-1)!+f(n-1)+(n-1)*f(n-1);    
 第二个答案公式 f(n) = f(n-1)+1/n; (由第一个 公式 除于 n!)
1.如果n放在最后,原来的f(n-1)依旧存在不受影响,同时加多(n-1)!个
2.接着对于(n-1)!个排列,把n这个数分别放在1,2,3,4,n-1这些位置,如下f(3)->f(4)

3个数的排列:
123
132
213
231
312
4个数的排列可以看成在3个数排列的基础上添加一位,则4可以放在第一,第二,第三,第四位
①如果放在最后一位则有 (4-1)! 个
1234
1324
2134
2314
3124
3214
②如果是放在第一,第二和第三时则有 (4-1)*f(4-1);
4123   1423   1243
4132   1432   1342
4213   2413   2143
4231   2431   2341
4312   3412   3142
4321   3421   3241 


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define MAX 1000005
#define mod 1000000007
#define  ll long long int
using namespace std;

ll a[MAX], b[MAX];
double c[MAX];
int n, t;

int main() {
	scanf("%d",&n);
		a[1] = 1,b[1] = 1,c[1] = 1;
		for (int i = 2; i < MAX; i++) {
			a[i] = (a[i-1] *i) % mod;
			b[i] = (a[i-1] + i*b[i-1]) % mod;
			c[i] = c[i-1] + 1.0 / i;	
		}

		for (int i = 1; i <=n; i++)  {
			scanf("%d",&t);
			printf("Case %d: %lld %.6lf\n",i,b[t],c[t]);
		}

		return 0;
}



1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值