Throwing Dice LightOJ - 1064

方法:

设ans[i][j]表示i个骰子点数恰好为j的概率。那么ans[1][1]到ans[1][6]都为1/6。

显然,$ans[i][j]=sum\{ans[i-1][j-k]\}(1<=k<=6,j>k)$

n和x上限很小,直接处理出所有点数恰好为某个值的结果,然后再做一遍类似前缀和的东西处理出所有点数大于等于某个值的结果。这里答案需要分数,于是乱写了一个分数结构体。

分数运算(没有优化的):

初始化空的分数:分子为0,分母为1

加法:先通分,然后加分子,然后约分

乘法:先互相约分,然后分子分母分别相乘

错误次数:1次

错误原因:由于把前缀和的范围限制与原数据的范围限制搞混,66行>=0写成>=i,导致WA

 #include<cstdio>
typedef long long LL;
LL gcd(LL a,LL b)
{
LL t;
while(b!=)
{
t=a;
a=b;
b=t%b;
}
return a;
}
struct X
{
LL a,b;
X()
{
a=;b=;
}
X(int x,int y)
{
a=x;b=y;
}
X operator+(const X& c)
{
X ans;
LL tmp=gcd(b,c.b);
ans.b=b/tmp*c.b;
ans.a=ans.b/b*a+ans.b/c.b*c.a;
tmp=gcd(ans.a,ans.b);
ans.a/=tmp;
ans.b/=tmp;
return ans;
}
X operator*(const X& c)
{
LL tmp1=gcd(a,c.b),tmp2=gcd(b,c.a);
X ans;
ans.a=a/tmp1*c.a/tmp2;
ans.b=b/tmp2*c.b/tmp1;
return ans;
}
void print()
{
if(b==)
printf("%lld",a);
else
printf("%lld/%lld",a,b);
}
}ans[][],ans2[][];
LL T,TT,n,x;
int main()
{
int i,j,k;
for(i=;i<=;i++)
ans[][i]=(X){,};
for(i=;i<=;i++)
for(j=i;j<=*i;j++)
for(k=;k<=;k++)
if(j-k>)
ans[i][j]=ans[i][j]+ans[i-][j-k]*ans[][];//ans[1][1]就是1/6
for(i=;i<=;i++)
{
ans2[i][*i]=ans[i][*i];
for(j=*i-;j>=;j--)
ans2[i][j]=ans2[i][j+]+ans[i][j];
}
scanf("%lld",&T);
for(TT=;TT<=T;TT++)
{
scanf("%lld%lld",&n,&x);
printf("Case %lld: ",TT);
ans2[n][x].print();
puts("");
}
return ;
}

Throwing Dice LightOJ - 1064 || (勉强能用的)分数类的更多相关文章

  1. Throwing Dice(概率dp)

    C - Throwing Dice Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Lig ...

  2. lightoj 1064 Throwing Dice

    题意:给你n个骰子,求n个骰子的和不小于x的概率. 刚开始想每给一组数就计算一次~~太笨了- -,看了别人的代码,用dp,而且是一次就初始化完成,每次取对应的数据就行了.WA了好多次啊,首先不明白的就 ...

  3. Light OJ 1064 - Throwing Dice

    题目大意: 给你n个骰子, 问点数大于等于x的概率是多少? #include<cstdio> #include<cstring> #include<iostream> ...

  4. LightOJ1064 Throwing Dice(DP)

    第一眼以为是概率DP,我还不会.不过看题目那么短就读读,其实这应该还不是概率DP,只是个水水的DP.. dp[n][s]表示掷n次骰子点数和为s的情况数 dp[0][0]=1 dp[i][j]=∑dp ...

  5. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  6. 10409 - Die Game

    Problem G: Die Game Life is not easy. Sometimes it is beyond your control. Now, as contestants of AC ...

  7. UVA 657 The die is cast

      The die is cast  InterGames is a high-tech startup company that specializes in developing technolo ...

  8. The Die Is Cast(poj 1481简单的双dfs)

    http://poj.org/problem?id=1481 The Die Is Cast Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  9. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

随机推荐

  1. Quartz深入浅出(二)

    Hello Quartz / 本文通过一个简单的样例让大家高速了解Quartz,上手.并了解Quartz内的一些关键对象 如 Scheduler.Job.Trigger.JobExecutionCon ...

  2. MessageBox_ swt

    SWT有不同类型的对话框.有些对话框具有特殊的属性. MessageBox messageBox = new MessageBox(shell, SWT.OK|SWT.CANCEL); if (mes ...

  3. ruby 学习网站

    Ruby on Rails官网: http://rubyonrails.org/ Rails Guides:http://guides.rubyonrails.org/ -中文版: http://gu ...

  4. iOS——多线程编程详细解析

    基本定义: 程序:由代码生成的可执行应用.(例如QQ.app) 进程:一个正在运行的程序可以看做是一个进程. (例如:正在运行的QQ 就是一个进程),进程拥有独立运行所需要的全部资源. 线程: 程序中 ...

  5. Oracle dbf文件删除后,启动服务ORA-01157问题

    有一个数据库硬盘空间满了,查看发现一个dbf超大,并且不在规定的路径下,知道是一个非重要数据文件,于是删除. 后来重启数据库时, SQL> startupORACLE instance star ...

  6. g00 网站说明

    最近在做dns tunnel检测,发现了一堆类似这样的域名:c-6rtwjumjzx7877x24uwjkjwjshjx78x2eywzx78yjx2ehtr.g00.medicinenet.com ...

  7. mysql 5.5升级到5.7版本操作流程

    一.备份原来 phpStudy 中 MySQL 安装目录 二.把下载的 MySQL 压缩文件解压至 phpStudy 下的 MySQL目录,复制 my-default.ini ,重命名为 my.ini ...

  8. 分段控制器--UISegmentedControl 基本用法

    http://blog.csdn.net/heng615975867/article/details/43527295 http://blog.csdn.net/gf771115/article/de ...

  9. 一个点亮屏幕的service

    这个版本是只能点亮不能解锁的版本(注意很多句子都被注释掉了,那部分是用来实现解锁屏幕的),达到了预期的效果,特此纪念. 把代码贴出来: package com.larry.msglighter; im ...

  10. java8的常用的新特征

    一.Java 8 对接口做了进一步的增强.(默认方法和静态方法) a. 在接口中可以添加使用 default 关键字修饰的非抽象方法.即:默认方法(或扩展方法不用被实现)如:comparator接口. ...