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 ...
随机推荐
- java Arrays.asList()和Collections.addAll()
java中的方法Arrays.asList(arg1,arg2,arg3...),经常用在将多个元素或数组转化为List中的元素,但是在使用的时候,应该注意: arg1决定返回list的元素类型(即第 ...
- nutch 采集到的数据与实际不符
现象,这个网站我总计能抽取将近500个URL,但实际只抽取了100条 解析:nutch默认从一个页面解析出的链接,只取前 100 个. <property> <name>db. ...
- 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...
- [转载]C# 多选功能(checkedListBox控件)
// 全选; private void btn_allSelected_Click(object sender, EventArgs e) { //this.CheckedListBox1.Check ...
- Python中文全攻略
原文链接:http://blog.csdn.net/mayflowers/archive/2007/04/18/1568852.aspx 1. 在Python中使用中文 在Python中 ...
- Foundation和UIKit框架图
学习Foundation和UIKit的时候比较容易忽视的一个问题: 对于一个新的类,知道它的用法和属性方法,但往往忽视了它的继承关系, 了解类的继承关系能帮助加深对其理解. 另外在官方文档中每一个类的 ...
- Mysql Not in有null值查询的问题
今天发现Mysql的not in使用的一个问题,大致是: select * from A where id not in (select fid from B). 发现查询结果无论如何都是0条记录.后 ...
- win7桌面图标小盾牌怎么去掉(2种方法)
很多用户都会在桌面上放置一些常用的程序图标,由于win7系统提高了系统安全性,新增用户帐户控制,所以会在图标上显示小盾牌,表示需要管理员权限打开.不少win7 32位旗舰版用户觉得这个小盾牌很碍眼,那 ...
- Co-variant array conversion from x to y may cause run-time exception
http://stackoverflow.com/questions/8704332/co-variant-array-conversion-from-x-to-y-may-cause-run-tim ...
- Tolerance (定义发票允差)
(N) AP > Setup > Invoice > Tolerance (定义发票允差) 这里只对价格进行了设置,其他保持了默认.To set tolerance levels f ...