题目传送门

这是一道数位DP

令 \(dp_{i,j,k}\) 为满足由 \(i\) 位组成,且其中有 \(j\) 个1,第 i 位(从右往左数)为 \(k\) 的二进制数的数量。

可以得出状态转移方程:

\(dp_{i,j,0}=dp_{i-1,j,1}+dp_{i-1,j,0}\;(2\le i,0\le j< i)\)

\(dp_{i,j,1}=dp_{i-1,j-1,0}+dp_{i-1,j-1,1}\;(2\le i,0<j\le i)\)

边界:\(dp_{1,0,0}=1,dp_{1,1,1}=1\)

对于 \(dp_{i,j,k}\),如果满足 \(1\le i,0\le j\le \lfloor \frac{i}{2}\rfloor\),则这个状态是合法的。因为0的个数为 \(i-j\),要满足 \(j\le i-j\),则 \(2j\le i\) 所以 \(j\le \lfloor \frac{i}{2}\rfloor\)

令 \(f(x)\) 为区间 \([1,x-1]\) 内的“圆数”个数,则区间 \([L,R]\) 内的“圆数”个数为 \(f(R+1)-f(L)\)。

对于求\(f(x)\),我们先将 \(x\) 转换成二进制,设其二进制位数为 \(len\)。

  1. 将二进制位数小于 \(len\) 的“圆数”个数统计到答案中。这时,对于 \(dp_{i,j,k}\),如果满足 \(1\le i,0\le j\le \lfloor \frac{i}{2}\rfloor\),则这个状态是合法的。因为0的个数为 \(i-j\),要满足 \(j\le i-j\),则 \(2j\le i\) 所以 \(j\le \lfloor \frac{i}{2}\rfloor\)

  2. 对于 \(x\) 的二进制除首位外的每一位 \(i\),都判断其是否为1 。如果为1,说明存在一些数,它们长度为 \(len\),值小于 \(x\),且二进制表示中的前 \(i-1\) 位与 \(x\) 相同,第 \(i\) 位为0 。然后将这些数中的“圆数”个数加入答案即可。这时,判断一个状态是否合法,需要考虑前 \(i-1\)位中0和1的个数。

参考代码

略微压行,轻喷。

  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. #define in __inline__
  5. typedef long long ll;
  6. #define rei register int
  7. #define FOR(i, l, r) for(rei i = l; i <= r; ++i)
  8. #define FOL(i, r, l) for(rei i = r; i >= l; --i)
  9. char inputbuf[1 << 23], *p1 = inputbuf, *p2 = inputbuf;
  10. #define getchar() (p1 == p2 && (p2 = (p1 = inputbuf) + fread(inputbuf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
  11. in int read() {
  12. int res = 0; char ch = getchar(); bool f = true;
  13. for(; ch < '0' || ch > '9'; ch = getchar())
  14. if(ch == '-') f = false;
  15. for(; ch >= '0' && ch <= '9'; ch = getchar())
  16. res = res * 10 + (ch ^ 48);
  17. return f ? res : -res;
  18. }
  19. const int N = 40;
  20. ll dp[N][N][2];
  21. int a, b, A[N], la, lb, B[N];
  22. ll solve(int x[], int len) {
  23. ll res = 0; int s0 = 0, s1 = 1;
  24. //s0表示0的个数,s1表示1的个数
  25. FOL(i, len - 1, 1) FOR(j, 0, (i >> 1)) res += dp[i][j][1];//第1类数
  26. FOL(i, len - 1, 1) {//第二类数
  27. if(x[i]) FOR(j, 0, i) if(s0 + i - j >= s1 + j) res += dp[i][j][0];
  28. //s0+i-j表首位至当前位0的个数,s1+j表首位至当前位1的个数,注意j要枚举至i
  29. x[i] ? (++s1) : (++s0);
  30. }
  31. return res;
  32. }
  33. signed main() {
  34. a = read(), b = read();
  35. for(; a; a >>= 1) A[++la] = a & 1;
  36. for(; b; b >>= 1) B[++lb] = b & 1;//转换成二进制
  37. ++B[1];
  38. for(rei i = 2; i <= lb && B[i - 1] == 2; ++i) B[i - 1] = 0, ++B[i];
  39. if(B[lb] == 2) B[lb] = 0, B[++lb] = 1;
  40. while(!A[la]) --la;
  41. while(!B[lb]) --lb;//给B加上1
  42. dp[1][0][0] = dp[1][1][1] = 1;
  43. FOR(i, 2, lb) FOR(j, 0, i) {//DP
  44. if(j < i) dp[i][j][0] = dp[i - 1][j][1] + dp[i - 1][j][0];
  45. if(j) dp[i][j][1] = dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1];
  46. }
  47. printf("%lld\n", solve(B, lb) - solve(A, la));
  48. return 0;//结束
  49. }

【题解】P6218 [USACO06NOV] Round Numbers S的更多相关文章

  1. 洛谷 P6218 [USACO06NOV] Round Numbers S

    洛谷 P6218 [USACO06NOV] Round Numbers S 题目描述 如果一个正整数的二进制表示中,\(0\) 的数目不小于 \(1\) 的数目,那么它就被称为「圆数」. 例如,\(9 ...

  2. POJ 3252 Round Numbers 数学题解

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  3. 题解【POJ3252】Round Numbers

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  4. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

  5. [BZOJ1662][POJ3252]Round Numbers

    [POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...

  6. Round Numbers(组合数学)

    Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10484 Accepted: 3831 Descri ...

  7. bzoj1662: [Usaco2006 Nov]Round Numbers 圆环数

    Description 正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如谁先挤奶的顺序.她们甚至也不能通过仍硬币的方式. 所以她们通过"round number" ...

  8. BZOJ1662: [Usaco2006 Nov]Round Numbers

    1662: [Usaco2006 Nov]Round Numbers Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 147  Solved: 84[Sub ...

  9. 【BZOJ】1662: [Usaco2006 Nov]Round Numbers 圆环数(数位dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1662 这道题折腾了我两天啊-!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 果然 ...

随机推荐

  1. 一只简单的网络爬虫(基于linux C/C++)————读取命令行参数及日志宏设计

    linux上面的程序刚开始启动的时候一般会从命令行获取某些参数,比如以守护进程运行啊什么的,典型的例子就是linux下的man,如下图所示 实现该功能可以使用getopt函数实现,该函数在头文件uni ...

  2. 最长公共子串(Longest common substring)

    问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...

  3. Java基础-数据类型的拓展

    整数拓展:进制 二进制 0b开头 十进制 八进制 0开头 十六进制 0x开头 0~9 A~F 16 int i = 10; int i1 = 010;//八进制 0开头 int i2 = 0x10;/ ...

  4. python 矢量数据转栅格数据

    from osgeo import gdal,osr,ogr#定义投影sr = osr.SpatialReference('LOCAL_CS["arbitrary"]')#在内存中 ...

  5. LeetCode--Array--Remove Element && Search Insert Position(Easy)

    27. Remove Element (Easy)# 2019.7.7 Given an array nums and a value val, remove all instances of tha ...

  6. Coursera课程笔记----计算导论与C语言基础----Week 7

    C语言中的数据成分(Week7) 内存 把内存想象成长带,带子上有许多方格,每个方格有8位(8bit) 2^10 = 1024 1B = 8 b 1KB = 1024Byte MB.GB.TB.PB- ...

  7. Linux系统上LNMP服务器的搭建

    一.确保登录用户权限为root 如果没有root权限: su  root 切换到root用户,但不切换环境变量: 或者 su - root 完整地切换到root用户环境. 二.开始下载并安装LNMP( ...

  8. AbstractList源码分析

    AbstractList 1 类图 2 字段 // 默认容量 private static final int DEFAULT_CAPACITY = 10; // 共享的空数组 private sta ...

  9. 管理环境一:venv

    初衷: 在工作的时候,我们会有很多个项目,每个项目使用的库的版本不一样,导致我们切换项目的时候会很麻烦. 比如:我有两个django项目,项目一使用的版本是 django 1.7 , 项目二使用的版本 ...

  10. 在没有RedirectAttributes的环境中如何在重定向环境中报错错误提示信息供页面使用

    Spring在无RedirectAttributes的情况下(如Interceptor.filter中)使用Flash scope 今天遇到一个应用场景: 在需要在自定义的Interceptor中判断 ...