1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <cstdio> #include <stack> using namespace std; typedef long long LL; const int N = 15; LL f[N], p[N], num[N], ans[N]; void init() { p[0] = 1; for (int i = 1; i < N; i++) { f[i] = f[i - 1] * 10 + p[i - 1]; p[i] = p[i - 1] * 10; } } void solve(int x) { int tmp = x; int idx = 0; while (tmp) { num[++idx] = tmp % 10; tmp /= 10; } for (int i = idx; i; i--) { for (int j = 0; j < 10; j++) ans[j] += f[i - 1] * num[i]; for (int j = 0; j < num[i]; j++) ans[j] += p[i - 1]; x -= p[i - 1] * num[i]; ans[num[i]] += x + 1; ans[0] -= p[i - 1]; } } int main() { init(); int n; scanf("%d", &n); solve(n); LL res = 0; for (int i = 0; i < 10; i++) res += i * ans[i]; printf("%lld", res); return 0; }
|