HDU-2204-Eddy's hobby - seeking inclusion and exclusion number within the number n of the form M ^ K

HDU-2204-Eddy's hobby - seeking inclusion and exclusion number within the number n of the form M ^ K


【Problem Description】

slightly

【Solution】

For an index \ (k \) , to find one of the biggest \ (m \) so that \ (m ^ k \ Le the n-\) , then \ (k \) contribution to the index of the answer is \ (m \) , because for \ (i \ in [1, m] \) of the number \ (i ^ k \) constant less \ (n-\) . And \ (^ {n-m = \ FRAC. 1 {{}}} K \) . The only apparent from decomposition theorem, \ (K \) constant can be expressed as the product of a number of prime numbers. We only need to consider \ (64 \) within a prime number can be. Duplicate value but will, for example, \ (^ 2 =. 8. 4. 3 ^ 2 = ^ {2 \}. 3 Times \) , it is necessary to re-repellent capacity can be used.


【Code】

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 65
int prime[maxn],cnt=0;
bool vis[maxn]={1,1};
void Euler(){ //欧拉筛素数
    for(int i=2;i<maxn;i++){
        if(!vis[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<maxn;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int n,ans=0;
int fpow(int a,int b){ //快速幂
    int ans=1;
    while(b){
        if(b&1) ans*=a;
        a*=a;
        b>>=1;
    }
    return ans;
}
void dfs(int pos,int num,int val){ //容斥
    if(val>64) return ; //最大值不超过64
    int tmp=pow(n,1.0/val)+0.1; //求最大的m
    if(fpow(tmp,val)>n) tmp--;tmp--; //精度判断
    if(num) if(num&1) ans+=tmp;
    else ans-=tmp;
    for(int i=pos+1;i<=cnt;i++){
        dfs(i,num+1,val*prime[i]);
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);Euler();
    while(cin>>n){
        ans=1;//1一定满足条件
        dfs(0,0,1);
        cout<<ans<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/--Simon/p/11637237.html