codeforces gym 100357 H (DP 高精度)
题目大意
有r*s张扑克牌,数字从1到 r,每种数字有s种颜色。
询问对于所有随机的d张牌,能选出c张组成顺子的概率和组成同花的概率。
解题分析
对于组成顺子的概率,令dp[i][j][k]表示一共选出了i张牌,数字从1~j,最后有k张牌是顺子。对于每个数字进行考虑,有0~s种选法。要保证连续c张牌的顺子。
对于组成同花的概率,令dp[i][j]表示一共选出了i张牌,颜色从1~j,。对于每种颜色进行考虑,有0~r种选法。要保证没有c张牌是相同颜色的。
最后用高精度来输出答案。
参考程序
#include <bits/stdc++.h>
using namespace std; const int N=; class bign
{
public:
enum {MAXN = };
int len, s[MAXN];
void clean()
{
while(len > && !s[len-]) len--;
}
bign ()
{
memset(s, , sizeof(s));
len = ;
}
bign (int num) { *this = num; }
bign (long long num) { *this = num; }
bign (const char *num) { *this = num; }
bign operator = (const long long num)
{
char s[MAXN];
sprintf(s, "%I64d", num);
*this = s;
return *this;
}
bign operator = (const int num)
{
char s[MAXN];
sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator = (const char *num)
{
for(int i = ; num[i] == '' && num[]!='\0'; num++) ; //去前导0
len = strlen(num);
for(int i = ; i < len; i++) s[i] = num[len-i-] - '';
return *this;
}
bign operator + (const bign &b) const //+
{
bign c;
c.len = ;
for(int i = , g = ; g || i < max(len, b.len); i++)
{
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % ;
g = x / ;
}
return c;
}
bign operator += (const bign &b)
{
*this = *this + b;
return *this;
}
bign operator * (const int x)
{
bign c;
int j=; for (int y=x;y;y/=,j++);
c.len = len + j;
for(int i = ; i < len; i++)
c.s[i] = s[i] * x; for(int i = ; i < c.len; i++)
{
c.s[i+] += c.s[i]/;
c.s[i] %= ;
}
c.clean();
return c;
}
bign operator * (const bign &b) //*
{
bign c;
c.len = len + b.len;
for(int i = ; i < len; i++)
{
for(int j = ; j < b.len; j++)
{
c.s[i+j] += s[i] * b.s[j];
}
}
for(int i = ; i < c.len; i++)
{
c.s[i+] += c.s[i]/;
c.s[i] %= ;
}
c.clean();
return c;
}
bign operator *= (const bign &b)
{
*this = *this * b;
return *this;
}
bign operator *= (const int x)
{
*this = *this * x;
return *this;
}
bign operator - (const bign &b)
{
bign c;
c.len = ;
for(int i = , g = ; i < len; i++)
{
int x = s[i] - g;
if(i < b.len) x -= b.s[i];
if(x >= ) g = ;
else
{
g = ;
x += ;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
bign operator -= (const bign &b)
{
*this = *this - b;
return *this;
}
bign operator / (const bign &b)
{
bign c, f = ;
for(int i = len-; i >= ; i--)
{
f = f*;
f.s[] = s[i];
while(f >= b)
{
f -= b;
c.s[i]++;
}
}
c.len = len;
c.clean();
return c;
}
bign operator /= (const bign &b)
{
*this = *this / b;
return *this;
}
bign operator % (const bign &b)
{
bign r = *this / b;
r = *this - r*b;
return r;
}
bign operator %= (const bign &b)
{
*this = *this % b;
return *this;
}
bool operator < (const bign &b)
{
if(len != b.len) return len < b.len;
for(int i = len-; i >= ; i--)
{
if(s[i] != b.s[i]) return s[i] < b.s[i];
}
return false;
}
bool operator > (const bign &b)
{
if(len != b.len) return len > b.len;
for(int i = len-; i >= ; i--)
{
if(s[i] != b.s[i]) return s[i] > b.s[i];
}
return false;
}
bool operator == (const bign &b)
{
return !(*this > b) && !(*this < b);
}
bool operator != (const bign &b)
{
return !(*this == b);
}
bool operator <= (const bign &b)
{
return *this < b || *this == b;
}
bool operator >= (const bign &b)
{
return *this > b || *this == b;
}
operator string() const
{
string res = "";
for(int i = ; i < len; i++) res = char(s[i]+'') + res;
return res;
}
friend istream& operator >> (istream &in, bign &x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
friend ostream& operator << (ostream &out, const bign &x)
{
out << string(x);
return out;
}
}; int r,s,d,c;
int C[N][N];
bign dp[N][N];
bign f[N][N][N]; bign calc_1(int n,int k)
{
bign tmp=;
for (int i=;i<=k;i++)
{
tmp=tmp*(n-(i-));
tmp=tmp/i;
}
return tmp;
} bign gcd(bign x,bign y)
{
//return y>0?gcd(y,x % y):x;
bign tmp;
if (y>) tmp=gcd(y,x % y); else tmp=x;
return tmp;
} int main()
{
freopen("poker.in","r",stdin);
freopen("poker.out","w",stdout); for (int i=;i<N;i++) C[i][]=;
for (int i=;i<N;i++)
for (int j=;j<=i;j++)
C[i][j]=C[i-][j]+C[i-][j-]; cin.sync_with_stdio();
while (cin>>r>>s>>d>>c)
{
if (!r && !s && !d && !c) break; memset(f,,sizeof(f));
for (int j=;j<=r;j++) f[][j][]=;
for (int i=;i<=d;i++)
for (int j=;j<=r;j++)
{
for (int k=;k<=min(i,c-);k++)
f[i][j][]+=f[i][j-][k];
for (int k=;k<=min(j,c-);k++)
for (int l=;l<=min(i,s);l++)
f[i][j][k]+=f[i-l][j-][k-]*C[s][l];
}
bign ans=;
for (int k=;k<=c-;k++) ans+=f[d][r][k];
bign a=calc_1(r*s,d),b=a-ans;
cout<<b/gcd(a,b)<<"/"<<a/gcd(a,b)<<endl; memset(dp,,sizeof(dp));
for (int j=;j<=s;j++) dp[][j]=;
for (int i=;i<=d;i++)
for (int j=;j<=s;j++)
for (int k=;k<=min(i,c-);k++)
dp[i][j]+=dp[i-k][j-]*C[r][k];
a=calc_1(r*s,d),b=a-dp[d][s];
cout<<b/gcd(a,b)<<"/"<<a/gcd(a,b)<<endl<<endl;
}
}
codeforces gym 100357 H (DP 高精度)的更多相关文章
- codeforces Gym 100187H H. Mysterious Photos 水题
H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- codeforces Gym 100500H H. ICPC Quest 水题
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- Codeforces Gym 100425H H - Football Bets 构造
H - Football BetsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- codeforces gym 100357 K (表达式 模拟)
题目大意 将一个含有+,-,^,()的表达式按照运算顺序转换成树状的形式. 解题分析 用递归的方式来处理表达式,首先直接去掉两边的括号(如果不止一对全部去光),然后找出不在括号内且优先级最低的符号.如 ...
- codeforces gym 100357 I (费用流)
题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...
- codeforces gym 100357 J (网络流)
题目大意 有n种物品,m种建筑,p个人. n,m,p∈[1,20] 每种建筑需要若干个若干种物品来建造.每个人打算建造一种建筑,拥有一些物品. 主角需要通过交易来建造自己的建筑,交易的前提是对方用多余 ...
- codeforces gym 100286 H - Hell on the Markets (贪心算法)
题目链接 题意:n个数分别为a[i],问是否存在一组对应的b[i],b[i]=1 || b[i]=-1,使得ai*bi的n项和为0. 题解: 先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可 ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
随机推荐
- 【React Native】React Native项目设计与知识点分享
闲暇之余,写了一个React Native的demo,可以作为大家的入门学习参考. GitHub:https://github.com/xujianfu/ElmApp.git GitHub:https ...
- JavaScript--DOM访问子结点childNodes
访问子结点childNodes 访问选定元素节点下的所有子节点的列表,返回的值可以看作是一个数组,他具有length属性. 语法: elementNode.childNodes 注意: 如果选定的节点 ...
- CentOS环境下下调整home和根分区大小
项目建设方给提供了3台CentOS的服务器,连接进去之后发现磁盘空间很大,但是都放在了home目录下,所以需要调整一下. 1.查看磁盘使用情况 [root@CentOS ~]# df -h Files ...
- 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...
- c++自动导出lua绑定
cocos 使用bindings-generator脚本代替了toLua++. 编写效率大大提高. 具体的在本机中分享:http://note.youdao.com/noteshare?id=0f41 ...
- linux centos7 安装nginx并启动
Linux下安装Nginx完整教程及常见错误解决方案:https://blog.csdn.net/chenxiaochan/article/details/63688346 CentOS 7 安装Ng ...
- javascript 到将来某个时间(2020-5-20)的倒计时
function countDown(dateStr){ var end = +new Date(dateStr), start = +new Date(), during = Math.floor( ...
- win7如何设置自动关机
如果想设置Win7按照自己意愿自动关机,而又不希望下载安装第三方软件,则可以通过以下两个方法来简单实现. 工具/原料 Windows7操作系统环境 方法1:利用cmd命令 1 打开cmd窗口. 方法一 ...
- jstree的基本应用----记录
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- vim之vimrc配置文件
""""""""""""""""&quo ...