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. 高中信息技术《算法与程序设计VB(选修)》知识要点

    原博主: http://blog.sina.com.cn/buyanshibai [转载] (一)算法 1.定义 相关题解: 1算法:就是解决问题的方法和步骤.算法是程序设计的“灵魂”,算法+数据结构 ...

  2. 导致SQL执行慢的原因

    索引对大数据的查询速度的提升是非常大的,Explain可以帮你分析SQL语句是否用到相关索引. 索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左 ...

  3. error C3872: '0x3000': this character is not allowed in an identifier 解决方法

    文章参考地址:http://blog.csdn.net/danxuezx/article/details/5096497 编译时遇到这个错误多半是从网上拷贝一段代码到VS里然后编译时产生的,这是因为拷 ...

  4. BZOJ day8

    好吧,, 补一天题解. 1001  狼抓兔子 妥妥的网络流啊,难度仅次于草地排水,边都给出来了.就是注意反向边也要有流量就行. 1007 水平可见直线 这个题按斜率排序(注意不是绝对值),然后将直线入 ...

  5. Ubuntu下安装LNMP之php7的安装并配置Nginx支持php及卸载php

    据了解,php7是比之前的版本性能快很多的.http://php.net/get/php-7.2.2.tar.gz/from/a/mirror 安装前也可提前将相关依赖库安装好,或者在安装php时若安 ...

  6. How do I see what character set a database / table / column is in MySQL?

    Q: How do I see what the character set that a MySQL database, table and column are in? Is there some ...

  7. Codeforces Round #525 (Div. 2)B. Ehab and subtraction

    B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...

  8. 自定义toolbar教程

    1.写toolbar的布局文件 ,toolbar.xml <?xml version="1.0" encoding="utf-8"?> <Re ...

  9. 买卖股票的最佳时机 [ leetcode ]

    原题地址:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 给定一个数组,它的第 i 个 ...

  10. .NET之特性和属性(转)

    1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定 ...