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. 【题解】SDOI2009学校食堂

    不知道有没有人跟我有一样的感觉……实际上很多的状压DP都不难,然而调到心碎……这题题面看起来很长,还有混合的‘位运算’来吓唬人(实际上就是异或而已).但实际上只要仔细阅读,发现也是一道水水的裸题. 首 ...

  2. 【NOIP2017 D1 T1 小凯的疑惑】

    题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小 凯想知道在无法准确支付的物品中,最贵的 ...

  3. Website Collection

    前一百个卡特兰数 Candy?的博弈论总结 杜教筛资料 线性基资料 (ex)BSGS资料 斐波那契数列前300项 斯特林数 STL标准库-容器-unordered_set C++ unordered_ ...

  4. 如何在Javascript中利用封装这个特性

    对于熟悉C#和Java的兄弟们,面向对象的三大思想(封装,继承,多态)肯定是了解的,那么如何在Javascript中利用封装这个特性呢? 我们会把现实中的一些事物抽象成一个Class并且把事物的属性( ...

  5. TCP ------ TCP四次挥手(断开连接)及断开过程

    1.正常情况下,调用close(),产生的其中一个效果就是发送FIN,只有双方都调用close(),才会出现正常的四次挥手. 2.如果是服务器,发起四次挥手是在关闭accept()返回的套接字,而不是 ...

  6. rman备份与异机恢复

    一.rman备份脚本并为定时任务 #!/bin/bashsource ~/.bash_profileexport LANG=en_USBACKUP_DATE=`date +%d`#RMAN_LOG_F ...

  7. Spring 对象的声明与注入

    1.怎么把一个对象该过过交给Spring管理? 1)通过xml中配置<bean>节点来声明 2)通过Spring注解来声明,如:@Service.@Repository.@Componen ...

  8. codefoeces 671 problem D

    D. Roads in Yusland standard output Mayor of Yusland just won the lottery and decided to spent money ...

  9. algorithm ch15 FastWay

    这是DP最基础的一个问题,刚开始学习这一块,实现了一下,不要黑我巨长的参数表,新手. 代码如下: void FastWay(int l1[], int l2[], int e1, int e2, in ...

  10. PHP性能追踪及分析工具xhprof的安装与使用

    对于本地开发环境来说,进行性能分析xdebug是够用了,但如果是线上环境的话,xdebug消耗较大,配置也不够灵活,因此线上环境建议使用xhprof进行PHP性能追踪及分析. 我们今天就简单介绍一下x ...