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. VcCallC#_01

    1.C# 代码: using System; using System.Collections.Generic; //using System.Linq; using System.Text; //u ...

  2. Codeforces 768B - Code For 1(分治思想)

    768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  3. [.NET开发] C# 读写文件

    1.C#读文件 按行读取文件: public void Read(string path) { StreamReader sr = new StreamReader(path,Encoding.Def ...

  4. English trip -- Review Unit2 At school 在学校

    What do you need,Loki? I need an eraser What does he need? He needs a dictionary Where's my pencil? ...

  5. English trip -- VC(情景课)3 D Reading

    xu言: 感觉睡眠尽量还是要保证7+小时,才能让自己的脑袋更清醒一些... Read 阅读 My Family My name is Gloria. This is my family . This ...

  6. codeforces 497b// Tennis Game// Codeforces Round #283(Div. 1)

    题意:网球有一方赢t球算一场,先赢s场的获胜.数列arr(长度为n)记录了每场的胜利者,问可能的t和s. 首先,合法的场景必须: 1两方赢的场数不一样多. 2赢多的一方最后一场必须赢. 3最后一场必须 ...

  7. 『Re』知识工程作业_主体识别

    作业要求 环境路径 类似于这样的,一共50篇文档, 均为中文文档,是法院判决书的合集. 程序 程序如下,我完全使用正则表达式来实现功能, import re import glob import co ...

  8. Python基础--Python简介和入门

    ☞写在前面 在说Python之前,我想先说一下自己为什么要学Python,我本人之前也了解过Python,但没有深入学习.之前接触的语言都是Java,也写过一些Java自动化用例,对Java语言只能说 ...

  9. python-day7--%s与%d的使用,python2中的input及raw_input

    #coding:utf-8 #utf-8格式打开#%s %d# name='egon'# age=18# print('my name is',name)# print('my name is my ...

  10. html5绘制字符串

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...