题目链接:https://codeforces.com/contest/1073/problem/E

题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的和(并且模998244353)

例如求区间[10,50],k=1,答案为ans=(11+22+33+44)%998244353=110.

Examples

input

Copy
10 50 2
output

Copy
1230
input

Copy
1 2345 10
output

Copy
2750685
input

Copy
101 154 2
output

Copy
2189

解题思路:首先我们用数位dp求出区间[l,r]上合法数的个数并不难,可以采用二进制记录每一个数是否出现,难点在于如何对合法的数进行求和求和,我们肯定不能一个数一个数进行求和,,我们可以考虑在每个位放每个数,计算该位的该数对答案的贡献度。总权值和等于比它低位的权值和+当前位的权值。
需注意前导0的情况。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll mod=;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int a[],k;
ll l,r;
struct node{
ll a,b; //a表示合法数的个数,b表示权值的和
}dp[][];
int work(int x){ //计算数位中有多少个不同的数字
int cnt=;
for(int i=;i<=;i++)
if(x>>i&)cnt++;
return cnt;
}
node dfs(int pos,int sta,int limit,bool invalid){
//sta记录包含的不同数,invalid记录前导0
if(pos==) return node{,};
if(!limit&&dp[pos][sta].a!=)
return dp[pos][sta];
int up=limit?a[pos]:;
node ans,tmp;
ans.a=; ans.b=; //局部变量得初始化
for(int i=;i<=up;i++){
int x=sta|(int)(pow(,i)); //更新状态
if(work(x)>k) continue; //不同数字个数超过k,不合法剪枝
if(invalid&&i==){ //前导都为0,状态不用更新
tmp=dfs(pos-,sta,limit&&i==up,invalid&&i==);
}else{
tmp=dfs(pos-,x,limit&&i==up,invalid&&i==);
}
tmp.a%=mod; tmp.b%=mod;
ans.a=(ans.a+tmp.a)%mod; //累计合法数的个数
ll y=pow(,pos-);
ans.b=(ans.b+tmp.b+1ll*y%mod*i%mod*tmp.a%mod)%mod; //累计合法数的权值
}
if(!limit)
dp[pos][sta]=ans;
return ans;
}
ll solve(ll x){
int pos=;
while(x){
a[++pos]=x%;
x/=;
}
return dfs(pos,,,true).b;
}
int main(){
cin>>l>>r>>k;
cout<<(solve(r)-solve(l-)+mod)%mod<<endl; //防止答案为负
return ;
}

Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)的更多相关文章

  1. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum

    https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...

  2. Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)

    这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...

  3. Educational Codeforces Round 53 (Rated for Div. 2)

    http://codeforces.com/contest/1073 A. Diverse Substring #include <bits/stdc++.h> using namespa ...

  4. [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]

    http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...

  5. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  6. Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem

    题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合 题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感 ...

  7. Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair

    题意:一个人  有T块钱 有一圈商店 分别出售 不同价格的东西  每次经过商店只能买一个  并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...

  8. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot

    题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时  所进行的修改代价最小是多少 其中代价的定义是  终点序号-起点序号-1 思路:因为代价是终点序号减去 ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) A Diverse Substring

    传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题意: 给出串是多态的定义“长度为 n 的串 s ,当串 s 中所有字母出现的次数严格 ...

随机推荐

  1. 简易仿ios菊花加载loading图

    原文链接:https://mp.weixin.qq.com/s/wBbQgOfr59wntNK9ZJ5iRw 项目中经常会用到加载数据的loading显示图,除了设计根据app自身设计的动画loadi ...

  2. 预置第三方apk到MTK项目相关问题总结

    目前5.0之后项目预置方式通用步骤为: 建立apk文件夹;  置目标apk到该文件夹下;   解压缩apk查看是否包含lib/文件夹(apk项目是否包含lib库文件);  在该文件夹下编写Androi ...

  3. Git - git clone - 将远端仓库克隆拷贝到本地

    索引: 目录索引 参看代码 GitHub: git.txt 一.示例: git clone https://github.com/liumeng0403/lm.solution.git 二.说明: 1 ...

  4. maven springTest结合junit单元测试

    1.引入相关依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</artifac ...

  5. 前后端分离djangorestframework—— 接入第三方的验证码平台

    关于验证码部分,在我这篇文章里说的挺详细的了:Python高级应用(3)—— 为你的项目添加验证码 这里还是再给一个前后端分离的实例,因为极验官网给的是用session作为验证的,而我们做前后端分离的 ...

  6. input(type='file')上传多张照片并显示,传到后台

    以下内容为网络摘抄和实践修改所得,如有雷同,请谅解!!!! 1.首先是前端页面代码: 其中,<input type="file" id="file_input&qu ...

  7. VSCode的Python扩展下程序运行的几种方式与环境变量管理

    在VSCode中编写Python程序时,由于有些地方要使用环境变量,但是发现设置的环境变量有时不起作用,花了点时间研究了一下,过程不表,直接说结论. 首先,环境变量的设置,Python扩展中有三种方式 ...

  8. awk、grep、sed是linux操作文本的三大利器,也是必须掌握的linux命令之一

    awk.grep.sed是linux操作文本的三大利器,也是必须掌握的linux命令之一.三者的功能都是处理文本,但侧重点各不相同,其中属awk功能最强大,但也最复杂.grep更适合单纯的查找或匹配文 ...

  9. Oracle导入、导出数据库dmp文件

    版本 1.实例数据完全导出 即导出指定实例下的所有数据 exp username/password@192.168.234.73/orcl file=d:/daochu/test.dmp full=y ...

  10. 三机互ping(自己总结)

    主机与虚拟机互ping设置: 点击VMware下的[编辑]--[虚拟网络编辑器]设置如下:         屏幕剪辑的捕获时间: 2016/5/21 13:10         屏幕剪辑的捕获时间: ...