[UVA][DP] 10759 - Dice Throwing
Problem A
Dice Throwing
Input: standard input
Output: standard output
Time Limit: 1 second
n common cubic dice are thrown. What is the probability that the sum of all thrown dice is at least x?
Input
The input file contains several test cases. Each test case consists two integers n (1<=n<=24) and x(0<=x<150). The meanings of n and x are given in the problem statement. Input is terminated by a case where n=0 and x=0. This case should not be processed.
Output
For each line of input produce one line of output giving the requested probability as a proper fraction in lowest terms in the format shown in the sample output. All numbers appearing in output are representable in unsigned 64-bit integers. The last line of input contains two zeros and it should not be processed.
Sample Input Output for Sample Input
3 9 1 7 24 24 15 76 24 56 24 143 23 81 7 38 0 0 |
20/27 0 1 11703055/78364164096 789532654692658645/789730223053602816 25/4738381338321616896 1/2
55/46656 |
Problem setter: Poitr Rudnicki, University of Alberta, Canada
#include <stdio.h>
typedef unsigned long long ULL;
ULL gcd(ULL x, ULL y) {
if(x == 0) return y;
if(y == 0) return x;
ULL tmp;
while(x%y) {
tmp = x, x = y, y = tmp%y;
}
return y;
}
int main() {
ULL DP[25][151] = {}, total[25];
int i, j, k, n, m;
DP[0][0] = 1, total[0] = 1;
for(i = 1; i <= 24; i++) {
for(j = 6*i; j >= i; j--) {
for(k = 1; k <= 6 && j-k >= 0; k++)
DP[i][j] += DP[i-1][j-k];
}
total[i] = total[i-1]*6;
}
for(i = 1; i <= 24; i++) {
for(j = 6*i; j >= 0; j--) {
DP[i][j] += DP[i][j+1];
}
}
while(scanf("%d %d", &n, &m) == 2) {
if(n == 0 && m == 0)
break;
ULL x = DP[n][m], y = total[n];
ULL GCD = gcd(x, y);
x /= GCD;
y /= GCD;
if(y == 1)
printf("%llu\n", x);
else
printf("%llu/%llu\n", x, y);
}
return 0;
}