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 中所有字母出现的次数严格 ...
随机推荐
- Hibernate从入门到了解
目录 Hibernate的介绍与执行流程 运行流程: Hibernate运行环境搭建 Hibernate的基础示例 持久类的编写 持久类的介绍 几个考虑遵守的规则: 补充: Hibernate核心文件 ...
- MongoDB 执行mongoexport时异常及分析(关于数字类型的查询)
今天在用mongoexport导出满足一定条件下的数据时,遇到了一个报错,现纪录下来,并且针对此错误对MongoDB 的 数字类型 做了进一步的学习. 背景 及 报错信息 今天接到一个业务需求,需要从 ...
- 安装和使用git遇到的问题总结
一,centos7下安装(因为centos7下用yum安装git的版本太低了,所以只能下载源代码,然后用源代码安装) 下载编译工具 yum -y groupinstall "Developm ...
- Saltstack_使用指南06_远程执行-指定目标
1. 主机规划 Targeting Minions文档 https://docs.saltstack.com/en/latest/contents.html 另请参见:自动化运维神器之saltstac ...
- 戏说春秋_i春秋 writeup
<戏说春秋>第一关 图穷匕见 题目: 解:用winhex打开,拉到最后可发现一段编码 放到解密网站上解码. <戏说春秋>第二关 纸上谈兵 解:文中没有明确指出问题,也没有给出线 ...
- hmac_检验客户端是否合法
老师博客:http://www.cnblogs.com/Eva-J/articles/8244551.html#_label6 server端 import socket import os impo ...
- DRF 序列化器-Serializer (2)
作用 1. 序列化,序列化器会把模型对象转换成字典,经过response以后变成json字符串 2. 完成数据校验功能 3. 反序列化,把客户端发送过来的数据,经过request以后变成字典,序列化器 ...
- ESP8266产品ID
ESP.getChipId() https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Chip ...
- C++笔记--thread pool【转】
版权声明:转载著名出处 https://blog.csdn.net/gcola007/article/details/78750220 背景 刚粗略看完一遍c++ primer第五版,一直在找一些c+ ...
- Dictionary实现先进先出代替Queue
Queue删除其中一个元素比较麻烦,这是一个重码校验的类,主要处理是用Dictionary代替Queue了.目前使用下来还算稳定.代码贴出来给有缘人参考. /// <summary> // ...