题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3598

数位DP...东看西看:http://www.cnblogs.com/Artanis/p/3751644.html

            https://www.cnblogs.com/MashiroSky/p/6399095.html

好巧妙的思路啊!这样统计的东西就变得很简单了;

好美的 dfs!数位DP用 dfs 好像能变得很清楚。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll l,r,f[][],ans;
int n,a[],K;
ll dfs1(int pos,int s,bool lim)
{
if(pos==)return s;
if(!lim&&f[pos][s]!=-)return f[pos][s];
int end=K-; ll ret=;
if(lim)end=a[pos];
for(int i=;i<=end;i++)
ret+=dfs1(pos-,s+i*(pos-),lim&&(i==end));
if(!lim)f[pos][s]=ret;//!lim!
return ret;
}
ll dfs(int pos,int s,int m,bool lim)
{
if(s<)return ;//!
if(pos==)return s;
if(!lim&&f[pos][s]!=-)return f[pos][s];
int end=K-; ll ret=;
if(lim)end=a[pos];
for(int i=;i<=end;i++)
{
if(pos>=m)ret+=dfs(pos-,s+i,m,lim&&(i==end));
else ret+=dfs(pos-,s-i,m,lim&&(i==end));
}
if(!lim)f[pos][s]=ret;
return ret;
}
ll calc(ll x)
{
int n=;
while(x)a[++n]=x%K,x/=K;
memset(f,-,sizeof f);
ll ret=dfs1(n,,);
for(int i=;i<=n;i++)
{
memset(f,-,sizeof f);
ret-=dfs(n,,i,);
}
return ret;
}
int main()
{
scanf("%lld%lld%d",&l,&r,&K);
printf("%lld\n",calc(r)-calc(l-));
return ;
}

最新文章

  1. HTMl5的存储方式sessionStorage和localStorage详解
  2. 原生Android App项目调用Untiy导出的Android项目
  3. linux运维中的命令梳理(三)
  4. C# 正则匹配domain
  5. sql语句与 数据库
  6. NOIP2010普及组T3 接水问题 ——S.B.S.
  7. java可变参数例子:求学生成绩信息,不确定课程数
  8. 设计模式学习之抽象工厂(Abstract Factory,创建型模式)(3)
  9. hack是什么
  10. Linux系统编程——进程间通信:命名管道(FIFO)
  11. docker镜像的操作
  12. the design of everyday things
  13. OpenCV——ANN神经网络
  14. [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)
  15. Memcached源码分析之memcached.c
  16. python常用模块详解
  17. getopt for windows
  18. C语言程序设计第二次作业—————顺序结构
  19. 跨界 - Omi 发布多端统一框架 Omip 打通小程序与 Web
  20. [ZJOI2013]K大数查询

热门文章

  1. [Windows Server 2012] 安装PHP+MySQL方法
  2. 【译】x86程序员手册01
  3. Caffe2:python -m caffe2.python.operator_test.relu_op_test
  4. 在Windows下安装Elasticsearch5.0
  5. 【技术累积】【点】【java】【29】MapUtils
  6. 怎么设置font awesome图标的大小?
  7. xcode构建webdriverAgent时报错Messaging unqualified id的解决办法
  8. Mac 执行 gulp 报错 -bash: gulp: command not found
  9. Cat VS Dog HDU_3829(最大独立集最大匹配)
  10. 第十三节:pandas之groupby()分组