Uva10759-dice Throwing (DP+GCD)

Source: Internet
Author: User

Uva10759-dice Throwing (DP+GCD)

Topic links

The main idea: n Dice, the sum of the N boson is not less than the probability of X.

Problem-solving ideas: Because the topic can be converted to n dice and less than X number, and then 6^n minus the number is greater than or equal to the number of X. Because n is the maximum of 24, this can still be used long long to store, the final output required is the fractional form, the numerator denominator with gcd numerator output.

Code:

#include <cstdio>#include <cstring>typedef Long LongllConst intMAXN = -;Const intMAXM =155;intN, X;ll F[MAXN][MAXM], T[MAXN];voidInit () {t[0] =1LL for(inti =1; i < MAXN; i++) T[i] = t[i-1] *6;} ll GCD (ll A, ll b) {returnb = =0? A:GCD (b, a%b);} LL DP (intNintx) {ll& ans = f[n][x];if(ans! =-1)returnAnsif(n = = N) {if(x < x)returnAns =1;returnAns =0; } ans =0; for(inti =1; I <=6; i++)if(x + i < x) ans + = DP (n +1, x + i);Else             Break;returnAns;}intMain () {init (); while(scanf("%d%d", &n, &x) && (n| | X)) {memset(F,-1,sizeof(f)); ll a = T[n]-DP (0,0);if(A = =0) {printf("0\n");Continue; }if(A = = T[n]) {printf("1\n");Continue; } ll tmp = GCD (A, t[n]);printf("%lld/%lld\n", a/tmp, t[n]/tmp); }}

Uva10759-dice Throwing (DP+GCD)

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.