HDU-2204 Eddy's爱好

HDU-2204

题意:

  给你一个正整数N,确定在1到N之间有多少个可以表示成M^K(K>1)的数。

思路:

  我们可以由n^(1/p),知道指数为p的有多少个数。

  通过观察,可以发现若一个数可以表示成x^(k*t),则可以表示成(x^k)^t。因此指数必然为素数。

  枚举素数便可以得到指数为p的个数,但是可能出现重复,例如:x^3=y^5,其中x=t^5,y=t^3。

  运用容斥原理,设a[i]表示指数为第i个素数的个数,那么答案等于满足一个的,减去两个的,加上三个的……

  由于2^60>10^18,2*3*5*7>60,所以只要枚举到三即可。

参考

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

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

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);



template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
            int prime[100],p[100],tot;
            void init(){
                for(int i=2; i<=66; i++){
                    if(prime[i] == 0){
                        p[++tot] = i;
                    }
                    for(int j=1; j<=tot && i*p[j] <=66; j++){
                        prime[i*p[j]] = 1;
                        if(i%p[j] == 0)break;
                    }
                }
            }
int main(){
            ll n,ans;
            init();
            while(~scanf("%lld", &n)){
                ll ans = 0;
                for(int i=1; i<=tot; i++){
                    ll tmp = (ll) (pow((double)n , 1.0/p[i])+esp);
                    if(tmp==1)break;
                    ans += tmp - 1;
                }

                for(int i=1; i<=tot; i++){
                    for(int j=i+1; j<=tot; j++){
                        ll tmp = (ll) (pow((double) n, 1.0/ (p[i]*p[j])) + esp);
                        if(tmp == 1)break;
                        ans -= tmp - 1;
                    }
                }

                for(int i=1; i<=tot; i++){
                    for(int j=i+1; j<=tot; j++){
                        for(int k=j+1; k<=tot; k++){
                            ll tmp = (ll) (pow((double) n, 1.0/(p[i]*p[j]*p[k])) + esp);
                            if(tmp==1)break;
                            ans += tmp - 1;
                        }
                    }
                }
                printf("%lld\n",ans+1);
            }
            return 0;
}
HDU - 2204

猜你喜欢

转载自www.cnblogs.com/ckxkexing/p/9502727.html
今日推荐