-
Notifications
You must be signed in to change notification settings - Fork 0
/
digisum.cpp
164 lines (141 loc) · 3.15 KB
/
digisum.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <bits/stdc++.h>
#define MAXN 10000005
#define int long long
using namespace std;
int n;
int he;
int aswh;
int ans;
int uu[MAXN];
inline void init(int n) {
for (int i = 1; i <= n; i++) {
uu[i] = i;
}
}
inline int swh(int x) {
int tot = 0;
while (x) {
tot += x % 10;
x /= 10;
}
return tot;
}
inline int anssum(int n, int k) {
printf("%lld\n", k);
if (n < 10) return k;
int e = n;
int tot = 0;
int _p = 1;
while (e / 10) {
e /= 10;
tot++;
}
for (int i = 1; i <= tot; i++) e *= 10, _p *= 10;
e -= 1;
if (n - e == _p) e = n;
// printf("%lld",e);
int aaaswh = swh(e);
int ooo;
while (aaaswh > 0) {
ooo = swh(aaaswh);
while (ooo >= 10) ooo = swh(ooo);
if (ooo == k) return aaaswh;
aaaswh--;
}
return 1;
}
inline void solve2();
inline void out2(int l, int r);
int llans;
inline void solve(int n, int sss) {
if (n <= 3) return solve2();
int e = n;
int k = 0;
int o;
while (e) k++, e /= 10;
k--;
int mid = 0;
if (k != 0) {
o = sss % k ? sss / k + 1 : sss / k;
o = o >= 9 ? 8 : o;
int _p = 1;
for (int i = 1; i < k; i++) {
mid += o;
mid *= 10;
_p *= 10;
}
if (sss - (k - 1) * o >= 9)
mid += 8;
else
mid += sss - (k - 1) * o;
} else
mid = sss;
printf("%lld\n", mid);
out2(mid + 1, n);
out2(1, mid - 1);
// int e;
int aans;
if (mid > 1 && mid < n) {
printf("%lld + %lld -> %lld\n", uu[mid + 1], uu[1], e = swh(uu[mid + 1] + uu[1]));
printf("%lld + %lld -> %lld\n", e, mid, aans = swh(e + mid));
} else {
if (mid == 1)
e = uu[2];
else if (mid == n)
e = uu[1];
printf("%lld + %lld -> %lld\n", e, mid, aans = swh(e + mid));
}
if (aans != sss) {
printf("NO,ans should be : %lld\n\n", sss);
} else
printf("YES,ans : %lld\n\n", sss);
llans = sss;
}
inline void out2(int l, int r) {
int e;
for (int i = r - 1; i >= l; i--) {
e = swh(uu[i + 1] + uu[i]);
printf("%lld + %lld -> %lld\n", uu[i + 1], uu[i], e);
uu[i] = e;
}
}
inline void solve2() {
init(n);
out2(2, n);
int aans;
printf("%lld + %lld -> %lld\n", uu[2], uu[1], aans = swh(uu[2] + uu[1]));
if (aans != aswh) {
printf("NO,ans should be : %lld\n\n", aswh);
} else
printf("YES,ans : %lld\n\n", aswh);
if (n <= 3) return;
if (llans != ans) {
printf("NO,ans should be : %lld\n\n", ans);
} else
printf("YES,ans : %lld\n\n", ans);
}
#undef int
int main() {
#define int long long
scanf("%lld", &n);
if (n == 1) {
printf("1");
return 0;
}
// for(int i = 1;i <= n;i++)
// {
// uu[i] = i;
// }
init(n);
he = (n + 1) * n / 2;
aswh = swh(he);
// printf("%lld\n",aswh);
while (aswh >= 10) aswh = swh(aswh);
// printf("%lld\n",aswh);
ans = anssum(n, aswh);
// printf("%lld\n",ans);
solve(n, ans);
solve2();
return 0;
}
/*12351*/