bzoj 3598 [ Scoi 2014 ] 方伯伯的商场之旅 ——数位DP
2024-08-29 23:03:28
题目: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 ;
}
最新文章
- HTMl5的存储方式sessionStorage和localStorage详解
- 原生Android App项目调用Untiy导出的Android项目
- linux运维中的命令梳理(三)
- C# 正则匹配domain
- sql语句与 数据库
- NOIP2010普及组T3 接水问题 ——S.B.S.
- java可变参数例子:求学生成绩信息,不确定课程数
- 设计模式学习之抽象工厂(Abstract Factory,创建型模式)(3)
- hack是什么
- Linux系统编程——进程间通信:命名管道(FIFO)
- docker镜像的操作
- the design of everyday things
- OpenCV——ANN神经网络
- [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)
- Memcached源码分析之memcached.c
- python常用模块详解
- getopt for windows
- C语言程序设计第二次作业—————顺序结构
- 跨界 - Omi 发布多端统一框架 Omip 打通小程序与 Web
- [ZJOI2013]K大数查询
热门文章
- [Windows Server 2012] 安装PHP+MySQL方法
- 【译】x86程序员手册01
- Caffe2:python -m caffe2.python.operator_test.relu_op_test
- 在Windows下安装Elasticsearch5.0
- 【技术累积】【点】【java】【29】MapUtils
- 怎么设置font awesome图标的大小?
- xcode构建webdriverAgent时报错Messaging unqualified id的解决办法
- Mac 执行 gulp 报错 -bash: gulp: command not found
- Cat VS Dog HDU_3829(最大独立集最大匹配)
- 第十三节:pandas之groupby()分组