Manthan, Codefest 17-E-Salazar Slytherin's Locket(数位DP)
来源:互联网 发布:花都金蝶软件代理商 编辑:程序博客网 时间:2024/05/18 12:31
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.
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).
You have to output q lines, each containing a single integer, the answer to the corresponding query.
22 4 93 1 10
12
22 1 1005 1 100
214
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;}
- Manthan, Codefest 17 E. Salazar Slytherin's Locket 数位DP
- Manthan, Codefest 17 E. Salazar Slytherin's Locket(数位DP)(进制)
- Manthan, Codefest 17 E. Salazar Slytherin's Locket(数位DP)
- Manthan, Codefest 17: E. Salazar Slytherin's Locket(数位DP)
- Manthan, Codefest 17-E-Salazar Slytherin's Locket(数位DP)
- Manthan, Codefest 17 E. Salazar Slytherin's Locket 数位DP+状态压缩
- cf Manthan, Codefest 17 E. Salazar Slytherin's Locket
- Codeforces 855E Salazar Slytherin's Locket(数位dp)
- CodeForces 855E Salazar Slytherin's Locket(数位dp+搜索剪枝)
- codeforces Manthan, Codefest 17 B Marvolo Gaunt's Ring(dp)
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup(树形DP)
- Manthan, Codefest 17: C. Helga Hufflepuff's Cup(树形DP)
- Manthan, Codefest 17-C-Helga Hufflepuff's Cup(树形DP)
- Manthan, Codefest 17 B. Marvolo Gaunt's Ring(前后缀/dp)
- Manthan, Codefest 17 Marvolo Gaunt's Ring
- Codeforces Manthan, Codefest 17 C. Helga Hufflepuff's Cup 树型DP
- codeforces Manthan, Codefest 17 B.Marvolo Gaunt's Ring (简单dp)
- Codeforces Manthan Codefest 17 C 855CHelga Hufflepuff's Cup(树型DP)
- Discuz!开发之全局变量$_G详解
- 9.25 (周一)
- smoj1987(cdq分治+离散化/数据结构)
- HDU2147 Brave Game (巴什博弈 )
- Code Composer Studio 7.3.0安装错误。
- Manthan, Codefest 17-E-Salazar Slytherin's Locket(数位DP)
- 八大排序算法
- 仿函数小例子
- 慕课网bootstrap一
- [POJ](2251)Dungeon Master ---三维bfs
- 机器学习中的Bias,Error,Variance的区别
- hdu 2489 Minimal Ratio Tree(prim+dfs)
- android Messenger跨进程通信
- Scroller实现View弹性滑动