Throwing Dice LightOJ-1064 | | (Barely available) fractional class

Source: Internet
Author: User
Tags gcd

Throwing Dice LightOJ-1064

Method:

Set ANS[I][J] Indicates the probability that I have a dice point that is exactly J. Then ans[1][1] to ans[1][6] are 1/6.

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

The upper limit of N and X is very small, directly processing the result of all the points exactly a certain value, and then do again like the prefix and things to deal with all points greater than or equal to a value of the result. Here the answer needs a score, so we write a fractional structure.

Fractional operations (not optimized):

Initialize empty score: Numerator is 0, denominator is 1

Addition:-Pass first, then add molecule, then numerator

Multiplication: Numerator each other first, then multiplying the numerator by the denominator

Error number: 1 times

Error reason: Because the prefix and the scope limit of the original data is mixed, 66 lines >=0 written >=i, resulting in WA

1#include <cstdio>2typedefLong LongLL;3 ll gcd (ll A,ll b)4 {5 LL t;6      while(b!=0)7     {8t=A;9A=b;Tenb=t%b; One     } A     returnA; - } - structX the { - LL A, b; - X () -     { +A=0; b=1; -     } +XintXinty) A     { ata=x;b=y; -     } -Xoperator+(Constx&c) -     { - X ans; -LL tmp=gcd (B,C.B); inans.b=b/tmp*c.b; -ans.a=ans.b/b*a+ans.b/c.b*C.A; totmp=gcd (ans.a,ans.b); +Ans.a/=tmp; -Ans.b/=tmp; the         returnans; *     } $Xoperator*(Constx&c)Panax Notoginseng     { -LL TMP1=GCD (a,c.b), tmp2=gcd (B,C.A); the X ans; +ans.a=a/tmp1*c.a/TMP2; Aans.b=b/tmp2*c.b/Tmp1; the         returnans; +     } -     voidprint () $     { $         if(b==1) -printf"%lld", a); -         Else theprintf"%lld/%lld", A, b); -     }Wuyi}ans[ -][151],ans2[ -][151]; the LL t,tt,n,x; - intMain () Wu { -     inti,j,k; About      for(i=1; i<=6; i++) $ans[1][i]= (X) {1,6}; -      for(i=2; i<= -; i++) -          for(j=i;j<=6*i;j++) -              for(k=1; k<=6; k++) A                 if(j-k>0) +ans[i][j]=ans[i][j]+ans[i-1][j-k]*ans[1][1];//ans[1][1] is 1/6 the      for(i=1; i<= -; i++) -     { $ans2[i][6*i]=ans[i][6*i]; the          for(j=6*i-1; j>=0; j--) theans2[i][j]=ans2[i][j+1]+Ans[i][j]; the     } thescanf"%lld",&T); -      for(tt=1; tt<=t;tt++) in     { thescanf"%lld%lld",&n,&x); theprintf"Case %LLD:", TT); About ans2[n][x].print (); thePuts""); the     } the     return 0; +}

Throwing Dice LightOJ-1064 | | (Barely available) fractional class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.