题解:P5589 小猪佩奇玩游戏

· · 题解

简单题。

我们考虑一个数 x 何时会产生贡献,容易发现当且仅当所有能删掉 x 的数都在 x 之后被删除,设这样的数个数为 c_x,则 x 的贡献为 \frac{1}{c_x+1},故 ans=\sum_{x=1}^{n}\frac{1}{c_x+1}。容易发现 \sum c_x 不会太大,因此暴力枚举计算 c_x 即可,使用 map 存储。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3fll

ll n;
map<int,int> mp;

inline void Solve()
{
    cin >> n;
    mp.clear();
    for(int i=2;pow(2ll,i)<=n;i++)
    {
        ll now=2;
        while(pow(now,i)<=n) mp[pow(now++,i)]++;
    }
    double ans=n;
    for(auto [x,y]:mp) ans=ans-1+1.0/(y+1);
    printf("%.4lf\n",ans);
}

int main()
{
    // int t=clock();
    // freopen("gdx.in","r",stdin);
    // freopen("gdx.out","w",stdout);
    // ios::sync_with_stdio(0);
    // cin.tie(0);
    // cout.tie(0);
    // int Type;
    // cin >> Type;
    int T;
    cin >> T;
    while(T--)
    Solve();
    // cerr << clock()-t << '\n';
    return 0;
}