【題解】CSES 1725 Dice Probability

【題目敘述】https://cses.fi/problemset/task/1725

#include <iostream>
#include <iomanip>
using namespace std;
 
int n, a, b;
double dp[605], ans;
 
int main() {
    cin >> n >> a >> b;
    dp[0] = 1;
    for (int i = 1; i <= n; i++){
        for (int j = 6*i; j >= 0; j--){
            dp[j] = 0;
            for (int k = 1; k <= 6; k++){
                if (j-k < 0) break;
                dp[j] += dp[j-k];
            }
            dp[j] /= 6.0;
        }
    }
    for (int i = a; i <= b; i++){
        ans += dp[i];
    }
    cout << fixed << setprecision(6) << ans << "\n";
}
分享本文 Share with friends