You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from l l to r r (including l l and r r ) such that each number contains at most k k different digits, and print this sum modulo 998244353 998244353 .

For example, if k=1 k=1 then you have to calculate all numbers from l l to r r such that each number is formed using only one digit. For l=10,r=50 l=10,r=50 the answer is 11+22+33+44=110 11+22+33+44=110 .

Input

The only line of the input contains three integers l l , r r and k k (1≤l≤r<10 18 ,1≤k≤10 1≤l≤r<1018,1≤k≤10 ) — the borders of the segment and the maximum number of different digits.

Output

Print one integer — the sum of numbers from l l to r r such that each number contains at most k k different digits, modulo 998244353 998244353 .

Examples

Input
10 50 2
Output
1230
Input
1 2345 10
Output
2750685
Input
101 154 2
Output
2189

题意:求区间[L,R]的满足digit种类不超过K的数字之和。

思路:与常规我数位DP不一样的是,这里求是不是个数,而是这些书之和。所以我们要记录一个二元组(x,y)分别表示(子树之和,子树叶子个数)。

(数位DP其实就是一棵树,子树相同的时候可以直接调用答案)

那么当前节点为根的树的信息=(所有子树的和+当前位*叶子个数,所有叶子个数之和)。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define pii pair<int,int>
#define mp make_pair
using namespace std;
const int Mod=;
pii dp[][<<],O=mp(,);
int v[],num[<<],K,q[],tot;
pii dfs(int pos,int st,int lim)
{
if(!lim&&dp[pos][st]!=O) return dp[pos][st];
if(pos==) return mp(,);
int up=; pii res=O,tmp; if(lim) up=q[pos-];
rep(i,,up){
if(num[st|(<<i)]<=K){
tmp=dfs(pos-,st|(<<i),lim&&i==up);
(res.second+=tmp.second)%=Mod;
(res.first+=tmp.first)%=Mod;
(res.first+=(ll)v[pos-]*i%Mod*tmp.second%Mod)%=Mod;
}
}
return dp[pos][st]=res;
}
int cal(ll x)
{
if(x==) return ;
tot=; int ans=;
while(x) q[++tot]=x%,x/=;
memset(dp,,sizeof(dp));
rep(i,,tot){
ll up=; if(i==tot) up=q[tot];
rep(j,,up){
pii tmp=dfs(i,<<j,(i==tot)&&(j==q[tot]));
(ans+=(ll)v[i]*j%Mod*tmp.second%Mod)%=Mod;
(ans+=tmp.first)%=Mod;
}
}
return ans;
}
int main()
{
rep(i,,<<) num[i]=num[i>>]+(i&);
v[]=; rep(i,,) v[i]=(ll)v[i-]*%Mod;
ll L,R; scanf("%lld%lld%d",&L,&R,&K);
printf("%d\n",((cal(R)-cal(L-))%Mod+Mod)%Mod);
return ;
}

CodeForces - 1073E :Segment Sum (数位DP)的更多相关文章

  1. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  2. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  3. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...

  4. Codeforces #55D-Beautiful numbers (数位dp)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  6. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  7. codeforce 1073E. Segment Sum

    看到这个就是数位DP了,然而细节极多,对于i=1状态直接判了,还有最后一位直接算了 设f[i][zt][0/1]表示枚举到第i位,用了那些数字,是否有前导0(前导0不计入数字,否则就不知道后面有没有0 ...

  8. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

  9. CodeForces 628D Magic Numbers (数位dp)

    题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...

  10. zoj 3962 Seven Segment Display 数位dp

    非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能AC. 思路:dp(i, j)表示已经考虑了前i位,前i位的和为j的贡献.如果当前的选择一直是最大的选择,那么就必须从0~下一位的最大 ...

随机推荐

  1. Linux上安装Oracle的教程-centos7

    一.安装Oracle前准备 1.创建运行oracle数据库的系统用户和用户组   [humf@localhost ~]$ su root #切换到root   Password:   [root@lo ...

  2. Python string常用函数

    2017-07-03 23:26:08 1..replace(self, old, new, count=-1) replace()函数将旧字符串替换为新字符串,最后一个参数count为可选项,表示替 ...

  3. JDK1.5 新特性

    1:自动装箱与拆箱 自动装箱:每当需要一种类型的对象时,这种基本类型就自动地封装到与它相同类型的包装中. 自动拆箱:每当需要一个值时,被装箱对象中的值就被自动地提取出来,没必要再去调用intValue ...

  4. 2月9日 Time and Date(Ruby基础) \n 2月10日 ,使用Proc,block对象化。

    20.1Time类 ,Data类 Time.now或者Time.new:获取当前时间. 相关Method: year, month, hour, min ,sec, wday: 一周中的第几天,返回整 ...

  5. ubuntu下没有Language Support

    sudo apt-get installlanguage-selector-gnome

  6. 杜伦大学提出GANomaly:无需负例样本实现异常检测

    杜伦大学提出GANomaly:无需负例样本实现异常检测 本期推荐的论文笔记来自 PaperWeekly 社区用户 @TwistedW.在异常检测模块下,如果没有异常(负例样本)来训练模型,应该如何实现 ...

  7. 什么是 SSO 与 CAS?

    SSO SSO 是英文 Single Sign On 的缩写,翻译过来就是单点登录.顾名思义,它把两个及以上个产品中的用户登录逻辑抽离出来,达到只输入一次用户名密码,就能同时登录多个产品的效果. 打个 ...

  8. 软工作业No.5 甜美女孩第三周yep

    需求&原型改进: 1. 针对课堂讨论环节老师和其他组的问题及建议,对修改选题及需求进行修改 (5分) 没有问题及建议 2. 修改完善上周提交的需求规格说明书(10分) 上周的<需求规格说 ...

  9. L1-005 考试座位号

    每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考 ...

  10. Swift网络封装库Moya中文手册之Targets

    Targets 使用Moya,我们首先需要定义一个target - 这通常是继承 TargetType 协议的 枚举 变量.接下来,你的app只需要处理这些targets,也就是一些你希望调用API完 ...