CodeForces - 1073E :Segment Sum (数位DP)
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
10 50 2
1230
1 2345 10
2750685
101 154 2
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)的更多相关文章
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
- Codeforces #55D-Beautiful numbers (数位dp)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- codeforce 1073E. Segment Sum
看到这个就是数位DP了,然而细节极多,对于i=1状态直接判了,还有最后一位直接算了 设f[i][zt][0/1]表示枚举到第i位,用了那些数字,是否有前导0(前导0不计入数字,否则就不知道后面有没有0 ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- CodeForces 628D Magic Numbers (数位dp)
题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...
- zoj 3962 Seven Segment Display 数位dp
非常好的一个题,可以比赛时想到的状态太奇葩,不方便转移,就一直没能AC. 思路:dp(i, j)表示已经考虑了前i位,前i位的和为j的贡献.如果当前的选择一直是最大的选择,那么就必须从0~下一位的最大 ...
随机推荐
- string_01
内存释放和越界 越界:(1).复制越界,(2).取值越界 构造函数 (1).默认 空(无参) (2).拷贝(const string &) (3).带参数: const char * // Z ...
- spring-cloud: eureka之:ribbon负载均衡自定义配置(二)
spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...
- FMUtils.KeyboardHook 轻量级键盘监听器
Nuget添加引用 Install-Package FMUtils.KeyboardHook 调用示例: var KeyboardHook = new Hook("Global Action ...
- Python下图片的高斯模糊化的优化
资源下载 #本文PDF版下载 Python下图片的高斯模糊化的优化(或者单击我博客园右上角的github小标,找到lab102的W6目录下即可) #本文代码下载 高斯模糊(一维)优化代码(和本文方法集 ...
- python-day37--concurrent.futures模块 实现进程池与线程池
1.concurrent.futures模块 直接内置就是 异步的提交 ,如果你想同步也可以实现(p.submit(task,i).result()即同步执行) 2.属性和方法: 1.submit ...
- hdu多校(二) 1004 1007 1010
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- dp练习(2)——老鼠的旅行
1267 老鼠的旅行(来源:codevs) #include "bits/stdc++.h" using namespace std; ][]; ][]; int main() { ...
- Bata验收互评
小组的名字和链接 优点 缺点,bug报告 最终名次 编程题全队 ①限制用户重复注册同一个邮箱②注册之后可以弹出用户名,不用手动输入③细节考虑到位④面板可拖动,增删改查,还能添加成员 Q1:程序有什么具 ...
- 附录A——面向对象基础
在学习设计模式之前,C#语言中一些基本的面向对象的知识还是应该具备的,比如像继承.多态,接口.抽象类,集合.泛型等. A.2 类与实例 什么是对象? 一切事物(事和物)都是对象,对象就是可以看到.感觉 ...
- OAF点击事件对页面组件的Required属性不验证
在实际的需求中,OAF页面上很多字段设置了Required=YES,但是我们在点击某些按钮的时候,并不希望浏览器对其进行验证,可以通过设置 Disable Server Side Validation ...