首页 技术 正文
技术 2022年11月15日
0 收藏 900 点赞 3,925 浏览 1266 个字

http://acm.hdu.edu.cn/showproblem.php?pid=5898

题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数。(1<=L<=R<= 9*10^18)

思路:在比赛的时候只大概记得是怎么写的,但是就是不会写,虽然写过好几道可还是写不出来。回去后重新写又错了几次,主要前导零的情况没有考虑清楚。存的时候存长度和之前一位是偶数还是奇数和是否有前导零。然后根据条件判断才写了出来。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f long long dp[][][][];
int bit[];
//pos记录第几位,len记录目前的段的长度,pre记录之前一个位是奇偶,zero判断前导零
long long dfs(int pos, int pre, int len, int zero, bool f)
{
if(pos <= ) return (pre & ) != (len & );
if(f && dp[pos][len][pre][zero] != -) return dp[pos][len][pre][zero];
int d = f ? : bit[pos];
long long ans = ;
for(int i = ; i <= d; i++) {
if(zero == ) { //如果前面都是0,记得特殊考虑这种情况
if(i == ) ans += dfs(pos - , , , , f || i < d);
else ans += dfs(pos - , i & , , , f || i < d);
} else {
if(i & ) {
if(pre & ) ans += dfs(pos - , i & , len + , , f || i < d);
else {
if(len & ) ans += dfs(pos - , i & , , , f || i < d);
}
} else {
if(pre & ) {
if(!(len & )) ans += dfs(pos - , i & , , , f || i < d);
} else {
ans += dfs(pos - , i & , len + , , f || i < d);
}
}
}
}
if(f) dp[pos][len][pre][zero] = ans;
return ans;
} long long solve(long long x)
{
int len = ;
while(x) {
bit[++len] = x % ;
x /= ;
}
return dfs(len, , , , );
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
memset(dp, -, sizeof(dp));
long long l, r;
scanf("%I64d%I64d", &l, &r);
printf("Case #%d: %I64d\n", cas, solve(r) - solve(l - ));
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902