Codeforces 855E - Salazar Slytherin's Locket
855E - Salazar Slytherin's Locket
题意
给出一个区间,问这个区间内有多少个数满足,将这个数转化为某个进制后,所有数的数量都为偶数。
分析
谁能想到 数位DP 的裸题竟然放到了 E , 美滋滋。
考虑用一个二进制数记录每种数出现的次数的奇偶性,很容易用异或去操作。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
ll dp[11][70][2048][2]; // dp[i][j][state][k] : i 进制 , j 长度 , 状态为 state , k 是否处于前导 0
int bit[70];
ll dfs(int jz, int len, int state, int pre0, int limit) {
if(!len) return !state;
if(!limit && dp[jz][len][state][pre0] != -1) return dp[jz][len][state][pre0];
ll res = 0;
int mx = limit ? bit[len] : (jz - 1);
for(int i = 0; i <= mx; i++) {
if(!i && pre0) {
res += dfs(jz, len - 1, state, 1, limit && (i == mx));
} else {
res += dfs(jz, len - 1, state ^ (1 << i), 0, limit && (i == mx));
}
}
if(!limit) dp[jz][len][state][pre0] = res;
return res;
}
ll solve(ll n, int jz) {
int l = 0;
while(n) {
bit[++l] = n % jz;
n /= jz;
}
return dfs(jz, l, 0, 1, 1);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
memset(dp, -1, sizeof dp);
int Q;
cin >> Q;
while(Q--) {
int b; ll l, r;
cin >> b >> l >> r;
cout << solve(r, b) - solve(l - 1, b) << endl;
}
return 0;
}
Codeforces 855E - Salazar Slytherin's Locket的更多相关文章
- Salazar Slytherin's Locket CodeForces - 855E
Salazar Slytherin's Locket CodeForces - 855E http://www.cnblogs.com/ftae/p/7590187.html 数位dp: http:/ ...
- Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索
https://codeforces.com/problemset/problem/65/D 哈利波特!一种新思路的状压记忆化dfs,记得每次dfs用完要减回去.而且一定是要在dfs外部进行加减!防止 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- Mybatis缓存机制及mybatis的各个组成部分
Mybatis 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 ...
- hdu1281(棋盘游戏,车的放置)
Problem Description 给定一个n * m的棋盘,在棋盘里放尽量多的国际象棋中的车,使他们不能相互攻击 已知有些格子不能放置,问最多能放置多少个车 并计算出必须棋盘上的必须点. Inp ...
- 解析json方式之net.sf.json
前面转载了json解析的技术:fastjson,今天说下另外一种技术. 下载地址 本次使用版本:http://sourceforge.net/projects/json-lib/files/json- ...
- border-image
一.border-image的兼容性 border-image可以说是CSS3中的一员大将,将来一定会大放光彩,其应用潜力真的是非常的惊人.可惜目前支持的浏览器有限,仅 Firefox3.5,chro ...
- 查找算法总结Java实现
之前对查找算法做的一些简单总结与实现: 查找算法时间复杂度: 1.二分查找的实现(待补充) public class Test { //循环实现二分查找 public static int binar ...
- 最大流算法 ISAP 模板 和 Dinic模板
ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...
- sql 批量更新表中多字段为不同的值
,),,),rand()) select newid() ,) update tablename , FB,)) , ), FC,)) , )
- win端git连接私服仓库+上传本地项目+从服务器下载文件到win
win端git连接私服仓库: 1.win端 检查c:/Users/用户/.ssh/目录下是否有config文件(!!!没有任何后缀名).如果没有则新建config文件,然后修改添加如下内容: Host ...
- Spark优化之一:分布式下的map操作是闭包
例如对一个JavaPairRDD<String, String>做遍历操作,常见的,我们可以通过先通过collect()操作将它转化为Map对象再进行遍历,也可以使用Spark提供的map ...
- 让button居中显示的的标签
<center> <input type="button" class="buttoncls" style="width:80px& ...