Description
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 ll and rr (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 $b_i$ , $l_i$ and $r_i$ , the base and the range for which you have to find the count of magic numbers.
求$l cdots r$之间转成$b$进制后,$0,1,2 cdots b-2,b-1$都出现偶数次的数的个数。
Solution
数位DP,有一个记忆化的优化
如果从高位到低位枚举,可以发现对于确定的进制数,确定的剩余位数和确定的需要每个数字的奇偶状态,且不在边界,数的个数是一样的

#include<iostream> #include<cstring> #include<cstdio> using namespace std; long long q,b,l,r,num[100],tot,dp[12][65][(1<<11)-1]; inline long long read() { long long w=0,f=1; char ch=0; while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { w=(w<<1)+(w<<3)+ch-'0'; ch=getchar(); } return w*f; } long long dfs(long long pos,long long sta,bool zer,bool lim) { if(!pos) { return !sta; } if(!zer&&!lim&&dp[b][pos][sta]!=-1) { return dp[b][pos][sta]; } long long top=lim?num[pos]:b-1,ret=0; for(long long i=0;i<=top;i++) { ret+=dfs(pos-1,(!i&&zer)?sta:sta^(1<<i),!i&&zer,lim&&(i==top)); } if(!zer&&!lim) { dp[b][pos][sta]=ret; } return ret; } long long solve(long long x) { tot=0; while(x) { num[++tot]=x%b; x/=b; } return dfs(tot,0,true,true); } int main() { memset(dp,-1,sizeof(dp)); q=read(); for(long long i=1;i<=q;i++) { b=read(); l=read(); r=read(); printf("%lld ",solve(r)-solve(l-1)); } return 0; }