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)的更多相关文章

  1. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  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) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

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

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

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

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

  6. Educational Codeforces Round 53 Div. 2翻车记

    A:差点开场懵逼.只要有相邻两位不同就可以作为答案. #include<iostream> #include<cstdio> #include<cmath> #in ...

  7. Educational Codeforces Round 1 A. Tricky Sum 暴力

    A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...

  8. Educational Codeforces Round 53 Editorial

    After I read the solution to the problem, I found that my solution was simply unsightly. Solved 4 ou ...

  9. 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 ...

随机推荐

  1. 怎样判断浏览器是否支持canvas

    1. 如果网页必须使用canvas, 则需要告知用户更换或更新浏览器. 这时可以通过在<canvas>标签之间添加替代元素进行 <canvas id="c1"&g ...

  2. BaseHandler的封装, 处理handler中的内存泄漏

    package de.bvb.study.common; /** * 用于规范 Message.what此属性,避免出现魔法数字 */ public final class What { public ...

  3. Attribute自定义特性+Asp.net MVC中的filter详解

    转载自:http://blog.csdn.net/wangyy130/article/details/44241957 一.filter简介 在了解自定义特性前,先引入一个概念filter,它是MVC ...

  4. Log4net采用外部配置文件和多记录器的方法

    1) 创建配置文件,可以放在任意位置,名字可以任意的xml文件 例如,文件名 Log.Config.xml,内容如下 <?xml version="1.0" encoding ...

  5. zepto学习(三)之详解

    zepto Zepto就是jQuery的移动端版本, 可以看做是一个轻量级的jQuery github地址: https://github.com/madrobby/zepto 官方地址: http: ...

  6. 复杂度n求数组的第K大值

    利用快速排序的方法进行: #include<iostream> using namespace std; int test() { ; return a; } int quickSort( ...

  7. Jboss未授权访问部署木马 利用exp

    查看系统名称 java -jar jboss_exploit_fat.jar -i http://www.any.com:8080/invoker/JMXInvokerServlet get jbos ...

  8. sql临时表 通过临时表循环处理数据

    -- 创建临时表 IF OBJECT_ID('tempdb.dbo.#temprecord','U') IS NOT NULL DROP TABLE dbo.#temprecord; GO SELEC ...

  9. cnblogs排版样式预览

    说明:关于本博主题及样式来源于[GitHub]:本博总体排版目录样式风格参照博文[修仙成神之路]进行预览:参照本博设置可参考博文[设置跟本博一样的效果]本博之前发表过的博文存在样式不协调,后期会逐一完 ...

  10. VLC 可能的 XML parser error 解决

    由于 VLC 设置不当 (通常是动了 skin 选项……),再次加载时 VLC 不能正常启动,并报如下错误: [00007f7dd003b670] xml xml reader error: XML ...