【题解】P6218 [USACO06NOV] Round Numbers S
这是一道数位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\)。
将二进制位数小于 \(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\)
对于 \(x\) 的二进制除首位外的每一位 \(i\),都判断其是否为1 。如果为1,说明存在一些数,它们长度为 \(len\),值小于 \(x\),且二进制表示中的前 \(i-1\) 位与 \(x\) 相同,第 \(i\) 位为0 。然后将这些数中的“圆数”个数加入答案即可。这时,判断一个状态是否合法,需要考虑前 \(i-1\)位中0和1的个数。
参考代码
略微压行,轻喷。
#include <cstdio>
#include <cstring>
using namespace std;
#define in __inline__
typedef long long ll;
#define rei register int
#define FOR(i, l, r) for(rei i = l; i <= r; ++i)
#define FOL(i, r, l) for(rei i = r; i >= l; --i)
char inputbuf[1 << 23], *p1 = inputbuf, *p2 = inputbuf;
#define getchar() (p1 == p2 && (p2 = (p1 = inputbuf) + fread(inputbuf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
in int read() {
int res = 0; char ch = getchar(); bool f = true;
for(; ch < '0' || ch > '9'; ch = getchar())
if(ch == '-') f = false;
for(; ch >= '0' && ch <= '9'; ch = getchar())
res = res * 10 + (ch ^ 48);
return f ? res : -res;
}
const int N = 40;
ll dp[N][N][2];
int a, b, A[N], la, lb, B[N];
ll solve(int x[], int len) {
ll res = 0; int s0 = 0, s1 = 1;
//s0表示0的个数,s1表示1的个数
FOL(i, len - 1, 1) FOR(j, 0, (i >> 1)) res += dp[i][j][1];//第1类数
FOL(i, len - 1, 1) {//第二类数
if(x[i]) FOR(j, 0, i) if(s0 + i - j >= s1 + j) res += dp[i][j][0];
//s0+i-j表首位至当前位0的个数,s1+j表首位至当前位1的个数,注意j要枚举至i
x[i] ? (++s1) : (++s0);
}
return res;
}
signed main() {
a = read(), b = read();
for(; a; a >>= 1) A[++la] = a & 1;
for(; b; b >>= 1) B[++lb] = b & 1;//转换成二进制
++B[1];
for(rei i = 2; i <= lb && B[i - 1] == 2; ++i) B[i - 1] = 0, ++B[i];
if(B[lb] == 2) B[lb] = 0, B[++lb] = 1;
while(!A[la]) --la;
while(!B[lb]) --lb;//给B加上1
dp[1][0][0] = dp[1][1][1] = 1;
FOR(i, 2, lb) FOR(j, 0, i) {//DP
if(j < i) dp[i][j][0] = dp[i - 1][j][1] + dp[i - 1][j][0];
if(j) dp[i][j][1] = dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1];
}
printf("%lld\n", solve(B, lb) - solve(A, la));
return 0;//结束
}
【题解】P6218 [USACO06NOV] Round Numbers S的更多相关文章
- 洛谷 P6218 [USACO06NOV] Round Numbers S
洛谷 P6218 [USACO06NOV] Round Numbers S 题目描述 如果一个正整数的二进制表示中,\(0\) 的数目不小于 \(1\) 的数目,那么它就被称为「圆数」. 例如,\(9 ...
- POJ 3252 Round Numbers 数学题解
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- 题解【POJ3252】Round Numbers
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- [BZOJ1662][POJ3252]Round Numbers
[POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...
- Round Numbers(组合数学)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10484 Accepted: 3831 Descri ...
- bzoj1662: [Usaco2006 Nov]Round Numbers 圆环数
Description 正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如谁先挤奶的顺序.她们甚至也不能通过仍硬币的方式. 所以她们通过"round number" ...
- BZOJ1662: [Usaco2006 Nov]Round Numbers
1662: [Usaco2006 Nov]Round Numbers Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 147 Solved: 84[Sub ...
- 【BZOJ】1662: [Usaco2006 Nov]Round Numbers 圆环数(数位dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1662 这道题折腾了我两天啊-!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 果然 ...
随机推荐
- 刷短视频看新闻看小说也能赚钱的几款APP
一.抖音极速版 发啦! 刷短视频也能赚钱 第1步:下载[抖音极速版] 第2步:填我邀请码[831008243] 第3步:立刻提现最高[38元]红包 二.今日头条极速版 1元现金速撸 下载[今日头条极速 ...
- HTML 页面跳转的五种方法
H方法TML 页面跳转的五种方法 下面列了五个例子来详细说明,这几个例子的主要功能是:在5秒后,自动跳转到同目录下的hello.html(根据自己需要自行修改)文件.1) html的实现 <he ...
- shell之路 shell核心语法【第三篇】运算
Bash 支持很多运算符,包括算数运算符.关系运算符.布尔运算符.字符串运算符和文件测试运算符. 原生bash不支持简单的数学运算,默认都是字符串操作,但是可以通过其他命令来实现 算数运算 expr. ...
- andorid jar/库源码解析之Bolts
目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...
- golang关键字select的三个例子, time.After模拟socket/心跳超时
golang关键字select的三个例子, time.After模拟socket/心跳超时 例子1 select会随机选择一个可执行的case // 这个例子主要说明select是随机选择一个 ...
- LeetCode--Sort Array By Parity && N-Repeated Element in Size 2N Array (Easy)
905. Sort Array By Parity (Easy)# Given an array A of non-negative integers, return an array consist ...
- hadoop文件系统常用操作
详细可参考hadoop官方文档filesystem shell一节 使用hadoop离不开文件系统,比如hdfs,我们可能需要从hdfs中读取文件作为输入,并将输出保存到hdfs上某个文件中 首先创建 ...
- 【Poj-3693】Maximum repetition substring 后缀数组 连续重复子串
POJ - 3693 题意 SPOJ - REPEATS的进阶版,在这题的基础上输出字典序最小的重复字串. 思路 跟上题一样,先求出最长的重复次数,在求的过程中顺便纪录最多次数可能的长度. 因为sa数 ...
- Mysql 常用语句实战(3)
前置 sql 语句 用来创建表.插入数据 ; DROP TABLE IF EXISTS dept_;-- 部门表 DROP TABLE IF EXISTS emp_;-- 部门表 ; SELECT @ ...
- 使用Python创建一个系统监控程序--李渣渣(lizaza.cn)
最近在做个人网站,但是由于服务器资源不足,偶尔会出现系统崩溃的现象,所以想写一个程序来实时监控系统状态.当系统资源占用过高时发送邮件提醒. psutil(进程和系统实用程序)是一个跨平台的库,用于检索 ...