UVA 10759 Dice Throwing
题意为抛n个骰子凑成的点数和大于或等于x的概率,刚开始用暴力枚举,虽然AC了,但时间为2.227s,然后百度了下别人的做法,交了一遍,靠,0.000s,然后看了下思路,原来是dp,在暴力的基础上记忆化搜索,把所有可能枚举出来再累加,然后自己也打了一遍,0.000sA了,做法是开一个二维数组,第一维是骰子个数,第二维是和x,然后一个只有可能是1,2,3,4,5,6,倒两个骰子时是1+1,2,3,4,5,6 2+1,2,3,4,5,6 3+...累加向上推即可
//暴力做法,2.227s
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring> using namespace std; typedef long long ll; int n,x,dice[],prime[]={,,,,,,,,},fact[][];//n个dice,x为和,dice[i]点数为i的骰子个数
ll fz,fm; void fact_table()//fact[i] i!的质因分解表
{
memset(fact,,sizeof(fact));
for(int i=;i<=;i++)
{
int x=i;
for(int j=;x>=prime[j]&&j<;j++)
while(x%prime[j]==)
{
fact[i][j]++;
x/=prime[j];
}
}
for(int i=;i<=;i++)
{
for(int j=;j<;j++)
fact[i][j]+=fact[i-][j];
}
} void A6n()//dice[1]个1,dice[2]个2...时的种数=n!/(dice[1]!*dice[2]!*dice[3]!*dice[4]!*dice[5]!*dice[6]!)
{
ll x=;
int fn[],fnum[];
memcpy(fn,fact[n],sizeof(fn));
for(int i=;i<=;i++)//n!/dice[i]!
{
memcpy(fnum,fact[dice[i]],sizeof(fnum));
for(int j=;j<;j++)
fn[j]-=fnum[j];
}
for(int i=;i<;i++)
x*=pow(prime[i],fn[i]);
/*for(int i=1;i<=6;i++)
printf("%d ",dice[i]);
puts("");*/
fz+=x;//累加倒分子
} void C6n(int u,int n,int sum)//枚举所有可能
{
if(u==)
{
dice[u]=n;
sum+=u*n;
if(sum>=x) A6n();//和大于或等于x就加到分子中
return;
}
for(int i=;i<=n;i++)
{
dice[u]=i;
C6n(u+,n-i,sum+i*u);
}
} void slove()
{
if(*n<x)
{
puts("");
return;
}
fz=;
C6n(,n,);
int e2,e3;
e2=e3=n;//6^n=2^e2*2^e3
//printf("fz=%lld\n",fz);
while(fz%==)
{
fz>>=;
e2--;
if(!e2) break;//e2==0时要break掉,不然变成2的负数次方会wa
}
while(fz%==)
{
fz/=;
e3--;
if(!e3) break;
}
//printf("fz=%lld e2=%d e3=%d\n",fz,e2,e3);
fm=pow(,e3)*pow(,e2);
if(fz==) puts("");
else if(fm==) puts("");
else printf("%lld/%lld\n",fz,fm);
} int main()
{
//freopen("/home/user/桌面/in","r",stdin);
fact_table();
while(scanf("%d%d",&n,&x)==&&(n||x))
slove();
return ;
}
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring> using namespace std; typedef long long ll; int n,x;//n个dice,x为和
ll fz,fm,fact[][]; void fact_table()
{
memset(fact,,sizeof(fact));
for(int i=;i<=;i++)
fact[][i]=;
for(int i=;i<=;i++)//fact[i][j]表示i个骰子和为j的种数
for(int j=;j<=;j++)
for(int k=i-;k<=(i-)*;k++)
fact[i][k+j]+=fact[i-][k];
/*for(int i=1;i<=24;i++)
{
printf("%d: ",i);
for(int j=i;j<i*6;j++)
printf("%lld ",fact[i][j]);
printf("\n");
}*/
}
void slove()
{
int e2=n,e3=n;
fz=;
for(int i=x;i<=;i++)
fz+=fact[n][i];
while(fz%==)
{
fz>>=;
e2--;
if(!e2) break;
}
while(fz%==)
{
fz/=;
e3--;
if(!e3) break;
}
fm=pow(,e3)*pow(,e2);
if(fz==) puts("");
else if(fm==) puts("");
else printf("%lld/%lld\n",fz,fm);
} int main()
{
//freopen("in","r",stdin);
fact_table();
while(scanf("%d%d",&n,&x)==&&(n||x)) slove();
return ;
}
UVA 10759 Dice Throwing的更多相关文章
- 【UVA】10935 Throwing cards away I(STL队列)
题目 题目 分析 练习STL 代码 #include <bits/stdc++.h> using namespace std; int main() { int n; wh ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- hdu4405 Aeroplane chess
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 4405:Aeroplane chess(概率DP入门)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description Hzz loves ...
- hdu 4405Aeroplane chess(概率DP)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Aeroplane chess(HDU 4405)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 4405 Aeroplane chess(简单概率dp 求期望)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU-4405 Aeroplane chess
http://acm.hdu.edu.cn/showproblem.php?pid=4405 看了一下这个博客http://kicd.blog.163.com/blog/static/12696191 ...
- hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- WEB前端组件思想【分页】
DEMO1: 很早就想写一些功能性的组件,无奈技术有限一点一点的边工作,边学. 近日工作中用到分页功能,当然由于加快业务进度,第一选择肯定是选择插件,但是实用性来说,还是有那么一点不适合.毕竟插件是通 ...
- Servlet图片上传
package com.servlet; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io ...
- .Net Core 学习资料
官方网站:https://www.microsoft.com/net/core#windows 官方文档:https://docs.asp.net/en/latest/intro.html 中 ...
- Lightoj 1066 Gathering Food (bfs)
Description Winter is approaching! The weather is getting colder and days are becoming shorter. The ...
- MySQL DATE_FORMAT
MySQL DATE_FORMAT(date,format) 根据format字符串格式化date值 (在format字符串中可用标志符: %M 月名字(January……December) %W ...
- 711B - Chris and Magic Square 模拟
题目大意:在num[i][j]==0处填一个数使每行,每列,对角线的和相同,若果有多种答案输出一种. 题目思路:模拟 #include<iostream> #include<algo ...
- touchesBegan: withEvent: <--- with UIScrollView / UIImageView
touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对 ...
- Minigui3.0 自定义遥控输入引擎
本人最近在从事minigui的开发工作,使用的gui版本为最新的3.0.12,平台环境为海思的HI3515. 在历经千辛万苦,终于将gui成功的移植到了开发板上,这里不多赘述,没有移植成功的朋友可以点 ...
- 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭 引用: http://www.cnblogs.com/maxao/archive/2011/03/18/19881 ...
- python常用函数年初大总结
1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(obj) 得到o ...