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.mzph.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…