跳到正文

题解

题解:P11680 [Algo Beat Contest 001 B] Between Head and Tail

SOLIVAULT约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月6日
篇幅
约 2 分钟
题目
P11680

版本

  1. v1当前

P11680 [Algo Beat Contest 001 B] Between Head and Tail 题解


“水题,切了”

题目传送门

一道很适合新手的数位拆分练习题。

首先考虑暴力,每次枚举[L,R]之间的每个数,判段是否为首中尾相等数,是就输出该数,可以通过此题。

但是作为一个OIer,我们肯定要装13优化一下。

可以提前预处理出 1 \ 1\ ~  105 \ 10^5\ 中的的首中尾相等数,每次询问时,遍历一下存首中尾相等数的数组,判断是否在[L,R]之间

ACcode:\color{lime}ACcode:

C++
#include<bits/stdc++.h>//万能头
using namespace std;
vector<int> bht;//存首中尾相等数的数组
int init(){//预处理,返回总首中尾相等数个数
	for(int i=1;i<=100005;i++){
		int n=0,s=1,t=i,end=i%10,mid,t2;//细节s=1
		while(t>=10) t/=10,s++;//为了方便求首位,t是个位数时就结束循环 ,现在的t就是首位 ,所以s=1
		mid=s>>1,t2=i;
		if(t!=end||s%2==0) continue;//如果首尾不同或是偶数位数就不计入bht数组
		while(n<mid){//获取中间位的数
			t2/=10;
			n++;
		}
		if(t2%10!=end) continue;//如果 中间位数与末位不同就不计入bht数组
		bht.push_back(i);//能跑到这里说明是首中尾相等数了,就放入bht数组
	}
	return bht.size();
}
int main(){
	int T,l,r,n=init();
	scanf("%d",&T);
	while(T--){//多测
		scanf("%d %d",&l,&r);
		for(int i=0;i<n;i++){//遍历bht数组
			if(bht[i]>r) break;//小优化,如果大于R说明超过区间范围,往后没有可能的数了
			if(bht[i]>=l) printf("%d ",bht[i]);//如果R>=此数>=L,说明目标合法,输出
		}
		printf("\n");//记得换行
	}
    return 0;
    //杜绝抄袭,人人有责
}

附赠备用方案:打表

原文