Educational Codeforces Round 50 (Rated for Div. 2)F. Relatively Prime Powers

实际上就是求在[2,n]中,x != a^b的个数,那么实际上就是要求x=a^b的个数,然后用总数减掉就好了。

直接开方求和显然会有重复的数。容斥搞一下,但实际上是要用到莫比乌斯函数的,另外要注意减掉1^b这种情况。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <stack>
using namespace std;
typedef long long ll;
#define T int t_;Read(t_);while(t_--)
#define dight(chr) (chr>='0'&&chr<='9')
#define alpha(chr) (chr>='a'&&chr<='z')
#define INF (0x3f3f3f3f)
#define maxn (300005)
#define maxm (10005)
#define mod 1000000007
#define ull unsigned long long
#define repne(x,y,i) for(i=(x);i<(y);++i)
#define repe(x,y,i) for(i=(x);i<=(y);++i)
#define repde(x,y,i) for(i=(x);i>=(y);--i)
#define repdne(x,y,i) for(i=(x);i>(y);--i)
#define ri register int
inline void Read(int &n){char chr=getchar(),sign=1;for(;!dight(chr);chr=getchar())if(chr=='-')sign=-1;
    for(n=0;dight(chr);chr=getchar())n=n*10+chr-'0';n*=sign;}
inline void Read(ll &n){char chr=getchar(),sign=1;for(;!dight(chr);chr=getchar())if
    (chr=='-')sign=-1;
    for(n=0;dight(chr);chr=getchar())n=n*10+chr-'0';n*=sign;}
int mu[105],prim[105],len;
ll n;
bool isprim[105];
void init(){
    for(int i = 1;i <= 100;++i) isprim[i] = true;
    for(int i = 2;i <= 100;++i){
        if(isprim[i]) prim[len++] = i,mu[i] = -1;
        for(int j = 0;j < len && i * prim[j] <= 100;++j){
            mu[i*prim[j]] = -mu[i];
            isprim[i*prim[j]] = false;
            if(i % prim[j] == 0){
                mu[i*prim[j]] = 0;
                break;
            }
        }
    }
}
int main()
{
    freopen("a.in","r",stdin);
    freopen("b.out","w",stdout);
    init();
    T{
        Read(n);
        ll sum = 0;
        for(int i = 2;i <= 60;++i){
            sum = sum + (ll)mu[i] * ((ll)pow((long double)n+0.1,(long double)1.0/i)-1);//减掉等于1的情况
        }
        printf("%lld\n",sum+n-1);
    }
    return 0;
}

 

posted @ 2018-09-13 12:47  zhuiyicc  阅读(157)  评论(0编辑  收藏  举报