[hdu 6239 Interview]数学OR打表 - Fighting! - CSDN博客

原题链接:Interview(hdoj/hdu6239) ?
文章价值(3) ?
阅读数(458) ?

[hdu 6239 Interview]数学OR打表

分类:Math

1. 题目链接

[hdu 6239 Interview]

2. 题意描述

题意描述

3. 解题思路

明明知道公式似乎并不是很难,但是怎么就是推不对。
最后投机取巧的打了个表,然后就很容易找出规律。
概率论太差呀,很难想象这种题目在现场赛竟然是道金牌题?

4. 实现代码

#include 
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair pll;

const int inf = 0x3f3f3f3f;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
template<typename T> inline void umax(T &a, T b) { a = max(a, b); }
template<typename T> inline void umin(T &a, T b) { a = min(a, b); }
void debug() { cout << endl; }
template<typename T, typename ...R> void debug (T f, R ...r) { cout << "[" << f << "]"; debug (r...); }

const ll MOD = 1e9 + 7;

ll qpow(ll a, ll b) {
    ll ret = 1;
    a %= MOD;
    while (b > 0) {
        if (b & 1) ret = ret * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ret;
}

ll solve1(ll n) {
    vector<int> r(n + 1);
    for (int i = 1; i <= n; ++i) {
        r[i] = i;
    }
    ll up = 0, dw = 0;
    do {
        for (int k = 0; k <= n; ++k) {
            if (r[1] <= k || r[2] > k) continue;
            ++ dw;
            up += (r[1] - k);
        }
    } while (next_permutation(r.begin() + 1, r.end()));
    debug(n, up, dw);
    return up * qpow(dw, MOD - 2) % MOD;
}

ll solve2(ll n) {
    vector<int> r(n + 1);
    for (int i = 1; i <= n; ++i) {
        r[i] = i;
    }
    ll up = 0, dw = 0;
    do {
        for (int k = 0; k <= n; ++k) {
            if (r[1] <= k || r[2] <= k) continue;
            ++ dw;
            up += (r[1] - k);
        }
    } while (next_permutation(r.begin() + 1, r.end()));
    debug(n, up, dw);
    return up * qpow(dw, MOD - 2) % MOD;
}


int main() {
#ifdef ___LOCAL_WONZY___
    freopen("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
    // std::vector v(1000);
    // for(ll n = 1; n <= 15; ++n) {
    //     v[n] = solve1(n);
    // }
    // return 0;
    int T; cin >> T;
    while (T --) {
        ll n, d; cin >> n >> d;
        ll ans;
        if (d == 1) {
            ans = (n + 2) * qpow(4, MOD - 2) % MOD;
        } else {
            // ans = solve2(n);
            ans = 3ll * (n + 2) % MOD * qpow(8, MOD - 2) % MOD;
        }
        printf("%lld\n", ans);
    }
#ifdef ___LOCAL_WONZY___
    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << "ms." << endl;
#endif // ___LOCAL_WONZY___
    return 0;
}




(注:此网站文章大都来自公开的博客,如有原作者发现有侵权问题,请在此留言,我们会立刻删除相关文章。)

我的评价: