hdu 2204 容斥原理

分析:

根据指数乘法原理: xab=xab
所以指数必须是质数,切质数显然应该小于等于 263
但是这些质数并不是完全互质的,所以要用容斥原理来做。
这里计算一个指数有多少个可行的数使用的开根号运算符 pow(n,1.0/x)+1e8 加上浮点误差避免。

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define pr(x) cout << #x << ": " << x << "  " 
#define pl(x) cout << #x << ": " << x << endl;
typedef long long ll;

struct jibancanyang
{
    ll n, ans;
    int prime[100], pCnt;

    void getPrime() {
        bool isprime[100]; 
        pCnt = 0;
        memset(isprime, 0, sizeof(isprime));
        for (int i = 2; i < 64; ++i) {
            if (!isprime[i]) {
                prime[pCnt++] = i;
                for (int j = i + i; j < 64; j += i) isprime[j] = true;
            }
        }
    }

    void dfs(int cur, ll x, int p) {
        if (x >= 63 || 1ll << x > n) return;
        //pr(cur), pl(x);
        if (cur == pCnt) {
            if (x != 1) ans += p * (int(pow(n, 1.0 / x) + 1e-9) - 1);
            return;
        }
        dfs(cur + 1, x * prime[cur], -p);
        dfs(cur + 1, x, p);
    }

    void fun() {
        getPrime();
        while (~scanf("%lld", &n)) {
            ans = 0;
            dfs(0, 1, -1);
            printf("%lld\n", ans + 1);
        }

    }

}ac;

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    ac.fun();
    return 0;
}

你可能感兴趣的:(ACM_组合数学)