Integer’s Power HDU - 3208 组合数学

题意:给你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 long
const int maxn = 100;
#define inf 1e18
LL 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值