传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2048

全错=全不匹配

设当前全错的个数是dp[n]

那么前(n-1)个全错的话,第n个数就可以从前(n-1)个任意挑选一个进行交换,得到的即是全错的;

dp[n] += (n-1)*dp[n-1]

也可以从下面这种情况样获得全错的排列:

在前(n-1)个中挑出(n-2)个全错,仅有一个对的情况,第n个数只需要和这个对的交换就能得到全错的结果。

可供挑选的(n-2)个全错的情况,在(n-1)个数中,有(n-1)种;(n-2)个全错又有dp[n-2]种

所以:

dp[n] += (n-1)*dp[n-2]

最终递推式就是:

dp[i]=(i-1)*(dp[i-1]+dp[i-2]);

  

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define sc scanf
#define pt printf
#define maxe 25600
#define maxv 55
#define maxn 21
#define mll unsigned long long
const int inf =;
mll dp[maxn];
mll w1[maxn],w2[maxn],b1[maxn],b2[maxn];
mll A[maxn];
int main()
{
freopen("in.txt","r",stdin);
mll i;
A[]=; for(i=;i<=;++i) A[i]=A[i-]*i;
dp[]=,dp[]=; for(i=;i<=;++i) dp[i]=(i-)*(dp[i-]+dp[i-]);
int T,n;
sc("%d",&T);
while(T--)
{
sc("%d",&n);
double ans = 1.0*dp[n]/A[n];
ans*=100000.0;
int res = (int)ans;
if(res%>=) res=res-res%+;
else res=res-res%;
res/=;
//pt("%d\n",res);
i=;
char s[];
while(res!=)
{
s[i]=res%+''; res/=; --i;
}
for(++i;i<=;++i)
{
if(i==) putchar('.');
putchar(s[i]);
}
putchar('%'); putchar('\n');
}
return ;
}

HDU 2048

动态规划-递推-HDU2048的更多相关文章

  1. 最长上升子序列(动态规划递推,LIS)

    1759:最长上升子序列 题目: 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的 ...

  2. 最大子段和(洛谷P1115,动态规划递推)

    洛谷题目链接 题目赋值出来格式有问题,所以我就只放题目链接了 下面为ac代码 #include<bits/stdc++.h> #define ll long long using name ...

  3. 数的计数(noip2001,动态规划递推)

    题目链接: 普通版: https://www.luogu.org/problemnew/show/P1028 数据加强版: https://www.luogu.org/problemnew/show/ ...

  4. P1759 通天之潜水(不详细,勿看)(动态规划递推,组合背包,洛谷)

    题目链接:点击进入 题目分析: 简单的组合背包模板题,但是递推的同时要刷新这种情况使用了哪些物品 ac代码: #include<bits/stdc++.h> using namespace ...

  5. P2347 砝码称重(动态规划递推,背包,洛谷)

    题目链接:P2347 砝码称重 参考题解:点击进入 纪念我第一道没理解题意的题 ''但不包括一个砝码也不用的情况'',这句话我看成了每个砝码起码放一个 然后就做不出来了 思路: 1.这题数据很小,10 ...

  6. NOIP 2008 传纸条(洛谷P1006,动态规划递推,滚动数组)

    题目链接:P1006 传纸条 PS:伤心,又想不出来,看了大神的题解 AC代码: #include<bits/stdc++.h> #define ll long long using na ...

  7. NOIP2000方格取数(洛谷,动态规划递推)

    先上题目: P1004 方格取数 下面上ac代码: ///如果先走第一个再走第二个不可控因素太多 #include<bits/stdc++.h> #define ll long long ...

  8. P1541 乌龟棋 题解(洛谷,动态规划递推)

    题目:P1541 乌龟棋 感谢大神的题解(他的写的特别好) 写一下我对他的代码的理解吧(哎,蒟蒻就这能这样...) 代码: #include<bits/stdc++.h> #define ...

  9. Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)

    有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...

随机推荐

  1. LCD应用程序测试

    #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl ...

  2. HDU 1174 题解(计算几何)

    题面: 爆头 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. C#linq计算总条数并去重复的写法

    一,在实际需求中我们会存在选出了一个集合,而这时我们需要通过集合的某几个字段来计算重复,和统计重复的数量,这时我们可以用到linq来筛选和去重复. 二,如下代码: using System; usin ...

  4. Git:将本地项目连接到远程(github、gitee、gitlab)仓库流程

    当进行协同开发或者为了代码安全备份需要,一般都会将本地代码和远程仓库相连接. 备注:Github.Gitee.Gitlab是三个常用的远程git仓库,操作流程基本一致. 提前环境要求: 1.node. ...

  5. 通过express来打造api服务器

    通过express来打造api服务器[ 后端接口 ] 1.步骤 1.通过脚手架创建项目 const express = require('express'); const router = expre ...

  6. websocket在springboot+vue中的使用

    1.websocket在springboot中的一种实现 在java后台中,websocket是作为一种服务端配置,其配置如下 @Configuration public class WebSocke ...

  7. vue项目上传到OSS

    1.输入阿里云登陆地址 http://signin.aliyun.com/1987179281335458/login.htm 登陆地址 阿里云账号    2.选择对象oss,建议文件夹   3.将文 ...

  8. 一、bootstrap-upload

    一.bootstrap-upload 前端代码: @{ Layout = null; } <!DOCTYPE html> <html lang="zh-CN"&g ...

  9. jquery 模态对话框传值,删除,新增表格行

    个人的练习代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  10. mysql数据按条件导出

    仅导出部分数据: mysqldump -hlocalhost -uuser -p --skip-triggers --no-create-info dbname tbname -w "id ...