URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理
URAL 1994 The Emperor's plan 求组合数 大数用log
#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#include<map>
#include<set>
#include <stack>
#define REP(i, n) for(int i=0; i<n; i++)
#define PB push_back
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std; const int maxn = 210; double LOG[210];
void pre()
{
for (int i = 1; i < 210; i++)
LOG[i] = LOG[i - 1] + log(i * 1.0);
}
double CN(int a, int b)
{
return LOG[b] - LOG[a] - LOG[b - a];
}
double PN(int a, int b, int c, int d)
{
return exp( CN(c, a) + CN(d, b) - CN(c + d, a + b) );
} double dp[210][21];
bool vis[210][21]; double dpf(int n, int k)
{
if (k == 0) return n;
if (n <= k) return 0;
if (vis[n][k]) return dp[n][k];
vis[n][k] = 1;
double &ans = dp[n][k];
ans = 0;
n -= k; int sum = n + k;
for (int i = 1; i < sum; i++)
{
double now = 0;
int x = min(i, k);
for (int j = 0; j <= x; j++)
now += dpf(n - (i - j), k - j) * PN(n, k, i - j, j);
ans = max(ans, now);
}
return ans;
} /***求组合数,无效值为0
const int maxcn = 20;
int cn[maxcn][maxcn];
int init()
{
for (int i = 1; i < maxcn; i++)
{
cn[i][0] = cn[i][i] = 1;
for (int j = 1; j < i; j++)
cn[i][j] = cn[i - 1][j - 1] + cn[i - 1][j];
}
}
*/ int main()
{
int n, k;
pre();
scanf("%d%d", &n, &k);
printf("%.10lf\n", dpf(n - k, k));
}
URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理的更多相关文章
- URAL 1994 The Emperor's plan
期望$dp$. $dp[i][j]$表示第$1$种人有$i$个,第$2$种人有$j$个的情况下,到达目标状态的期望值.初始化$dp[i][0]=i$. 枚举一下这个状态死多少人,取个$max$,最后$ ...
- lucas求组合数C(n,k)%p
Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...
- N!分解质因子p的个数_快速求组合数C(n,m)
int f(int n,int p) { ) ; return f(n/p,p) + n/p; } https://www.xuebuyuan.com/2867209.html 求组合数C(n,m)( ...
- 求组合数、求逆元、求阶乘 O(n)
在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...
- HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解
题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...
- hdu 2519 求组合数
求组合数 如果求C5 3 就是5*4*3/3*2*1 也就是(5/3)*(4/2)*(3/1) Sample Input5 //T3 2 //C3 25 34 43 68 0 Sample Outpu ...
- 求组合数 C++程序
一 递归求组合数 设函数为void comb(int m,int k)为找出从自然数1.2.... .m中任取k个数的所有组合. 分析:当组合的第一个数字选定时,其后的数字是从余下的m-1个数中 ...
- HDU 5698——瞬间移动——————【逆元求组合数】
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
随机推荐
- C#变成数据导入Excel和导出Excel
excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...
- Searching for Approximate Nearest Neighbours
Searching for Approximate Nearest Neighbours Nearest neighbour search is a common task: given a quer ...
- android activity在横竖屏切换的时候不重新调用onCreate方法
在安卓系统中,横竖屏切换会默认重新调用onCreate等生命周期方法,如果此时有一些临时数据没有保存下来,很有可能会导致该数据丢失. 因此我们可以进行以下设置,来避免恒切换时重新调用onCreate方 ...
- 配置Pycharm3.4.1调试edX Devstack
前提: 1.安装好Ubuntu 2.在Ubuntn上利用vagrant+VirtualBox 搭建好了edX Developer Stack,并能成功访问 3.在Ubuntu下安装好了Pycharm- ...
- Mpeg-2的同步及时间恢复--STC,PCR,DTS,PTS
http://blog.csdn.net/hice1226/article/details/6717354 Mpeg-2的同步及时间恢复--STC,PCR,DTS,PTS 摘要:Mpeg-2同步及时间 ...
- 函数 flst_get_first
/********************************************************************//** Gets list first node addre ...
- BZOJ_1084_[SCOI2005]_最大子矩阵_(动态规划)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1084 给出一个n*m的矩阵,其中m<=2,取k个子矩阵,求最大子矩阵和. 分析 1.m= ...
- JSOI2009 游戏
1443: [JSOI2009]游戏Game Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 557 Solved: 251[Submit][Stat ...
- apache开源项目--kafka
kafka是一种高吞吐量的分布式发布订阅消息系统,她有如下特性: 通过O(1)的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. 高吞吐量:即使是非常普通的 ...
- c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...