博客
关于我
【数位dp】学习
阅读量:630 次
发布时间:2019-03-14

本文共 1392 字,大约阅读时间需要 4 分钟。

根据
发现数位dp不仅能做数位统计,也能做数位计算

//#pragma comment(linker, "/STACK:102400000,102400000")#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;#define mem(a, b) memset(a, b, sizeof(a))/*例一: 按位求和问题给定A,B(1<=A,B<=10^5),求[A,B]内的所有数的k进制表示下各数位之和dp[i][j] 表示 0 ~ k^i-1 的位数和 (在j进制下)pw[i][j] 表示 j^i 的值对pw处理不理解 建议先做 51nod 1009题目没有提交,与网上代码对拍,无差异。*/#define Elem LLconst int Dight = 65;int bits[Dight];Elem dp[Dight][11];Elem pw[Dight][11];void init(){ for(int i = 2; i <= 10; i++) { pw[0][i] = 1; for(int j = 1; j < Dight; j++) pw[j][i] = pw[j-1][i]*(LL)i; }}Elem dfs(int bit, int pos, int sta, bool limit){ if(pos==0) return (LL)sta; if(!limit && dp[pos][bit]!=-1) return dp[pos][bit] + pw[pos][bit]*(LL)sta; int up = limit ? bits[pos]:(bit-1); Elem res = 0; for(int i = 0; i <= up; i++) res += dfs(bit,pos-1,sta+i,limit&&(i==up)); if(!limit) dp[pos][bit] = res; return res;}Elem solve(Elem n, int bit){ int pos = 0; while(n){ bits[++pos] = n%bit; n /= bit; } return dfs(bit,pos,0,1);}int main(){ init(); int T,k; memset(dp,-1,sizeof(dp));// freopen("G:\\duipai\\in.txt","r", stdin);// freopen("G:\\duipai\\out1.txt","w", stdout); scanf("%d",&T); LL a,b; while(T--){ scanf("%I64d%I64d%d",&a,&b,&k); printf("%I64d\n",solve(b,k)-solve(a-1,k)); } return 0;}

转载地址:http://miaoz.baihongyu.com/

你可能感兴趣的文章
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>