AT_abc363_d 题解
题目传送门
思路
本题要求的是第
| 位数 | 回文数个数 |
|---|---|
总结一下,当位数为
我们可以用上述规律逐步逼近我们的答案,最后将回文数拼接起来,就是我们最终的答案。
注意事项
-
如果该数的位数是奇数,那么就只需要计算左半边。
-
long long也见祖宗了,需要开__int128。
AC CODE
#include<bits/stdc++.h>
using namespace std;
__int128 read(){__int128 x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
void write(__int128 x){if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');return;}
int main(){
__int128 n=read()-2,sum=0,p1=9,wei=0;
while(true){
if(wei>0&&wei%2==0)
p1*=10;
++wei;
if(sum+p1>n)
break;
sum+=p1;
}
n-=sum;
__int128 p2=1;
for(int i=1;i<=(wei-1)/2;++i)
p2*=10;
__int128 res=n+p2,temp=res;
if(wei&1)
temp/=10;
while(temp)
res=res*10+temp%10,temp/=10;
write(res);
return 0;
}