Integer’s Power HDU

来源:互联网 发布:淘宝客a5淘客交流 编辑:程序博客网 时间:2024/06/03 17:12

题意:给你l,r,问你在这个区间中the sum of the power of the integers
一个数能被另外一个数的a次方表示的,power of the integers 是最大的a
2<=l<=r<=10^18

这个并不是一般的那种容斥原理。

因为这个指数我们知道是在61之内的,然后我们可以枚举指数,然后dp[i]中存指数为i的数的个数,我们求个数的时候,,那个防溢出的真的是…….
去重是从后往前减。因为我们要求对应i的dp[i],而不是总数。

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#include <queue>using namespace std;#define LL long longconst int maxn = 100;#define inf 1e18LL dp[maxn];LL a(LL x,int n){    LL ans=1,q=x;    while(n)    {        if(n&1) {        double tmp=1.0*inf/ans;        if(q>tmp) return -1;        ans=ans*q;        }        n>>=1;        q=q*q;    }    return ans;}LL aa(LL n,LL i){    LL k=(LL)pow(n,1.0/i);    LL l=a(k-1,i),r=a(k+1,i),mid=a(k,i);    LL ans=0;    if(r<=n&&r>0) ans=k+1;    else if(mid<=n&&mid>0) ans=k;    else if(l<=n&&l>0) ans=k-1;    return ans;}LL solve(LL n){    memset(dp,0,sizeof(dp));    if(n==1) return 1;    dp[1]=n;    int i;    for(i=2;i<62;i++)    {        dp[i]=aa(n,i)-1;        if(dp[i]<=0) break;    }    for(int k=i-1;k>=1;k--)    {        for(int j=1;j<k;j++)        {            if(k%j==0) dp[j]-=dp[k];        }    }    LL ans=0;    for(int h=1;h<i;h++)        ans+=dp[h]*h;    return ans;}int main(){    LL a,b;    while(scanf("%I64d %I64d",&a,&b)!=EOF)    {        if(a==0&&b==0) break;        LL a1=solve(b);        LL a2=solve(a-1);        printf("%I64d\n",a1-a2);    }    return 0;}
原创粉丝点击