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个的数的 ...
随机推荐
- flask自动代码自动补全
编写py文件时,无法补全: 在app对象后面添加:# type:Flask app=Flask(__name__) # type:Flask from flask import Flask, fl ...
- P4381 [IOI2008]Island(基环树+单调队列优化dp)
P4381 [IOI2008]Island 题意:求图中所有基环树的直径和 我们对每棵基环树分别计算答案. 首先我们先bfs找环(dfs易爆栈) 蓝后我们处理直径 直径不在环上,就在环上某点的子树上 ...
- hibernate validator自定义校验注解以及基于服务(服务组)的校验
hibernate validator是Bean Validation 1.1 (JSR 349) Reference Implementation,其广泛的应用在mvc的参数校验中,尤其是使用服务端 ...
- 【题解】Luogu P3901 数列找不同
我博客中对莫队的详细介绍 原题传送门 不错的莫队练手题 块数就直接取sqrt(n) 对所有询问进行排序 排序第一关键词:l所在第几块,第二关键词:r的位置 考虑Ai不大,暴力开数组 add时如果加之后 ...
- 为什么预处理和参数化查询可以防止sql注入呢?
在传统的写法中,sql查询语句在程序中拼接,防注入(加斜杠)是在php中处理的,然后就发语句发送到mysql中,mysql其实没有太好的办法对传进来的语句判断哪些是正常的,哪些是恶意的,所以直接查询的 ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Git Add,Git别名等
一,Git Add 1. git add -A 保存所有的修改 2. git add . 保存新的添加和修改,但是不包括删除 3. git add -u 保存修改和删除,但是不 ...
- Qt重绘机制
一.引发重绘的事件 1.调用repaint() 2.调用uodate() 二.控件hide或者show 三.其他 ps: repaint函数是立即重绘,没有优化 update会优化,异步重绘,所以如果 ...
- ODAC(V9.5.15) 学习笔记(七)TOraUpdateSQL
名称 类型 说明 DataSet 指向需要执行更新操作的数据集 DeleteObject 当执行删除操作时,通过该属性执行另外一个数据集,由后者来执行更多的删除动作 DeleteSQL TString ...
- ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(2)
2.连接相关 名称 类型 说明 Connection 指向一个数据库连接对象 Disconnected 设置为True将在数据库关闭后继续保持数据集的开启状态. 3. 数据获取 名称 类型 说明 Fe ...