cf1073E(状压+数位DP+容斥)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qkoqhh/article/details/83410110

题意:求[l,r]内满足最多包含k个数位的数字

这个显然需要数位DP,然后由于窝的数位DP是先确定高位再确定低位,因此在dfs的过程中比较难去维护使用了哪些数位。。

所以选择了先枚举可选数位再进行数位DP。。

然后会发现会存在有些数位没选的情况,所以需要做一下容斥。。

至于怎么容斥其实就把状态S的真子集给去掉即可。。即设G[S]为在S限制状态下的数个数,F[S]为选的数为S的状态数的个数

F[S]=G[S]-\sum F[k]\,\,\,k\subsetneqq S

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)>>1)
#define NM 25
#define nm 100005
#define pi 3.1415926535897931
using namespace std;
const ll inf=998244353;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}





ll _x,_y,p[NM],ans[1024],_ans;
bool v[NM],_v[NM];
int cnt,tot,pp,b[NM];

struct tmp{int x;ll y;}d[NM];


tmp dfs(int n,bool f,bool _f){
    if(!n)return tmp{1,0};
    if(!f&&!_f&&v[n])return d[n];
    tmp ans;int m=f?b[n]:9;ans.x=ans.y=0;
    inc(i,0,m)if((_f&&i==0)||_v[i]){
	tmp t=dfs(n-1,f&&i==m,_f&&i==0);
	ans.y+=(i*p[n]%inf*t.x%inf+t.y)%inf;ans.y%=inf;
	ans.x+=t.x;ans.x%=inf;
    }
    if(!f&&!_f)v[n]++,d[n]=ans;
    return ans;
}

ll solve(ll t){
    tot=0;mem(d);mem(v);
    for(ll x=t;x;x/=10)b[++tot]=x%10;
    return dfs(tot,1,1).y;
}

void work(){
    int k=0;
    inc(i,0,9)if(_v[i])k|=succ(i);
    ans[k]=(solve(_y)-solve(_x-1)+inf)%inf;
}

void _dfs(int x){
    if(x==10){work();return;}
    _dfs(x+1);_v[x]++;_dfs(x+1);_v[x]=false;
}

int main(){
    p[1]=1;inc(i,2,20)p[i]=p[i-1]*10%inf;
    _x=read();_y=read();pp=read();
    _dfs(0);
    tot=succ(10)-1;
    inc(i,0,tot){
	for(int t=(i-1)&i;t;t=i&(t-1))ans[i]+=inf-ans[t],ans[i]%=inf;
	ans[i]+=inf-ans[0];ans[i]%=inf;
	if(__builtin_popcount(i)<=pp)
	    _ans+=ans[i],_ans%=inf;
    }
    return 0*printf("%lld\n",_ans);
}

E. Segment Sum

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two integers l

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

.

For example, if k=1

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

.

Input

The only line of the input contains three integers l

, r and k (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

to r such that each number contains at most k different digits, modulo 998244353

.

Examples

Input

Copy

10 50 2

Output

Copy

1230

Input

Copy

1 2345 10

Output

Copy

2750685

Input

Copy

101 154 2

Output

Copy

2189

Note

For the first example the answer is just the sum of numbers from l

to r which equals to 50⋅512−9⋅102=1230. This example also explained in the problem statement but for k=1

.

For the second example the answer is just the sum of numbers from l

to r which equals to 2345⋅23462=2750685

.

For the third example the answer is 101+110+111+112+113+114+115+116+117+118+119+121+122+131+133+141+144+151=2189

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/83410110
今日推荐