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

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
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.

题意:给你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-9
int 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值