题意为抛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的更多相关文章

  1. 【UVA】10935 Throwing cards away I(STL队列)

    题目 题目     分析 练习STL     代码 #include <bits/stdc++.h> using namespace std; int main() { int n; wh ...

  2. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  3. hdu4405 Aeroplane chess

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. HDU 4405:Aeroplane chess(概率DP入门)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description   Hzz loves ...

  5. hdu 4405Aeroplane chess(概率DP)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. Aeroplane chess(HDU 4405)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. HDU-4405 Aeroplane chess

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 看了一下这个博客http://kicd.blog.163.com/blog/static/12696191 ...

  9. hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. jquery 画板折叠

    <!doctype html><html lang="en"><head> <meta charset="utf-8" ...

  2. Java Object 引用传递和值传递

    Java Object 引用传递和值传递 @author ixenos Java没有引用传递: 除了在将参数传递给方法(或函数)的时候是"值传递",传递对象引用的副本,在任何用&q ...

  3. 浙大pat 1029题解

    1029. Median (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incre ...

  4. sqlplus登录Oracle时ORA-01017: invalid username/password; logon denied的错误

    今天用scott用户登录Oracle数 据库时,竟然出现了ORA-01017: invalid username/password; logon denied错误,原以为是因为我的scott用户没有解 ...

  5. ejabberd组成模块

    转自:http://blog.sina.com.cn/u/1776260990 mod_adhoc 特定命令 (XEP-0050) mod_announce 管理公告推荐 mod_adhoc mod_ ...

  6. 修改TFS与本地源代码映射路径

    使用源代码管理资源管理器修改工作区 在“文件”菜单上单击“源代码管理”,再单击“工作区”. 在“管理工作区”对话框的“名称”列下,突出显示要修改的工作区,然后单击“编辑”. 在“编辑工作区”对话框中: ...

  7. hive深入

    Hive QL: Create Table 创建一个指定名字的表.如果相同名字的表已经存在,则抛出异常:用户可以用 IF NOT EXIST 选项来忽略这个异常. EXTERNAL 关键字可以让用户创 ...

  8. HDU 5933/思维

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=5933]; 题意: 给出n堆物品,问能不能分成K个数量相等的堆,如果能分,则最少次数是多少.分的规则为: ...

  9. wireshark 包过滤

    tcp.port == 443 or udp.port==443 or tcp.port==53 or udp.port==53

  10. android基础知识点复习之短信发送

    界面布局: activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...