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 ...
随机推荐
- 计算机概论 64bit和32bit的CPU的不同
32位(WOW32).64位(WOW64,x64)指的是两种不同的CPU架构. 32位的CPU能够在每个时钟周期传输32位数据,而64位的CPU能够在每个时钟周期传输64位数据. 32位系统可以访问2 ...
- vue项目中导出PDF的两种方式
参考大家导出的方式,基本上是如下两种: 1.使用 html2Canvas + jsPDF 导出PDF, 这种方式什么都好,就是下载的pdf太模糊了.对要求好的pdf这种方式真是不行啊! 2.调用浏览器 ...
- javaweb开发技术--监听器
监听器定义:是指专门用于其他对象身上发生的事件或状态改变进行监听和相应的处理的对象,当被监视的对象发生变化时立即采取相应的行动. web监听器的定义:servlet规范中定义的一种特殊类.用于监听Se ...
- LEANGOO卡片
转自:https://www.leangoo.com/leangoo_guide/leangoo_cards.html#toggle-id-10 Leangoo的卡片可以是需求.目标.任务.问题.缺陷 ...
- 使用代码将github仓库里某个issue同步到CSDN博客上
我是一个懒惰的程序员.我在github仓库里用issue的方式写了很多分享文章,想同步到CSDN上.但是我又不想一篇篇手动复制粘贴,因此想用代码来实现自动化. 例子: https://github.c ...
- Linux内核移植的若干问题
- Linux内核移植主要过程
- Dart的导包
Dart中的库主要有三种: 1.我们自定义的库 import 'lib/xxx.dart'; 2.系统内置库 import 'dart:math'; import 'dart:io'; import ...
- web开发:web前端初识
一.前端三剑客 二.编辑器 三.第一个页面 四.基本标签 五.标签分类 一.前端三剑客 html:完成页面架构的搭建 css:完成页面样式布局 js:完成页面功能 二.编辑器 插件: 插件管理器 ct ...
- CSP2019 退役记
本来想写"退役在即"的,考完 Day2 后直接改成"退役记"了 Day 0 在 ssf 的机房里继续变弱,自己写了一遍 splay 板子,居然写对了,开心 非常 ...