博客
关于我
【数位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 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>