HDU- 6861 Gaming of Co-prime Disallowance (概率,莫比乌斯容斥)

HDU- 6861 Gaming of Co-prime Disallowance (概率,莫比乌斯容斥)

题面描述:

HDU- 6861 Gaming of Co-prime Disallowance (概率,莫比乌斯容斥)

思路:

我们考虑在一次游戏中\(gcd=1\)之前的\(gcd\)数值为\(g,cnt_x\)为\(\mathit x\) 的倍数个数。

\[f(x,k)=\frac{cnt_x-k}{n-k} \]

代表第\(\mathit k\) 次取到\(\mathit x\) 的倍数概率。

那么在游戏中\(gcd=1\)之前的\(gcd\)数值为\(g\) ,Cuber 输掉的概率为:

\[\sum_{2|i,i \leq n}{\frac{n-cnt_g}{n-k}\prod_{j=1}^i f(g,j)} \]

即前偶数次操作时,双方都取的是\(\mathit g\) 的倍数,然后先手取了一个非\(\mathit g\) 的倍数,然后先手输掉。

那么我们枚举\(\mathit g\)的所有可能取值,计算答案即可。

这里还有注意,合数是多个质数的倍数,所以会被多次计算,我们用莫比乌斯函数\(\mu(g)\)作为最大公约数\(\mathit g\) 对答案的概率的贡献即可。

得到的是Cuber输掉的概率,用1减去一下就是赢的答案了。

注意代码需要预处理一些信息:

我们可以\(m=max\{a_i\},O(m*logm)\)的时间复杂度,预处理\([1,m]\)的所有因子。

\(n=100,O(n^2)\) 得到 \(f[i][j];\)// i个gcd的倍数,j个非gcd的倍数时,先手输掉的概率

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
std::vector<int> fac[maxn];
long double f[110][110];// i个gcd的倍数,j个非gcd的倍数时,先手输掉的概率

void init()
{
    repd(i, 2, maxn - 1)
    {
        for (int j = i; j < maxn; j += i)
        {
            fac[j].push_back(i);
        }
    }
    for (int i = 2; i <= 100; ++i)
    {
        for (int j = 1; i + j <= 100; ++j)
        {
            long double p = 1;
            int num = i;
            for (int k = 1; k <= i; ++k)
            {
                p *= (1.0l * num) / (num + j);
                num--;
                if (k % 2 == 0)
                {
                    f[i][j] += p * (1.0l * j / (num + j));
                }
            }
        }
    }
}
bool vis[maxn];
long long prim[maxn], mu[maxn], sum[maxn], cnt;
void get_mu(long long n)
{
    mu[1] = 1;
    for (long long i = 2; i <= n; i++)
    {
        if (!vis[i]) {mu[i] = -1; prim[++cnt] = i;}
        for (long long j = 1; j <= cnt && i * prim[j] <= n; j++)
        {
            vis[i * prim[j]] = 1;
            if (i % prim[j] == 0)break;
            else mu[i * prim[j]] = -mu[i];
        }
    }
    // for (long long i = 1; i <= n; i++)sum[i] = sum[i - 1] + mu[i];
}
int a[maxn];
int cnt_f[maxn];
int main()
{
#if DEBUG_Switch
    freopen("C:\\code\\input.txt", "r", stdin);
#endif
    //freopen("C:\\code\\output.txt","w",stdout);
    int t;
    t = readint();
    init();
    get_mu(maxn - 1);
    while (t--)
    {
        int n = readint();
        int cnt1 = 0;
        repd(i, 1, n)
        {
            a[i] = readint();
            if (a[i] == 1)
                cnt1++;
        }
        int g = a[1];
        repd(i, 2, n)
        {
            g = gcd(g, a[i]);
        }
        if (g > 1)
        {
            if (n & 1)
            {
                printf("%.6f\n", 1.);
            } else
            {
                printf("%.6f\n", 0.);
            }
            continue;
        }
        repd(i, 1, n)
        {
            for (auto &x : fac[a[i]])
            {
                cnt_f[x]++;
            }
        }
        long double ans = 1.0l * cnt1 / n;
        repd(i, 2, 100000)
        {
            ans += -1.0l * mu[i] * f[cnt_f[i]][n - cnt_f[i]];
            cnt_f[i] = 0;
        }
        printf("%.9f\n", 1.0 - (double)ans );
    }

    return 0;
}


上一篇:牛客练习赛65 - A 最值序列 (贪心)


下一篇:正体不明