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. 图解Java继承内存分配

    图解Java继承内存分配   继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. (3)子 ...

  2. MongoDB的复合唯一索引

    一 创建 JavaScript Shell db.room.ensureIndex({'floor':1,'num':1}) Spring Data @Data // lombok @Document ...

  3. 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据

    一.  使用RSA证书加.解密敏感数据 X.509证书标准支持三种不对称加密算法:RSA, DSA, Diffie-Hellman algorithms.最常用的是RSA算法.所以本文就以前面章节使用 ...

  4. C#学习基础资料记录---字典(Dictionary),时间表示方法(DateTime.Now),文件操作

    1.字典 https://www.cnblogs.com/gengaixue/p/4002244.html 2.时间的表示方法 DateTime.Now的多种用法 https://www.cnblog ...

  5. LeetCode:184.部门工资最高的员工

    题目链接:https://leetcode-cn.com/problems/department-highest-salary/ 题目 Employee 表包含所有员工信息,每个员工有其对应的 Id, ...

  6. koa 实现session登陆

    在我们访问一些网站内部内容的时候,通常都会先验证我们是否已经登陆,如果登陆了就跳转到内容页面否则就跳转或者弹出登陆页面. 但是HTTP协议是没有状态的协议,无法标识一个用户的登录状态. 于是Cooki ...

  7. vue+element-ui upload图片上传前大小超过4m,自动压缩到指定大小,长宽

    最近项目需要实现一个需求,用户上传图片时,图片大小超过4M,长宽超过2000,需要压缩到400k,2000宽高.在git上找到一个不错的方法,把实现方法总结一下: 安装image-conversion ...

  8. RobHess的SIFT代码解析步骤四

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码 2.书:王永明 ...

  9. 工作中常用的Git操作

    粘贴自:微信公众号:程序员共成长 分支操作: git branch 创建分支 git branch -b 创建并切换到新建的分支上 git checkout 切换分支 git branch 查看分支列 ...

  10. 说一下 HashSet 的实现原理?(未完成)

    说一下 HashSet 的实现原理?(未完成)