把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

题解 P6356 [COCI2007-2008#3] CUDAK

水道简单题的题解

传送门

分析

首先从数据范围出发 1A,B1015,并且提问方式 [A,B] 可以发现,这就是一道数位 dp 的题目。

接着看数位 dp 的满足条件,和一定,那就更加确信是一道数位 dp 了。

至于第二问,我们可以通过二分答案来解决,答案是最早的到 A 中和为 S 的点。

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e2+5;
int n, m,sum,L,R;
int f[N][N],num[N];
inline int dfs(int now,int tot,int flag) {
if(!flag&&~f[now][tot]) return f[now][tot];
if(tot>sum) return 0;
if(now==0) return tot==sum;
int res=0,up=flag?num[now]:9;
for(int i=0;i<=up;++i) res+=dfs(now-1,tot+i,flag&&(i==num[now]));
if(!flag) f[now][tot]=res;
return res;
}
inline int query(int x) {
int cnt=0;
while(x) {
num[++cnt]=x%10;
x/=10;
}
return dfs(cnt,0,1);
}
signed main() {
cin>>L>>R>>sum;
memset(f,-1,sizeof f);
cout<<query(R)-query(L-1)<<"\n";
int l=L,r=R,res=0,x=query(L-1);
while(l<=r) {
int mid=l+r>>1;
if(query(mid)==x) l=mid+1;
else r=mid-1,res=mid;
}
cout<<res<<"\n";
return 0;
}
posted @   djh0314  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示