POJ
来源:互联网 发布:主流机械设计软件 编辑:程序博客网 时间:2024/06/03 19:54
定义状态dp[pos][zero][one],pos为当前数位,zero为前缀中0的个数,one为前缀中1的个数。
注意前导0!假如要计算小于等于12的Round Number个数,12的长度为4,在dfs到3的时候,路径上的数字依次为0011,0的个数大于1的个数,所以3是符合条件的。真的对吗?
实际上前两个0都是前导0,去掉前导0之后,就发现3其实是不合法的了。
所以dfs的时候,维护一个变量lead,表示当前数位的前缀中有没有1。
代码如下:
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>using namespace std;int dp[50][50][50];int digit[50];int dfs(int pos, int zero, int one, int lead, int limit){ if (!pos) return zero >= one; if (!limit && dp[pos][zero][one] != -1) return dp[pos][zero][one]; int up = limit ? digit[pos] : 1; int ans = 0; for (int i = 0; i <= up; i++) ans += dfs(pos - 1, lead ? 0 : zero + (i == 0), one + (i == 1), lead && !i, limit && i == up); return limit ? ans : dp[pos][zero][one] = ans;}int cal(int n){ int len = 0; while (n) { digit[++len] = n % 2; n >>= 1; } return dfs(len, 0, 0, 1, 1);}int main(){ int s, t; memset(dp, -1, sizeof(dp)); scanf("%d%d", &s, &t); printf("%d\n", cal(t) - cal(s - 1)); return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Nodejs 异步回调之异常
- vs编译生成之后报错
- JVM——内存管理和垃圾回收
- Java实现直接插入排序
- 从jQuery学到的几件事情
- POJ
- python通过thrift方式连接hive
- vue2.0中vuex到底是干什么的?怎么使用?
- javascript数组方法汇总
- PHP常用函数总结(180多个)
- 关于sqlsqlserver 对时间的截取转换操作
- Map值排序
- 3dsmax中的switch map用法
- [JZSC2017]【NOIP2017提高组模拟7.4】总结