容斥 HDU 2204

#include<stdio.h>
#include<vector>
#include<math.h>
#include<stdlib.h>

using namespace std;

vector<int>s;
int z[]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67};
typedef __int64 LL;

void Init()
{
    for(int i=0;z[i]<64;i++) //这个容斥的版挺好的
    {
        for(int j=s.size()-1;j>=0;j--)
            if(abs(s[j]*z[i])<=63)
                s.push_back(-s[j]*z[i]);//奇加偶减
        s.push_back(z[i]);
    }
}

int main()
{
    Init();
    LL x;
    while(scanf("%I64d",&x)!=EOF)
    {
        LL ans=0;
        for(int i=0;i<s.size();i++)
        {
            LL tmp=pow(x+0.5,1.0/abs(s[i]));
            if(s[i]<0)
                ans-=tmp;
            else
                ans+=tmp;
        }
        printf("%I64d
",ans-1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cherryMJY/p/6289421.html