CodeForces - 855E—Salazar Slytherin's Locket(数位DP)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:42   1516   0
Time limit
2000 ms
Memory limit
262144 kB


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≤liri≤1018).

Output

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

Examples

Input
2
2 4 9
3 1 10
Output
1
2
Input
2
2 1 100
5 1 100
Output
21
4

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.

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll board[70];
ll dp[70][15][2][2][2][2][2][2][2][2][2][2];
//从左到右分别对应数位,进制,0,1,2,3,4,5,6,7,8,9的奇偶状态。 

ll DFS(ll base,ll pos,ll A,ll B,ll C,ll D,ll E,ll F,ll G,ll H,ll I,ll J,bool limit,bool lead){ 
 if(pos == -1)return !A && !B && !C && !D && !E && !F && !G && !H && !I && !J;
 if(!limit && !lead && dp[pos][base][A][B][C][D][E][F][G][H][I][J]!=-1)
            return dp[pos][base][A][B][C][D][E][F][G][H][I][J];
 
 ll up = limit?board[pos]:base-1;
 ll ans = 0;
 for(ll _=0 ; _<=up ; ++_){
  if(lead && _==0)ans += DFS(base,pos-1,A,B,C,D,E,F,G,H,I,J,limit && _==up,true);
  else switch(_){
   case 0:
    ans += DFS(base,pos-1,A^1,B,C,D,E,F,G,H,I,J,limit && _==up,false);
    break;
   case 1:
    ans += DFS(base,pos-1,A,B^1,C,D,E,F,G,H,I,J,limit && _==up,false);
    break;
   case 2:
    ans += DFS(base,pos-1,A,B,C^1,D,E,F,G,H,I,J,limit && _==up,false);
    break;
   case 3:
    ans += DFS(base,pos-1,A,B,C,D^1,E,F,G,H,I,J,limit && _==up,false);
    break;
   case 4:
    ans += DFS(base,pos-1,A,B,C,D,E^1,F,G,H,I,J,limit && _==up,false);
    break;
   case 5:
    ans += DFS(base,pos-1,A,B,C,D,E,F^1,G,H,I,J,limit && _==up,false);
    break;
   case 6:
    ans += DFS(base,pos-1,A,B,C,D,E,F,G^1,H,I,J,limit && _==up,false);
    break;
   case 7:
    ans += DFS(base,pos-1,A,B,C,D,E,F,G,H^1,I,J,limit && _==up,false);
    break;
   case 8:
    ans += DFS(base,pos-1,A,B,C,D,E,F,G,H,I^1,J,limit && _==up,false);
    break;
   case 9:
    ans += DFS(base,pos-1,A,B,C,D,E,F,G,H,I,J^1,limit && _==up,false);
    break;
  }
 } 
 if(!limit && !lead)dp[pos][base][A][B][C][D][E][F][G][H][I][J] = ans;
 return ans;
}

ll Solve(ll base,ll t){
 ll pos = 0;
 while(t){
  board[pos++] = t%base;
  t /= base;
 }
 return DFS(base,pos-1,0,0,0,0,0,0,0,0,0,0,true,true);
}

int main(){
 
 int T;
 scanf("%d",&T);
 ll b,l,r;
 memset(dp,-1,sizeof(dp));
 while(T--){
  scanf("%lld %lld %lld",&b,&l,&r);
  printf("%lld\n",Solve(b,r)-Solve(b,l-1));
 }
 
 return 0;
}


转载于:https://www.cnblogs.com/vocaloid01/p/9514134.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP