• CodeForces


    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
    ",Solve(b,r)-Solve(b,l-1));
    	}
    	
    	return 0;
    }


  • 相关阅读:
    $().each() 与 $.each()区别,以及 jquery ajax 应用
    sql 随机获取数据
    Content type 'text/plain;charset=UTF-8' not supported
    mvn 安装jar到本地仓库
    idea 插件(字母顺序)
    adb连接逍遥模拟器,并自动设置和清除代理
    git仓库重新创建master分支
    js 日期格式化
    java 去除url中指定参数
    okhttp使用post出现:RFC 7230 and RFC 3986错误
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514134.html
Copyright © 2020-2023  润新知