题意:求一个区间内满足所有数位不同数字个数小于K的数字总和。比如:k=2   1,2,3所有数位的不同数字的个数为1满足,但是123数位上有三个不同的数字,即123不满足。

我们可以使用一个二进制的数字来记录某个数字是否已经出现,0为还没有出现,1表示该数字已经出现了。这里还需要注意前导零的干扰。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ri register int
typedef long long ll; inline ll gcd(ll i,ll j){
return j==0?i:gcd(j,i%j);
}
inline ll lcm(ll i,ll j){
return i/gcd(i,j)*j;
}
inline void output(int x){
if(x==0){putchar(48);return;}
int len=0,dg[20];
while(x>0){dg[++len]=x%10;x/=10;}
for(int i=len;i>=1;i--)putchar(dg[i]+48);
}
inline void read(int &x){
char ch=x=0;
int f=1;
while(!isdigit(ch)){
ch=getchar();
if(ch=='-'){
f=-1;
}
}
while(isdigit(ch))
x=x*10+ch-'0',ch=getchar();
x=x*f;
}
struct st{
ll num;
ll sum;
st():num(0),sum(0){
}
st(ll num,ll sum):num(num),sum(sum){
}
}dp[20][2000];
int maxs;
int a[20];
const ll mod=998244353;
int change(int n){
int cnt=0;
while(n){
if(n&1)cnt++;
n/=2;
}
return cnt;
}
st dfs(int pos,int sta,int pre,bool limit){
if(pos==-1){
// cout<<pre<<endl; if(change(sta)<=maxs)
return st(1,0);
return st(0,0);
}
if(dp[pos][sta].num!=0&&!limit){
// printf("%d %lld\n",pos,dp[pos][sta].sum);
return dp[pos][sta];
}
int up=limit?a[pos]:9;
st ans; for(int i=0;i<=up;i++){
st tem;
if(change(sta|(int)pow(2,i))>maxs)
continue;
if(pre==0&&i==0){
tem=dfs(pos-1,sta,0,i==up&&limit);
}
else{
tem=dfs(pos-1,((int)pow(2,i))|sta,i,i==up&&limit);
}
ans.num+=tem.num;
ans.num=ans.num%mod;
ans.sum=(ans.sum+(ll)pow(10,pos)%mod*i*tem.num%mod+tem.sum)%mod;
}
if(!limit){
dp[pos][sta]=ans;
}
return ans;
}
ll solve(ll n){
int len=0;
while(n){
a[len++]=n%10;
n/=10;
}
return dfs(len-1,0,0,true).sum;
}
int main(){
ll l,r;
scanf("%lld%lld%d",&l,&r,&maxs);
printf("%lld",(solve(r)-solve(l-1)+mod)%mod);
return 0;
}

  

E. Segment Sum(数位dp)的更多相关文章

  1. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  2. zoj 3962 Seven Segment Display 数位dp

    非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能AC. 思路:dp(i, j)表示已经考虑了前i位,前i位的和为j的贡献.如果当前的选择一直是最大的选择,那么就必须从0~下一位的最大 ...

  3. CodeForces - 1073E :Segment Sum (数位DP)

    You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...

  4. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

  5. Codeforces1073E Segment Sum 【数位DP】

    题目分析: 裸的数位DP,注意细节. #include<bits/stdc++.h> using namespace std; ; int k; ][],sz[][],cnt[][]; ] ...

  6. ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp

    Seven Segment Display Time Limit: 1 Second      Memory Limit: 65536 KB A seven segment display, or s ...

  7. ZOJ - 3962 - Seven Segment Display-17省赛-数位DP

    传送门:Seven Segment Display 题意:求一个给定区间每个数字的消耗值的和: 思路:数位DP,有点区间和的思想,还有就是这个十六进制,可以用%llx读,还是比较难的: 还有就是到最大 ...

  8. 数位DP:SPOJ KPSUM - The Sum

    KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that ...

  9. ZOJ 3962 Seven Segment Display(数位DP)题解

    题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...

随机推荐

  1. ArrayUtil的创建和使用

    求数组最小值求数组最大值遍历数组元素求数组总和求数组平均数求数组反转实现数组的复制对数组进行排序 写入的方法和包: public class ArrayUtil { //求数组的最大值 public ...

  2. 阅读 Device Driver Programmer Guide 笔记

    阅读 Device Driver Programmer Guide 笔记 xilinx驱动命名规则 以X开头 源文件命名规则 以x打头 底层头文件与高级头文件 重点来了,关于指针的使用 其中 XDev ...

  3. FTP文件传输服务

    FTP文件传输服务 一 .FTP 连接及传输的模式 l  控制连接:TCP21,用于发送FTP命令信息. l  数据连接:TCP 20, 用于上传下载数据. · 数据连接建立的类型: ·主动模式: 服 ...

  4. Docker镜像常用命令

    镜像(image)是Docker三大核心概念中最重要的,是运行容器的前提. Docker运行容器前需要本地存在对应的镜像,如果镜像没保存在本地,Docker会尝试先从默认镜像仓库下载(默认使用Dock ...

  5. vue1 & vue2 数据驱动更新视图机制对比

    vue1 小粒度更新,精确追踪到数据变化所影响的dom变化,精确更新变化的dom 具体实现为,维护 observer watcher directive 三个类 ·observer负责监听数据变化,并 ...

  6. js 常用代码片段

    一.预加载图像 如果你的网页中需要使用大量初始不可见的(例如,悬停的)图像,那么可以预加载这些图像. function preloadImages(){ for(var i=0;i<argume ...

  7. i++ 是线程安全的吗?

    相信很多中高级的 Java 面试者都遇到过这个问题,很多对这个不是很清楚的肯定是一脸蒙逼.内心肯定还在质疑,i++ 居然还有线程安全问题?只能说自己了解的不够多,自己的水平有限. 先来看下面的示例来验 ...

  8. leetcode22

    public class Solution { public IList<string> GenerateParenthesis(int n) { List<string> l ...

  9. 2018 pycharm最近激活码

    今天更新了一下pycharm,结果之前的激活就不能用了,下面是新的激活方法: 1.mac下在终端进入etc目录: cd /etc 2.编辑hosts文件: vi hosts 将“0.0.0.0 acc ...

  10. 最适合入门的Laravel中级教程(一)

    Laravel 是一个全栈框架: 我们使用 Laravel 开发业务常见有 3 个方向: 前端页面和后端逻辑混合的应用 主要是面向对 SEO 有需求的项目: 比如说新闻资讯博客文章等: 一般在控制器中 ...