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的更多相关文章

  1. Salazar Slytherin's Locket CodeForces - 855E

    Salazar Slytherin's Locket CodeForces - 855E http://www.cnblogs.com/ftae/p/7590187.html 数位dp: http:/ ...

  2. Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索

    https://codeforces.com/problemset/problem/65/D 哈利波特!一种新思路的状压记忆化dfs,记得每次dfs用完要减回去.而且一定是要在dfs外部进行加减!防止 ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. BZOJ5343 [Ctsc2018]混合果汁 【二分 + 主席树】

    题目链接 BZOJ5343 题解 明显要二分一下美味度,然后用尽量少的价格去购买饮料,看看能否买到\(L\)升,然后看看能否控制价格在\(g\)内 尽量少的价格,就优先先选完便宜的饮料,由于询问的是一 ...

  2. C&C++——C函数与C++函数相互调用问题

    C C++相互调用 在项目中融合C和C++有时是不可避免的,在调用对方的功能函数的时候,或许会出现这样那样的问题,但只要我的C代码和我的C++代码分别都能成功编译,那其他就不是问题.近来在主程序是C语 ...

  3. 【NOIP模拟赛】超级树 DP

    这个题我在考试的时候把所有的转移都想全了就是新加一个点时有I.不作为II.自己呆着III.连一个IV.连接两个子树中的两个V连接一个子树中的两个,然而V我并不会转移........ 这个题的正解体现了 ...

  4. cookie中的path与domain属性详解

    1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net.而跨域访问,如域A为t1 ...

  5. 安卓的progress

    https://www.cnblogs.com/wolipengbo/archive/2013/10/23/3383667.html

  6. Oulipo HDU - 1686

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  7. java,jenkins

    以前玩的是hudson ,现在玩的是jenkins.以前用的是Tomcat,现在不知道他们怎么不用... 1,装个Jenkins镜像. 2.配置项目: 先取个名字:exchange 配个svn: 构建 ...

  8. SQL性能分析

    MySQL常见瓶颈: CPU:CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据的时候. IO:磁盘I/O瓶颈发生在装入数据远大于内存容量的时候. 服务器硬件的性能瓶颈:top.free.io ...

  9. Idea工具点滴积累

    方法参数提示: keymap里面搜索 quick document,默认是F2

  10. python 调用Linux shell

    有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...