Manthan, Codefest 17-E-Salazar Slytherin's Locket(数位DP)

来源:互联网 发布:花都金蝶软件代理商 编辑:程序博客网 时间:2024/05/18 12:31
E. Salazar Slytherin's Locket
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 bili 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 biliri (2 ≤ bi ≤ 101 ≤ li ≤ ri ≤ 1018).

Output

You have to output q lines, each containing a single integer, the answer to the corresponding query.

Examples
input
22 4 93 1 10
output
12
input
22 1 1005 1 100
output
214
Note

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.

题意:给你q次查询,每次给你个进制限定和区间,问你这个区间内有多少有魔力的数。

有魔力的数定义为数的b进制下的每一位数都出现偶数次。

题解:直接数位DP即可。我们可以考虑用状态压缩记录每个数出现的次数。。

#include<set>  #include<map>     #include<stack>            #include<queue>            #include<vector>    #include<string> #include<time.h>#include<math.h>            #include<stdio.h>            #include<iostream>            #include<string.h>            #include<stdlib.h>    #include<algorithm>   #include<functional>    using namespace std;            #define ll __int64       #define inf 1000000000       #define mod 1000000007             #define maxn  100005#define lowbit(x) (x&-x)            #define eps 1e-9int b[maxn],digit[100],base;ll l[maxn],r[maxn],dp[64][1<<12],ans[maxn];ll dfs(int cnt,int mask,int limit,int head){if(!limit && !head && dp[cnt][mask]!=-1)return dp[cnt][mask];if(cnt==0)return (mask==0) && (!head);ll i,res=0;if(limit){if(head){for(i=1;i<digit[cnt];i++)res+=dfs(cnt-1,mask^(1<<i),0,0);res+=dfs(cnt-1,mask^(1<<digit[cnt]),1,0);res+=dfs(cnt-1,mask,0,1);}else{for(i=0;i<digit[cnt];i++)res+=dfs(cnt-1,mask^(1<<i),0,0);res+=dfs(cnt-1,mask^(1<<digit[cnt]),1,0);}}else{if(head){for(i=1;i<base;i++)res+=dfs(cnt-1,mask^(1<<i),0,0);res+=dfs(cnt-1,mask,0,1);}else{for(i=0;i<base;i++)res+=dfs(cnt-1,mask^(1<<i),0,0);}}if(!limit && !head)dp[cnt][mask]=res;return res;}ll work(ll x){if(x<=0)return 0;int len=0;while(x){digit[++len]=x%base;x/=base;}return dfs(len,0,1,1);}int main(void){int q,i,j,k;scanf("%d",&q);for(i=1;i<=q;i++)scanf("%d%I64d%I64d",&b[i],&l[i],&r[i]);for(i=2;i<=10;i++){base=i;for(j=0;j<64;j++)for(k=0;k<(1<<i);k++)dp[j][k]=-1;for(j=1;j<=q;j++){if(b[j]!=i)continue;ans[j]=work(r[j])-work(l[j]-1);}}for(i=1;i<=q;i++)printf("%I64d\n",ans[i]);return 0;}



阅读全文
0 0