概述
Salazar Slytherin's Locket
CodeForces - 855E
Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts teacher.
Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge's office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers l and r (both inclusive).
Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base b, all the digits from 0 to b - 1 appear even number of times in its representation without any leading zeros.
You have to answer q queries to unlock the office. Each query has three integers bi, li and ri, the base and the range for which you have to find the count of magic numbers.
Input
First line of input contains q (1 ≤ q ≤ 105) — number of queries.
Each of the next q lines contain three space separated integers bi, li, ri (2 ≤ bi ≤ 10, 1 ≤ li ≤ ri ≤ 1018).
OutputYou have to output q lines, each containing a single integer, the answer to the corresponding query.
Examples2 2 4 9 3 1 10
1 2
2 2 1 100 5 1 100
21 4
In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get:
- 4 = 1002,
- 5 = 1012,
- 6 = 1102,
- 7 = 1112,
- 8 = 10002,
- 9 = 10012.
Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1.
裸的数位dp,很不错的入门题,判断条件使用二进制压缩,每一位代表进制内的数字个数奇偶性,偶数0,奇数1,用异或实现就可以
code:
#include <iostream>
#include <cstdio>
#include <cstring>
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是否有前导零
int bit[70];
ll dfs(int base,int pos,int state,int lead,int limit){
if(pos == -1) return !state;//当全是0说明都是偶数,此时返回1
ll &dpnow = dp[base][pos][state][lead];
if(!limit && dpnow != -1)
return dpnow;
ll ans = 0;
int max_digit = limit ? bit[pos] : (base - 1);
for(int i = 0; i <= max_digit; i++){
if(i == 0 && lead){
ans += dfs(base,pos-1,state,1,limit && (i == max_digit));
}
else{
ans += dfs(base,pos-1,state ^ (1 << i),0,limit && (i == max_digit));
}
}
if(!limit) dpnow = ans;
return ans;
}
ll solve(ll n,int base){
int pos = 0;
while(n){
bit[pos++] = n % base;
n /= base;
}
return dfs(base,pos-1,0,1,1);
}
int main(){
memset(dp,-1,sizeof(dp));
int q;
scanf("%d",&q);
while(q--){
int b;
ll l,r;
scanf("%d%lld%lld",&b,&l,&r);
printf("%lldn",solve(r,b)-solve(l-1,b));
}
return 0;
}
最后
以上就是矮小红酒为你收集整理的Salazar Slytherin's Locket CodeForces - 855E (数位dp)的全部内容,希望文章能够帮你解决Salazar Slytherin's Locket CodeForces - 855E (数位dp)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复