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. [ios]ios tts的使用

    参考:http://www.tekuba.net/program/327/ http://blog.sina.com.cn/s/blog_923fdd9b0101flx3.html iOS平台由于本身 ...

  2. Caffe 学习系列

    学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...

  3. FASTQ 数据质量统计工具

    主流工具: FastQC fqcheck readfq 拿到测序数据的第一步就是做质量控制 fqcheck之后得到的结果: 它会统计每条reads,按read 1-100位点计算每个位置的ACGTN含 ...

  4. XAML中格式化日期

    要求被格式化数据的类型是DateTime StringFormat='yyyy-MM-dd' StringFormat={}{0:yyyy-MM-dd}

  5. English trip -- Phonics 1 ar

    Xu言: Learning is not a happy thing, but happiness always comes with learning...    - loki.valentine ...

  6. 第7章使用请求测试-测试API . Rspec: everyday-rspec实操。

    测试应用与非人类用户的交互,涵盖外部 API 7.1request test  vs feature test 对 RSpec 来说,这种专门针 对 API 的测试最好放在 spec/requests ...

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

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

  8. php date时间的获取

    1.得到当前时间:date('Y-m-d H:i:s',time()); 2.得到这个月有多少天:echo $getDay = date('t',date('Y-m-d', time())); 3.得 ...

  9. PHP:第二章——PHP中的equire与incude语句

    <?php header("Content-Type:text/html;charset=utf-8"); /* include: include_once//include ...

  10. 如何以编程方式打印到在 MFC 中的非默认打印机

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105790245b09c0252bd7a74a2485d315d2390f0750 ...