Codeforces 1073 E - Segment Sum
思路:
数位dp
我们平时做的数位dp都是求满足条件的数的个数, 这里要求满足条件的数的和
只要在原来的基础上求每一位的贡献就可以了,所以传参数时要传两个
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int MOD = ;
int k;
pll dp[][];
int a[], tot;
LL pw[];
pll dfs(int pos, int s, bool zero, bool limit) {
if(!pos) return {__builtin_popcount(s) <= k, };
if(!limit && !zero && ~dp[pos][s].fi) return dp[pos][s];
int up = ;
if(limit) up = a[pos];
pll ans = {, };
for (int i = ; i <= up; i++) {
pll res;
if(zero && i == ) res = dfs(pos-, s, zero, limit&&i==up);
else res = dfs(pos-, s|(<<i), zero&&i==, limit&&i==up);
(ans.fi = ans.fi + res.fi) %= MOD;
(ans.se = ans.se + res.se + i*res.fi%MOD*pw[pos-]%MOD) %= MOD;
}
if(!limit && !zero) dp[pos][s] = ans;
return ans;
}
void init() {
pw[] = ;
for (int i = ; i < ; i++) pw[i] = (pw[i-] * ) % MOD;
for (int i = ; i < ; i++)
for (int j = ; j < ; j++) dp[i][j].fi = dp[i][j].se = -;
}
LL solve(LL n) {
tot = ;
init();
while(n) {
a[++tot] = n % ;
n /= ;
}
return dfs(tot, , , ).se; }
int main() {
LL l, r;
scanf("%lld %lld %d", &l, &r, &k);
printf("%lld\n", (solve(r) - solve(l-) + MOD) % MOD);
return ;
}
Codeforces 1073 E - Segment Sum的更多相关文章
- CF 1073 E. Segment Sum
https://codeforces.com/problemset/problem/1073/E 题意:[l,r]中,出现0—9数字的种类数不超过k的数的和 dp[i][j][0/1] 表示 dfs到 ...
- 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 ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- CF1073E Segment Sum 解题报告
CF1073E Segment Sum 题意翻译 给定\(K,L,R\),求\(L~R\)之间最多不包含超过\(K\)个数码的数的和. \(K\le 10,L,R\le 10^{18}\) 数位dp ...
- CF1073E Segment Sum 自闭了
CF1073E Segment Sum 题意翻译 给定\(K,L,R\),求\(L\)~\(R\)之间最多不包含超过\(K\)个数码的数的和. \(K<=10,L,R<=1e18\) 我 ...
- Codeforces 963 A. Alternating Sum(快速幂,逆元)
Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum
https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
随机推荐
- P5290 [十二省联考2019]春节十二响(堆+启发式合并)
P5290 [十二省联考2019]春节十二响 从特殊到一般 我们先看链的情况. 我们把点$1$左右的两条子链分别扔入堆里 每次取出两个堆的最大值,把答案累加上更大的那个(另一堆为空则直接加上去). 那 ...
- 20145320周岐浩 web安全基础实践
20145320周岐浩 web安全基础实践 一.实验后回答问题 (1)SQL注入攻击原理,如何防御 一.SQL注入攻击原理 SQL注入攻击值得是通过构建特殊的输入作为参数传入web应用程序,而这些输入 ...
- Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划
In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlop ...
- 集训DAYn——拉格朗日插值法
看zzq大佬的博客,看到了这个看似很深奥的东西,实际很简单(反正比FFT简单,我是一个要被FFT整疯了的孩子) 拉格朗日插值法 是什么 可以找到一个多项式,其恰好在各个观测点取到观测到的值.这样的多项 ...
- Asp.Net 之 OnClientClick 与 OnClick 的执行顺序
Asp.net 中 OnClientClick 与 OnClick 的执行顺序为:客户端的OnClientClick先执行,服务器端的OnClick后执行. 拓展:在执行完客户端的OnClientCl ...
- LOJ121 「离线可过」动态图连通性
思路 动态图连通性的板子,可惜我不会在线算法 离线可以使用线段树分治,每个边按照存在的时间插入线段树的对应节点中,最后再dfs一下求出解即可,注意并查集按秩合并可以支持撤销操作 由于大量使用STL跑的 ...
- (转)How Hash Algorithms Work
本文转自:http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software Home Posted: Novembe ...
- postgreSql——时区问题
timestamptz.timestamp SELECT ts AT TIME ZONE 'UTC' FROM ( VALUES (timestamptz '2012-03-05 17:00:00+0 ...
- Gtk 窗口,控件,设置(添加图片等)
1.关于窗口 // 创建顶层窗体,后面有POPUP的 GtkWidget *main_window; main_window = gtk_window_new (GTK_WINDOW_TOPLEV ...
- Highlight.js语法突出显示
正如我在这个博客开始之前所说的最小可行产品,因此我在几个小时内就开始运行了.这种方法在初始推动之后会减慢你的速度.最新的例子是要提供语法高亮的代码片段. 规格由于使用静态网站生成器,我的狩猎的开始立即 ...