Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum
题意:
问[L,R]区间内有多少个数满足:其由不超过k种数字构成。
思路:
数位DP裸题,也比较好想。由于没考虑到前导0,卡了很久。但最惨的是,由于每次求和的时候需要用到10的pos次幂,我是用提前算好的10的最高次幂,然后每次除以10往下传参。但我手贱取模了,导致每次除以10之后答案就不同余了,这个NC细节错误卡了我一小时才发现。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<cmath>
#include<climits>
#include<functional>
#include<set>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> V;
typedef map<int,int> M;
typedef queue<int> Q;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=2e5+10,INF=0x3f3f3f3f,mod=998244353;
int k,dig[30];
P dp[30][2000];
inline ll add(ll a,ll b)
{
a+=b;
return a>=mod?a-mod:a;
}
inline ll mul(ll a,ll b)
{
return a*b%mod;
}
bool check(int sta)
{
int cnt=0;
for (int i=0;i<=9;++i)
if (sta&(1<<i))
cnt++;
return cnt<=k;
}
P dfs(int pos,int sta,ll v,int lead,int lit)
{
if (pos==-1)
return mp(0,1);
if (!lit&&dp[pos][sta].fi!=-1)
return dp[pos][sta];
int up=lit?dig[pos]:9;
ll sum=0,cnt=0;
for (int i=0;i<=up;++i)
{
int ns=(lead&&i==0)?sta:(sta|(1<<i));
if (!check(ns))
continue;
P tmp=dfs(pos-1,ns,v/10,lead&&i==0,lit&&i==dig[pos]);
cnt=add(cnt,tmp.se);
sum=add(sum,add(tmp.fi,mul(tmp.se,mul(i,v))));
}
if (!lit)
dp[pos][sta]=mp(sum,cnt);
return mp(sum,cnt);
}
ll count(ll n)
{
int pos=0;
while (n)
{
dig[pos++]=n%10;
n/=10;
}
ll v=1;
for (int i=0;i<pos-1;++i)
v*=10;
return dfs(pos-1,0,v,1,1).fi;
}
int main()
{
memset(dp,-1,sizeof(dp));
ll l,r;
cin>>l>>r>>k;
cout<<add(count(r)-count(l-1),mod);
return 0;
}
Educational Codeforces Round 53 E. Segment Sum(数位DP)的更多相关文章
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- 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) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
- 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)
http://codeforces.com/contest/1073 A. Diverse Substring #include <bits/stdc++.h> using namespa ...
- Educational Codeforces Round 53 Div. 2翻车记
A:差点开场懵逼.只要有相邻两位不同就可以作为答案. #include<iostream> #include<cstdio> #include<cmath> #in ...
- Educational Codeforces Round 1 A. Tricky Sum 暴力
A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...
- Educational Codeforces Round 53 Editorial
After I read the solution to the problem, I found that my solution was simply unsightly. Solved 4 ou ...
- Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法
F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...
随机推荐
- 牛客 26E 珂学送分2 (状压dp)
珂...珂...珂朵莉给你出了一道送分题: 给你一个长为n的序列{vi},和一个数a,你可以从里面选出最多m个数 一个合法的选择的分数定义为选中的这些数的和加上额外规则的加分: 有b个额外的规则,第i ...
- Angular6如何引入jQuery-knob
Angular6如何引入jQuery-knob 1.概述 Angular6引入jQuery变得异常简单,请参考https://blog.csdn.net/qq_35321405/article/det ...
- SpringBoot中获取spring.profiles.active的值
一.网上很多采用@Profile("dev")的方式获取,但是这个是类级别的 二.开发中可能需要代码级别 1.刚开始我想通过classpath下的文件读取方式,麻烦死了,于是换了个 ...
- C# Redis分布式锁的应用 - 叶子栈 - SegmentFault 思否
原文:C# Redis分布式锁的应用 - 叶子栈 - SegmentFault 思否 叶子 1 微信扫一扫 新浪微博 Twitter Facebook C# Redis分布式锁的应用 c#redis分 ...
- django管理系统代码优化-分组(二)
django管理系统代码优化-分组(二) 后续进行代码更新,优化 一优化的内容 优化前代码:https://www.cnblogs.com/pythonywy/p/11345626.html 路由进行 ...
- 【坑】在使用EL表达式时表达式无法获取数值
错误描述: 使用EL表达式 前台原样输出表达式而不输出值 错误环境: idea 2017.1.2 错误原因: jsp页面默认会忽略el表达式,需要设置为不忽略 解决方案 设置<%@ page i ...
- mmap:内存映射文件
介绍 建立一个文件的内存映射将使用操作系统虚拟内存来直接访问文件系统上的数据,而不是使用常规的I/O函数访问数据. 内存映射通常可以提高I/O性能,因为使用内存映射时,不需要对每一个访问都建立一个单独 ...
- spring 通用mapper的一些注解
1.1@Table 注解 注解作用:建立实体类和数据库表之间的对应关系.默认规则:实体类类名首字母小写作为表名.Employee 类→employee 表. 用法:在@Table 注解的 name 属 ...
- 010.简单查询、分组统计查询、多表连接查询(sql实例)
-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...
- 在SqlServer和Oralce中创建索引
给表名A的字段A增加索引 SqlServer: if exists (select 1 from sysobjects where name='表名A' and type='u')and exists ...