HDU 2204 - Eddy's爱好 (容斥原理)

思路

可知指数要为素数,不然就可以拆分成多个数的幂。

而且幂最大为59,因为$2^{60} > 1e18$

然后对n开pri[i]次幂,这样得出来的数字是n以内有几个数能被某个数的pri[i]次幂表示。

但是这样得出来的结果是由重复的,比如$4^3$和$8^2$。造成这样的原因是他们都可以表示成$2^{2*3}$。

那么就可以根据容斥原理,奇数的时候加,偶数的时候减。因为当有四位素数时,$2 * 3 * 5 * 7 >= 60$,超出了题目范围,所以不予考虑。

注意要加一个eps。

代码

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define LL long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-8;
const int MAXN = 6000 + 10;
const int MOD = 1000007;
 
typedef pair<int, int> pii;
typedef vector<int>::iterator viti;
typedef vector<pii>::iterator vitii;
 
int c, vis[MAXN];
LL n, ans, pri[MAXN];
 
 
void DFS(int curPos, int curPro, int curCnt)
{
    if (curCnt == 4) return;
    for (int i = curPos; i < 18; i++)
    {
        if (pri[i] * curPro >= 60) return;
        LL tmp = (LL)(pow(n, 1.0 / (pri[i] * curPro)) + eps);
        if (tmp < 2) return;
        if (curCnt & 1) ans += tmp - 1;
        else ans -= tmp - 1;
        DFS(i + 1, curPro * pri[i], curCnt + 1);
    }
}
 
void GetPrime()
{
    c = 0;
    int m = sqrt(62.5);
    for (int i = 2; i <= 5000; i++)
        if (!vis[i])
        {
            pri[c++] = i;
            for (LL j = i * i; j < 5000; j += i)
            {
                vis[j] = 1;
            }
        }
}
 
int main()
{
    //ROP;
    int i, j;
    GetPrime();
    while (cin >> n)
    {
        ans = 0;
        DFS(0, 1, 1);
        cout << ans + 1 << endl;
    }
    return 0;
}

Powered by Jekyll and Theme by solid