luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers
luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers
简述题意:
定义回文串为正着读反着读都一样的数字串,如果一个数字串的一个长度大于 \(1\) 的子串也为回文串的话,那么我们也定义这个数字串为回文串。
所以不是回文串的数字为非回文串,求区间 \([l, r]\) 内有多少个非回文串 ,数据范围: \(0 \le l \le r \le 10^{18}\)
Solution:
设 \(f[i][j][k]\) 表示长度为 \(i\) 最高位为 \(j\) 次高位为 \(k\) 的非回文串的个数
因为只要满足子串是回文数它就是回文串,所以判断比较的时候只需要与前两位数比较就好啦
转移方程:
\]
实际意义:表示区间 \([jk000\cdots,jk999\cdots]\) 的非回文数的和
对于求 \(ans_{l, r}\) 可以转化为 \(ans_{1, r} - ans_{1, l - 1}\)
求 \(ans_{l,r}\) 时的策略:
设 \(len\) 为 \(r\) 的位数,\(a[len]\) 中存 \(r\) 的每一位
1、对于所有长度小于 \(len\) 的 \(f\) , \(ans += \sum_{1 \le j \le 9}^{2 \le i \le len - 1} f[i][j][k](0 \le k \le 9)\)
2、对于长度小于等于 \(len\) 位且最高位小于 \(a[i]\) 的 \(f\),\(ans += f[i][j][k] (0 \le k \le 9)\) ,加的过程中注意判断此时是否是非回文串
3、因为 \(2\) 枚举不到最后的个位数,所以要在最后单独判断一遍
本人在理解上的一个很傻逼的误区:
主函数最后的那个 \(for\) 循环是判断 \(l\) 是否是非回文串,因为前面已经求了 \(ans_{1, r}\) 和 \(ans_{1, l}\) ,两者相减所求区间是 \(ans_{l + 1, r}\) 并没有取到 \(l\) ,按理说执行 \(solve(l - 1)\) 是可以解决的,但因为这里用的字符数组读入,所以难免有锅(我为这理解了一下午)
Code
/*
Work by: Suzt_ilymics
Knowledge: ??
Time: O(??)
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#define LL long long
using namespace std;
const int MAXN = 1010;
LL read(){
LL s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + ch - '0', ch = getchar();
return s * w;
}
char l[MAXN], r[MAXN];
LL f[MAXN][13][13];
LL a[MAXN];
void init(){
for(int i = 2; i <= 1001; ++i){
for(int j = 0; j <= 9; ++j){
for(int k = 0; k <= 9; ++k){
if(j == k) continue;//如果相邻两个元素一样,那么一定是回文串,直接跳过
for(int a = 0; a <= 9; ++a){
if(j != a && a != k)//如果不是2回文也不是3回文
f[i][j][k] += f[i - 1][k][a];
}
if(i == 2) f[i][j][k]++;//如果长度为2,一定不是
}
}
}
}
LL solve(char s[]){
memset(a, 0, sizeof(a));
LL tot = 0;
bool t = 1;
int len = strlen(s);
LL ans = 0, last1 = -1, last2 = -1, sum = 0;
for(int i = len; i >= 1; --i){
a[i] = s[len - i] - '0';//把个位放前面
sum = (sum << 1) + (sum << 3) + a[i];//
}
if(len == 1) return sum + 1;//如果长度为1,那么不用判断了
ans += 10;//算上长度为1的那10个数
for(int i = 2; i < len; ++i){//老套路把满着的先加上
for(int j = 1; j <= 9; ++j){
for(int k = 0; k <= 9; ++k){
ans = ans + f[i][j][k];
}
}
}
for(int i = len; i >= 2; i--){
for(int j = 0; j < a[i]; ++j){
if(i == len && j == 0) continue;//首位是0就跳过
for(int k = 0; k <= 9; ++k){
if(last1 != j && last2 != j && j != k && last1 != k){
ans = ans + f[i][j][k];
}
}
}
if(last1 == a[i] || last2 == a[i]) {//上一位和上两位
t = 0; break;
}
last2 = last1, last1 = a[i];//更新
}
if(t) {//如果没有中途退出
for(int i = 0; i <= a[1]; ++i){//最后一位剩下的那一点
if(i != last1 && i != last2) ans++;
}
}
return ans;
}
int main()
{
init();
cin >> l >> r;
LL ans = solve(r) - solve(l);
int t = strlen(l), flag = 0;
for(int i = 1; i < t; ++i){
if(l[i] == l[i - 1] || (l[i] == l[i - 2] && i > 1)) {
flag = 1;
break;
}
}
if(!flag) ans++;
printf("%lld", ans);
return 0;
}
luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers的更多相关文章
- P6753 [BalticOI 2013 Day1] Ball Machine
P6753 [BalticOI 2013 Day1] Ball Machine 题意 给你一个树,每次从根节点放一个求,如果其子节点有空这个球会向下滚,若有多个节点为空则找儿子中以子树内编号的最小值为 ...
- LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On
题目描述 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会.有 N×M 个这样 ...
- P6739 [BalticOI 2014 Day1] Three Friends 题解
目录 写在前面 Solution 何为字符串哈希(可跳过): Code 写在前面 P6739 [BalticOI 2014 Day1] Three Friends 听说这题可以用比较暴力的做法过,比如 ...
- NOIP 2013 day1
tags: 模拟 快速幂 逆序对 树状数组 归并排序 最小生成树 lca 倍增 categories: 信息学竞赛 总结 tex live 2017.iso 转圈游戏 火柴排队 货车运输 转圈游戏 s ...
- P4675 [BalticOI 2016 day1]Park (并查集)
题面 在 Byteland 的首都,有一个以围墙包裹的矩形公园,其中以圆形表示游客和树. 公园里有四个入口,分别在四个角落( 1 , 2 , 3 , 4 1, 2, 3, 4 1,2,3,4 分别对应 ...
- Exchange Server and Update Rollup Build Numbers
原文链接https://social.technet.microsoft.com/wiki/contents/articles/240.exchange-server-and-update-rollu ...
- Exchange Server 产品路线图 及 补丁下载
Exchange Server RU listExchange Server and Update Rollup Build Numbers -TechNet Articles -United Sta ...
- Exchange Version and UpdateRollups
Exchange Server 2010 Product name Build number Date KB Microsoft Exchange Server 2010 RTM 14.0.639.2 ...
- LOJ 一本通一句话题解系列:
第一部分 基础算法 第 1 章 贪心算法 1):「一本通 1.1 例 1」活动安排:按照结束时间排序,然后扫一遍就可以了. 2):「一本通 1.1 例 2」种树:首先要尽量的往区间重叠的部分种树,先按 ...
随机推荐
- LeetCode数组移除数组中目标元素等题目
一种自己解题,一种高赞解题 /** * 移除数组中目标元素,返回新数组长度 * @param nums * @param val * @return */ public int removeEleme ...
- svn怎么上传文件
首先去网站下载TortoiseSVN,并安装 安装完后随便打开一个文件夹,如图,笔者在 E:\svn\ 文件下创建了一个simbo文件夹,选中并右键,出现了TortoiseSVN应用的选项,我们点 ...
- ROS代码经验系列-- tf进行位置查询变换
include文件: #include "tf/transform_broadcaster.h" #include "tf/transform_listener.h&qu ...
- webservcie学习之webservice平台技术与开发
webservice平台技术有哪些 XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术. 1.XML+XSD WebService采用HTTP协议传输数据,采用XML格式封装 ...
- docker获取Let's Encrypt永久免费SSL证书
一 起因 官方的cerbot太烦了,不建议使用 还不如野蛮生长的acme.sh,而这里介绍docker运行cerbot获取Let's Encrypt永久免费SSL证书 二 选型 cerbot的证书不会 ...
- Ubuntu20.04编译ffmpeg
1.安装编译所需工具,GCC 2.安装yasm nasm yasm和nasm是两个编译器,编译ffmpeg需要用到 安装命令: sudo aptitude install yasm nasm 3.安装 ...
- 死磕以太坊源码分析之MPT树-下
死磕以太坊源码分析之MPT树-下 文章以及资料请查看:https://github.com/blockchainGuide/ 上篇主要介绍了以太坊中的MPT树的原理,这篇主要会对MPT树涉及的源码进行 ...
- 【原创】Linux PCI驱动框架分析(三)
背 景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本 ...
- ABP vNext 审计日志获取真实客户端IP
背景 在使用ABP vNext时,当需要记录审计日志时,我们按照https://docs.abp.io/zh-Hans/abp/latest/Audit-Logging配置即可开箱即用,然而在实际生产 ...
- idea启动build过慢
原文链接http://zhhll.icu/2020/04/17/idea/idea%E4%B9%8B%E7%BC%96%E8%AF%91%E9%97%AE%E9%A2%98/ 之前使用idea的时候每 ...