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. Jenkins+appium+testng持续集成

    Create maven project in eclipseAdd Appium , Selenium dependancyAdd Test in TestNG testCreate TestNG ...

  2. bash_profile打不开怎么办,用nano .bash_profile打开

    I’ve spent years curating a collection of Mac bash aliases and shortcuts to make my life easier. My ...

  3. poj 2264 Advanced Fruits(DP)

    Advanced Fruits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1944   Accepted: 967   ...

  4. vc6.0的一些快捷键

    1.检测程序中的括号是否匹配    把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键“Ctrl+]”.如果括号匹配正确,光标就跳到匹配的括号处 ...

  5. android5.0(Lollipop) BLE Peripheral牛刀小试

    转载请表明作者:http://blog.csdn.net/lansefeiyang08/article/details/46468743 知道Android L对蓝牙对了一些改进.包含加入A2dp s ...

  6. 在Android Studio中修改应用包名

    紧凑模式下(包名中的每个字段紧贴在一起,例如),右键单击包名,Refactor -> Rename,只能修改包名最外层的字段 分离模式下(点击设置,将Hide Empty Middle Pack ...

  7. SpringMVC 学习笔记(四) 处理模型数据

    Spring MVC 提供了下面几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体就可以通过该对象加入模型数据 – Map及Model: ...

  8. oracle中去掉文本中的换行符、回车符、制表符

    一.特殊符号ascii定义 制表符 chr(9)  换行符 chr(10) 回车符 chr(13) UPDATE tc_car_order set USE_REASON =  REPLACE('USE ...

  9. Redis实现中间件(订阅)

    什么是消息中间件 发布订阅 点对点   消息中间件本身是异步的通讯 案例:使用redis实现发布订阅功能 Redis发布订阅 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub) ...

  10. DedeCms如何调用Discuz论坛主题等数据方法总结

    DedeCms如何调用Discuz论坛主题等数据方法总结 同时使用Dedecms和Discuz论坛的朋友,难免要在网站内调用论坛的内容.使用Discuz论坛的JS调用方式,对搜索引擎不够友好,下面我们 ...