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)