日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FZU - 2037 -Maximum Value Problem(规律题)

發布時間:2023/11/30 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的FZU - 2037 -Maximum Value Problem(规律题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。