Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接: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
10 50 2
1230
1 2345 10
2750685
101 154 2
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求和)的更多相关文章
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum
https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...
- Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...
- Educational Codeforces Round 53 (Rated for Div. 2)
http://codeforces.com/contest/1073 A. Diverse Substring #include <bits/stdc++.h> using namespa ...
- [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,如果 ...
- 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 ...
- 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即可.(感 ...
- Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair
题意:一个人 有T块钱 有一圈商店 分别出售 不同价格的东西 每次经过商店只能买一个 并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot
题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时 所进行的修改代价最小是多少 其中代价的定义是 终点序号-起点序号-1 思路:因为代价是终点序号减去 ...
- Educational Codeforces Round 53 (Rated for Div. 2) A Diverse Substring
传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题意: 给出串是多态的定义“长度为 n 的串 s ,当串 s 中所有字母出现的次数严格 ...
随机推荐
- Android ScrollView和ListView滑动冲突解决记录
private int mLastX; private int mLastY; public View.OnTouchListener onTouchListener = new View.OnTou ...
- 使用GRPC远程服务调用
远程过程调用(英语:Remote Procedure Call,缩写为 RPC)是一个计算机通信协议.该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额外地为这个交互作用编程.如 ...
- 跨平台 webapp 开发技术之 Hybrid App
前所知的 APP 开发模式有三种: 基于操作系统运行的 APP -> Native App,侧重于原生开发,用户体验好,需要安装才会升级 基于浏览器运行的 APP -> Web App,侧 ...
- Git - git clone - 将远端仓库克隆拷贝到本地
索引: 目录索引 参看代码 GitHub: git.txt 一.示例: git clone https://github.com/liumeng0403/lm.solution.git 二.说明: 1 ...
- ORA-02030: can only select from fixed tables/views
有时候给一些普通用户授予查询系统对象(例如dynamic performance views)权限时会遇到"ORA-02030: can only select from fixed tab ...
- Python二次编码、小数据池之心照神交
二次编码.解码.小数据池: encode(str:编码):参数编码方式,返回字节码. str_1 = "编码" str_2 = str_1.encode("utf-8&q ...
- windows环境下安装yaf框架
windows环境下安装yaf框架 在windows下安装yaf框架 准备工作: php环境(过程略,wamp,xampp,phpstudy都行,php版本大于5.3) git工具(需要从github ...
- June. 22 2018, Week 25th. Friday
Where words fail, music speaks. 言语无法表达时,音乐就会响起. From Hans Christian Andersen. Where words fail, musi ...
- 英语口语练习系列-C12-不了解
词汇 air [eə(r)] n. 空气 fresh air 新鲜的空气 warm air 暖暖的空气 I like to air the room. 我喜欢给房间通气. on the air 正在播 ...
- Windows使用MongoDB,以及索引创建
安装MongoDB https://www.mongodb.com/download-center#community 点击msi安装程序进行安装,可以进行自定义安装,选择安装位置,我选择的是D盘 在 ...