E. Segment Sum(数位dp)


题意:求一个区间内满足所有数位不同数字个数小于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)的更多相关文章
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- zoj 3962 Seven Segment Display 数位dp
非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能AC. 思路:dp(i, j)表示已经考虑了前i位,前i位的和为j的贡献.如果当前的选择一直是最大的选择,那么就必须从0~下一位的最大 ...
- 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 ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
- Codeforces1073E Segment Sum 【数位DP】
题目分析: 裸的数位DP,注意细节. #include<bits/stdc++.h> using namespace std; ; int k; ][],sz[][],cnt[][]; ] ...
- 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 ...
- ZOJ - 3962 - Seven Segment Display-17省赛-数位DP
传送门:Seven Segment Display 题意:求一个给定区间每个数字的消耗值的和: 思路:数位DP,有点区间和的思想,还有就是这个十六进制,可以用%llx读,还是比较难的: 还有就是到最大 ...
- 数位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 ...
- ZOJ 3962 Seven Segment Display(数位DP)题解
题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...
随机推荐
- Windows7 密码修改
一:不用输入原密码的方式修改用户的密码 1 命令行输入命令:mmc #进入到控制台 2 点击左上角的文件,选择添加/删除管理单元 3 选择本地用户和组管理单元,添加到本地计算机,完成,确定 4 添加 ...
- 学习笔记TF039:TensorBoard
首先向大家和<TensorFlow实战>的作者说句不好意思.我现在看的书是<TensorFlow实战>.但从TF024开始,我在学习笔记的参考资料里一直写的是<Tenso ...
- design_patterns_in_typescript 学习
https://github.com/torokmark/design_patterns_in_typescript Creational Singleton [A class of which on ...
- 1.1.21 Word修改文章目录
1.选中目录后,右键[编辑域],选择[索引和目录].选择[TOC],点击右侧的[目录]. 2.选中[目录]后,按照如下[1][2][3]顺序,按格式要求修改目录即可.
- [zz]LyX 入门教程
http://blog.sina.com.cn/s/blog_630e5dec0100w3jl.html The LyX Tutorial by the LyX Team 1 目录 Chapter 1 ...
- 阿里四不像Fourinone
阿里四不像——分布式核心技术框架 Fourinone https://blog.csdn.net/shero_zsmj/article/details/52277194
- Python类的部分
先来一段代码 表示互殴 class Gailun: country='demaxia' def __init__(self,name,age,life_value,att): self.name=na ...
- java中如何给控件设置颜色
1. tv.setTextColor(Color.parseColor("#000000"));2. tv.setTextColor(getResources().getCo ...
- EL(Expression Language)和JSTL标签(JSP Standard Tag Library)
一.EL表达式: Expression Language提供了在 JSP 脚本编制元素范围外(例如:脚本标签)使用运行时表达式的功能.脚本编制元素是指页面中能够用于在JSP 文件中嵌入 Java代码的 ...
- Vue Checkbox全选和选中的方法
<div class="search-content"> <Checkbox :value="checkAll" @click.prevent ...