题意:求一个区间内满足所有数位不同数字个数小于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. Processing 编程学习指南 (丹尼尔·希夫曼 著)

    https://processing.org/reference/ 第1章 像素 (已看) 第2章 Processing (已看) 第3章 交互 (已看) 第4章 变量 (已看) 第5章 条件语句 ( ...

  2. Python shelve 模块

    使用json或者pickle持久化数据,能dump多次,但load的话只能取到最新的dump, 因为先前的数据已经被后面dump的数据覆盖掉了. 如果想要实现dump多次不被覆盖,就可以想到使用she ...

  3. Centos7.4安装配置haproxy和Keepalived补充内容

    补充比较杂 1.当master服务恢复正常之后,backup机器收到消息,然后让出vip 下面是master机器服务恢复正常后,backup机器的Keepalived日志 收到master的消息通知, ...

  4. linux删除某用户密码

    1.清空一个linux用户密码 # passwd -d user1 passwd: password expiry information changed. 2.指定key登录 ssh port111 ...

  5. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  6. ubuntu拒绝root用户ssh远程登录解决办法

    ubuntu拒绝root ssh远程登录通常情况是ssh设置了禁止root远程登录,解决办法就是:修改ssh配置,然后重启ssh服务即可. vi /etc/ssh/sshd_config 找到并用#注 ...

  7. AttributeError: 'module' object has no attribute 'main'

    本机环境:ubuntu16.04,  ros-kinetic $ roscore 报错 Traceback (most recent call last): File , in <module& ...

  8. 一切为了解决隐私问题,绿洲实验室Ekiden协议介绍

    绿洲实验室官网截图 下一代区块链平台的竞争已经悄然展开,每个月我们都能看到新成立的创业公司宣称,他们要采用区块链解决所有问题.大约80-90%的区块链项目,运行在像Ethereum这样的平台上. 创建 ...

  9. ZooKeeper和CAP理论及一致性原则

    一.CAP理论概述CAP理论告诉我们,一个分布式系统不可能同时满足以下三种 一致性(C:Consistency)可用性(A:Available)分区容错性(P:Partition Tolerance) ...

  10. java内存区域之程序计数器

    程序计数器(program counter register) 作用:字节码解释其工作时,通过这个计数器的值的改变,来选取下一条执行的字节码命令. 由于java虚拟机的都线程是通过线程轮流切换,并分配 ...