按位计数问题

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <unordered_set>
#define mkp make_pair
using namespace std;
const double EPS=1e-8;
typedef long long lon;
const lon SZ=21,SSZ=2*SZ,APB=20,INF=0x7FFFFFFF,mod=1000000007;
lon m,n,k,sz,arr[SZ];
int dp[SZ];

void release()
{
    
}

int dfs(int pos,int lim,bool type)
{
    int up=lim?arr[pos]:9;
    if(!lim&&dp[pos]!=-1)return dp[pos];
    else if(pos==sz-1)
    {
        int res=0;
        for(int i=0;i<=up;++i)res+=i;
        if(!lim)dp[pos]=res;
        return res;
    }
    else 
    {
        int res=0;
        for(int i=0;i<=up;++i)
        {
            //res+=i*(pow(k,sz-pos-1)*(sz-pos-1)*(k-1));
            int tmp=pow(k,sz-pos-1);
            if(lim&&i==up)tmp=(type?n:m)%(int)pow(k,sz-pos-1),++tmp;
            res+=i*tmp;
            if(sz==2)cout<<res<<endl;
            res+=dfs(pos+1,lim&&i==up,type);
            if(sz==2)cout<<res<<endl;
        }
        if(!lim)dp[pos]=res;
        return res;
    }
}

int calc(int x,bool type)
{
    memset(dp,-1,sizeof(dp));
    sz=0;
    if(x==0)
    {
        arr[sz++]=0;
    }
    else 
    for(;x;x/=10)arr[sz++]=x%10;
    reverse(arr,arr+sz);
    int res=dfs(0,1,type);
    return res;
}

void init()
{
    cin>>m>>n>>k;
    --m;
    cout<<calc(n,1)-calc(m,0)<<endl;
}

void work()
{
    
}

int main()
{
    //std::ios::sync_with_stdio(0);
    //freopen("d:\\2.txt","r",stdin);
    //freopen("d:\\3.txt","w",stdout);
    lon casenum;
    //cin>>casenum;
    //cout<<casenum<<endl;
    //for(lon time=1;time<=casenum;++time)
    //for(lon time=1;scanf("%d%d",&n,&m)!=EOF;++time)
    {
        init();
        work();
        release();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/gaudar/p/10696035.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值