博客
关于我
【数位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/

你可能感兴趣的文章
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>
mysql 判断表字段是否存在,然后修改
查看>>
mysql 协议的退出命令包及解析
查看>>
mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>