ARC075D Mirrored
作者:互联网
Description
设 \(r(x)\) 表示十进制下将 \(x\) 按位翻转并去掉前导 \(0\) 之后的数。
给定 \(d\),求满足 \(r(n) - n = d\) 的 \(n\) 的个数。
\(1 \le d < 10^9\)。
Solution
不难发现位数至多有 \(18\) 位,那么枚举位数、再枚举每一位有没有借位即可。
代码相较于其他题解特别好写。
注意判断无解情况。
#include <bits/stdc++.h>
using namespace std;
#define il inline
#define re register
#define rep(i, s, e) for (re int i = s; i <= e; ++i)
#define drep(i, s, e) for (re int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
using ll = long long;
const int N = 100 + 10;
il int read() {
int x = 0; bool f = true; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = false; c = getchar();}
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? x : -x;
}
int dlen, dig[N], dis[N];
ll d, ans;
ll solve(int len) {
int lim = 1 << len; ll res = 0;
rep(s, 0, lim - 1) {
rep(i, 0, len) dis[i] = dig[i];
bool flag = true;
rep(i, 0, len - 1) {
if ((s >> i) & 1) dis[i] -= 10, ++ dis[i + 1];
}
ll now = 1;
rep(i, 0, len >> 1) {
if (dis[i] + dis[len - i] != 0 || abs(dis[i]) == 10) { now = 0; break; }
now *= (10 - abs(dis[i]) - !i);
}
res += now;
}
return res;
}
int main() {
d = read();
if (d % 9) return puts("0") & 0;
rep(i, 0, 18) {
if (!d) { dlen = i; break; }
dig[i] = d % 10, d /= 10;
}
rep(i, max(2, dlen), 18) ans += solve(i - 1);
printf("%lld\n", ans);
return 0;
}
标签:10,int,rep,Mirrored,define,ARC075D,now,dis 来源: https://www.cnblogs.com/Scintilla/p/15254204.html