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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/386851.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/386851.shtml
英文地址,請注明出處:http://en.pswp.cn/news/386851.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解決Feign接口調用有時候不好用的分析思路

很多架構師為了鑒權&#xff0c;會把controller帶過來的header信息一股腦的利用feign的攔截器帶入RequestTemplate&#xff0c;然后方便feign接口鑒權。這時候可能會帶入其他的header信息&#xff0c;比如content-type&#xff0c;而有的feign接口是對特定對header信息有要求的…

關于同時可用git命令clone和TortoiseGit拉取代碼不需要密碼

工作需要在windows7下使用git分布式版本控制系統&#xff0c;需要同時可以在git命令行模式或TortoiseGit拉取代碼而不需要每次輸入密碼。 這時候需要同時安裝git和TortoiseGit。 git使用命令ssh-keygen -C “郵箱地址” -t rsa產生的密鑰在TortoiseGit中不能用。TortoiseGit 使…

交叉驗證 cross validation 與 K-fold Cross Validation K折疊驗證

交叉驗證&#xff0c;cross validation是機器學習中非常常見的驗證模型魯棒性的方法。其最主要原理是將數據集的一部分分離出來作為驗證集&#xff0c;剩余的用于模型的訓練&#xff0c;稱為訓練集。模型通過訓練集來最優化其內部參數權重&#xff0c;再在驗證集上檢驗其表現。…

這個太有意思了,程序員可以消遣娛樂

/***        ┏┓ ┏┓ *       ┏┛┻━━━━━━━┛┻┓ *       ┃       ┃*       ┃   ━   ┃ *       █████━█████ ┃*       ┃       ┃ *       ┃   ┻   ┃* …

第十一周總結

這個作業屬于那個課程 C語言程序設計II 這個作業要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass4-2018/homework/3203 我在這個課程的目標是 理解與使用遞歸函數。 參考文獻 基礎題 2-1 宏定義“#define DIV(a, b) a/b”&#xff0c;經DIV(x …

softmax函數與交叉熵損失函數

本文主要介紹了當前機器學習模型中廣泛應用的交叉熵損失函數與softmax激勵函數。 這個損失函數主要應用于多分類問題&#xff0c;用于衡量預測值與實際值之間的相似程度。 交叉熵損失函數定義如下: LCE(y^,y?)?∑i1Nclassesyi?log(yi^)L_{CE}(\hat{y}, y^*) - \sum_{i1}^…

vue配置git的子模塊

在vue的模塊需要調用許多公共組件&#xff0c;在公共組件之后會需要不斷的更新以及分組做&#xff0c;這時候可以利用git的方式更新組件所在位置 [submodule "src/component/common"] path src/component/common urlgit111.111.111.111:projectname/web-common-…

unity如何讓物體與特定物體之間不發生碰撞

unity中我們普遍使用的是碰撞器來實現各個物體的碰撞體積&#xff0c;例如Box collider, Sphere Collider。 在實現游戲的過程中&#xff0c;如果不想要物體與特定物體產生碰撞&#xff0c;或反之&#xff0c;只想讓碰撞發生在特定物體之間時&#xff0c;我們就需要配置layer …

jenkins的JAVA簡單順序配置git倉庫

后臺Java的發布配置 1、從源碼管理下載項目內容 2、構建觸發器 3 、構建下環境 4、構建后處理

SQLyog連接數據庫報錯plugin caching_sha2_password could not be loaded

打開cmd&#xff1a;mysql -uroot -p 進入mysql依次執行下面語句 ALTER USER rootlocalhost IDENTIFIED BY password PASSWORD EXPIRE NEVER; #修改加密規則 ALTER USER rootlocalhost IDENTIFIED WITH mysql_native_password BY password; #更新一下用戶的密碼 FLUSH PRIVI…

unity導入素材時材質丟失素材變成粉紅色的解決方法

有很多時候&#xff0c;當我們通過unity asset store或者blender等等外源導入素材時&#xff0c;會出現材質缺失的bug&#xff0c;如下圖所示 : 一個很可能的原因&#xff0c;是由于unity本身管線在每個版本的更新過程中&#xff0c;材質的渲染編碼發生了改變。由于這種原因引…

Jenkins 部署vue到服務器

鏈接github名稱 2、從源碼管理下載 3、更新最新前端模塊 4、進行構建和打包

unity用coroutine并發實現暫停執行程序

廢話不多說&#xff0c;下面就用一個簡單的顯示指引案件的例子來展示如何用coroutine來暫停程序的執行 using System.Collections; using System.Collections.Generic; using UnityEngine;public class TextTriggered : MonoBehaviour {public GameObject TextObject;// Start…

P2690 接蘋果

———————————————————————— 我用了記憶化&#xff0c;因為它比DP更好理解 ————————————————————————— 資料&#xff1a;百度百科&#xff08; MIKU,I Love HER &#xff09; 來自洛谷&#xff1a;&#xff08;背包的題解&am…

gitlab使用git sourcetree時候的命令

6. Git連接設置 MacOS 打開MacOS的 terminal.app 工具。 輸入 cat ~/.ssh/id_rsa.pub 確認是否有已經存在的證書。 如果提示存在證書&#xff0c;請跳至 第5步。 輸入 ssh-keygen -t rsa -C "your.mobile136.com" -b 4096&#xff0c;并回車&#xff0c;提示的輸入…

numpy數組提取一定規律的數據

numpy數組的索引也是符合start stop step規律的&#xff0c;因此可以通過索引提取出一系列索引有規律的元素&#xff0c;如下例子: import numpy as np i np.linspace(1,100,100, dtypeint)-1 print(i) i_train i[0:100:10] print(i_train)輸出結果如下 : 可以看到通過索引…

在layui中使用 jquery 觸發select 的 change事件無效

在layui中使用 jquery 觸發select 的 change事件無效 使用layui.use監聽select事件 <select lay-filter"demo" lay-verify"required"><script> layui.use([layer, jquery, form], function () { var layer layui.layer, $ layui.j…

Maven添加Oracle驅動及依賴

oracle驅動先去官網下載,下載下來后,需要安裝到maven本地倉庫,然后再pom中添加依賴. 1下載oracle驅動包 ojdbc6-11.2.0.3.jar 2命令行安裝到maven倉庫 mvn install:install-file -DgroupIdcom.oracle -DartifactIdojdbc6 -Dversion11.2.0.3.0 -Dpackagingjar -DfileE:\orac…

Unity C# namespace 命名空間的使用

命名空間在多個面對對象的語言中有應用&#xff0c;例如JAVA&#xff0c;C&#xff0c;C#。本文主要記錄了在C#中如何調用不同命名空間的public class。 首先對namespace做一個簡單的總結。如果說類是對屬性和方法的封裝&#xff0c;那么命名空間就是對各個類的進一步封裝。在…

CRM、用戶管理權限

CRM目錄結構 from django.shortcuts import HttpResponse,render,redirect from django.conf.urls import url from django.utils.safestring import mark_safe from django.urls import reverse from django.forms import ModelForm from stark.utils.my_page import Paginat…