题目大意
rev(n)为n翻转后的十进制数(从第一个非0位往后翻转)。
有多少n满足rev(n)=n+d。
做法
如果d末尾有0,显然那几位可以任意填(只要不造成前导0),先剔除掉。
然后就可以暴力枚举一半的数字并判断。
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
int a[20],b[20],ten[9];
int d;
int i,j,k,l,t,n,m,tot,top;
ll num,sum,wdc,pp,ans;
bool czy;
int main(){
ten[0]=1;
fo(i,1,8) ten[i]=ten[i-1]*10;
scanf("%d",&d);
pp=1;
if (d%10==0){
czy=1;
while (d%10==0){
d/=10;
pp*=10;
}
pp=pp/10*9;
}
fo(l,1,7)
fo(i,0,ten[l]-1){
t=0;
k=i;
top=0;
fo(j,1,l){
a[++top]=k%10;
k/=10;
}
k=i+d;
tot=0;
fo(j,1,top){
b[++tot]=k%10;
k/=10;
}
if (!czy&&b[1]==0) continue;
reverse(b+1,b+tot+1);
fo(j,1,tot) a[++top]=b[j];
num=0;
wdc=1;
fo(j,1,top){
num+=(ll)a[j]*wdc;
wdc*=10;
}
sum=0;
wdc=1;
reverse(a+1,a+top+1);
fo(j,1,top){
sum+=(ll)a[j]*wdc;
wdc*=10;
}
if (num+(ll)d==sum){
ans++;
}
//continue;
reverse(a+1,a+top+1);
top-=tot;
a[++top]=0;
fo(j,1,tot) a[++top]=b[j];
num=0;
wdc=1;
fo(j,1,top){
num+=(ll)a[j]*wdc;
wdc*=10;
}
sum=0;
wdc=1;
reverse(a+1,a+top+1);
fo(j,1,top){
sum+=(ll)a[j]*wdc;
wdc*=10;
}
if (num+(ll)d==sum){
ans+=10;
}
}
ans*=pp;
printf("%lld\n",ans);
}