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

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 ≤ li ≤ ri ≤ 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值