Salazar Slytherin's Locket CodeForces - 855E

http://www.cnblogs.com/ftae/p/7590187.html

数位dp:

http://www.cnblogs.com/xz816111/p/4809913.html

http://blog.csdn.net/wust_zzwh/article/details/52100392

1.

 #include<cstdio>
#include<cstring>
typedef long long LL;
LL q,b,l,r;
LL ans[][][][];
LL w[];//记录拆出来的i进制的各个数字
//ans[i][j][state][k]:i进制,i进制下j长度,状态为state,k表示是否处于前导0
//状态为0-b-1的数字出现奇数/偶数次
LL dp(LL jz,LL pos,LL state,bool pre0,bool limit)//pre0表示当前位的前一位是不是0
{
if(pos<) return !state;//只有状态全0也就是所有数字出现偶数次才有1个答案,曾经忘记
if(!limit&&ans[jz][pos][state][pre0]!=-)
return ans[jz][pos][state][pre0];
LL i,res=,end=limit?w[pos]:(jz-);//当前位上界,limit为0表示前面某一位已经取了不是最高值,那么后面的位可以取0-9//曾经把最大值错写成9
for(i=;i<=end;i++)
if(i==&&pre0)//如果当前位取0,且前面都是前导0,那么开头的0显然是不计入状态的
res+=dp(jz,pos-,state,,limit&&i==w[pos]);//曾经忘记写limit&&
else
res+=dp(jz,pos-,state^(<<i),,limit&&i==w[pos]);
return limit?res:(ans[jz][pos][state][pre0]=res);
}
LL get(LL b,LL x)
{
LL g;
for(g=;x>;x/=b) w[++g]=x%b;
return dp(b,g,,,);//这一种的返回的直接就是1-x的总答案
}
int main()
{
memset(ans,-,sizeof(ans));
scanf("%I64d",&q);
while(q--)
{
scanf("%I64d%I64d%I64d",&b,&l,&r);
printf("%I64d\n",get(b,r)-get(b,l-));
}
return ;
}

2.

 #include<cstdio>
#include<cstring>
typedef long long LL;
LL q,b,l,r;
LL ans[][][];
LL w[];
//ans[i][j][state][k]:i进制,i进制下j长度,状态为state,k表示是否处于前导0
//状态为0-b-1的数字出现奇数/偶数次
LL dp(LL jz,LL pos,LL state,bool pre0,bool limit)//pre0表示当前位的前一位是不是0
{
if(pos<) return !state;//只有状态全0也就是所有数字出现偶数次才有1个答案
if(!limit&&!pre0&&ans[jz][pos][state]!=-)
return ans[jz][pos][state];
LL i,res=,start=pre0?:,end=limit?w[pos]:(jz-);//当前位上界,limit为0表示前面某一位已经取了不是最高值,那么后面的位可以取0-9
for(i=start;i<=end;i++)
res+=dp(jz,pos-,state^(<<i),,limit&&i==w[pos]);
if(!limit&&!pre0)
ans[jz][pos][state]=res;
return res;
}
LL get(LL b,LL x)
{
LL g,i,ret=;
for(g=;x>;x/=b) w[++g]=x%b;
for(i=g;i>=;i--) ret+=dp(b,i,,,i==g);//如果总位数不到g,那么显然所有数字都可以随便取
return ret;//这一种的dp返回的是0(1)-x的数中i位的数满足条件的答案
}
int main()
{
memset(ans,-,sizeof(ans));
scanf("%I64d",&q);
while(q--)
{
scanf("%I64d%I64d%I64d",&b,&l,&r);
printf("%I64d\n",get(b,r)-get(b,l-));
}
return ;
}

3.

Salazar Slytherin's Locket CodeForces - 855E的更多相关文章

  1. Codeforces 855E - Salazar Slytherin's Locket

    855E - Salazar Slytherin's Locket 题意 给出一个区间,问这个区间内有多少个数满足,将这个数转化为某个进制后,所有数的数量都为偶数. 分析 谁能想到 数位DP 的裸题竟 ...

  2. Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索

    https://codeforces.com/problemset/problem/65/D 哈利波特!一种新思路的状压记忆化dfs,记得每次dfs用完要减回去.而且一定是要在dfs外部进行加减!防止 ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Boom!!!计算机系统,从理解到爆炸,Bomblab

    进入文件夹下 ./bomb 開始执行炸弹 对于炸弹command not found之类的鬼畜情况: chmod 777 bomb 然后再执行炸弹 objdump -d bomb > bomb. ...

  2. 字符串函数---strcat()与strncat具体解释及实现

    一.strcat()与strncat() strcat():strcat(dest,src);        strcat把src所指向的字符加入到dest结尾处(覆盖原dest结尾处的'\0').并 ...

  3. UVa 12587 Reduce the Maintenance Cost(Tarjan + 二分 + DFS)

    题意:n个城市(n <= 10000), 有m条边(m <= 40000),每一个城市有一个维护费用Cost(i),除此之外,每条边的维修费用为去掉该边后不能通信的城市对数与边权的积.这个 ...

  4. apt仓库以及apt-get分析

    1 debian repository 参考:https://wiki.debian.org/DebianRepository 1.1 版本代号 sid,still in development,该版 ...

  5. 如何在退出Hue后关闭Spark会话

    https://mp.weixin.qq.com/s/QLu9XhDv1YuK19VCJsFXGw

  6. 使用libcurl的包装库cpr发起http请求

    cpr GitHub地址https://github.com/whoshuu/cpr 简单示例:cpr_http_request.cpp #include <iostream> #incl ...

  7. Java,如何获取文件的MD5值

    MessageDigest类封装得很不错,简单易用 不多说,直接上代码 import java.io.FileInputStream;import java.security.MessageDiges ...

  8. JUNO eclipse Version: 4.2.0 添加svn插件

    1.下载最新的这个版本的SVN http://www.eclipse.org/subversive/latest-releases.php 实际的下载地址 http://www.eclipse.org ...

  9. 转载-STM32片上FLASH内存映射、页面大小、寄存器映射

    原文地址:http://blog.chinaunix.net/uid-20617446-id-3847242.html 本文以STM32F103RBT6为例介绍了片上Flash(Embedded Fl ...

  10. POJ3104 Drying —— 二分

    题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...