HDOJ4652 Dice #概率DP#

Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1119    Accepted Submission(s): 698
Special Judge

Problem Description

You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form:
0 m n: ask for the expected number of tosses until the last n times results are all same.
1 m n: ask for the expected number of tosses until the last n consecutive results are pairwise different.

Input

The first line contains a number T.(1≤T≤100) The next T line each line contains a query as we mentioned above. (1≤m,n≤106) For second kind query, we guarantee n≤m. And in order to avoid potential precision issue, we guarantee the result for our query will not exceeding 109 in this problem.

Output

For each query, output the corresponding result. The answer will be considered correct if the absolute or relative error doesn't exceed 10-6.

Sample Input

6 0 6 1 0 6 3 0 6 5 1 6 2 1 6 4 1 6 6 10 1 4534 25 1 1232 24 1 3213 15 1 4343 24 1 4343 9 1 65467 123 1 43434 100 1 34344 9 1 10001 15 1 1000000 2000

Sample Output

1.000000000 43.000000000 1555.000000000 2.200000000 7.600000000 83.200000000 25.586315824 26.015990037 15.176341160 24.541045769 9.027721917 127.908330426 103.975455253 9.003495515 15.056204472 4731.706620396

Source

Recommend

zhuyuanchen520

Solution

参考 https://www.cnblogs.com/cjyyb/p/8668964.html#4492955

P.S. 快读被卡 原因未知

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

template<typename T = int>
inline const T read() {
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

ll qpow(ll x, ll k) {
    ll res = 1;
    while (k)
    {
        if (k & 1) res = res * x;
        x = x * x; k >>= 1;
    }
    return res;
}

double solve1(int m, int n) {
    return (1.0 - qpow(m, n)) / (1.0 - m);
}

double solve2(int m, int n) {
    double res = 1.0, d = 1.0;
    for (int i = 1; i < n; i++) {
        d = d * m / (m - i);
        res += d;
    }
    return res;
}

int main() {
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    int t, opt, m, n;
    while (cin >> t) {
        while (t--) {
            cin >> opt >> m >> n;
            printf("%.9f\n", opt ? solve2(m, n) : solve1(m, n));
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/104190823