File tree Expand file tree Collapse file tree 1 file changed +72
-0
lines changed
Expand file tree Collapse file tree 1 file changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ title : 【精進6】ABC387-E
3+ date : 2025-1-08 18:38:00 0900
4+ categories : [競技プログラミング]
5+ tags : [AtCoder,ABC,精進,構築]
6+ math : true
7+ ---
8+ 前回のABCのupsolve。答え見ちゃったけど思ったよりギャグ
9+
10+ ## 問題
11+ 自然数について、自身の桁和が自身を割り切るとき良い整数と呼ぶ。$N\le a<2N$かつ$a,a+1$がともに良い整数となるような$a$が存在するならば$1$つ求めよ。
12+
13+ ## 解法
14+ > $a$が$8$の倍数かつ桁和が$8$で、$a+1$の桁和が$9$であるような$N\le a<2N$は条件を満たす。
15+
16+ これだけ。小さい$N$については愚直に求める。$N$が大きいときは$a$の下$3$桁を$0$にすることで$8$の倍数であるという条件と、$a+1$の桁和が$9$であるという条件が満たされる。あとは上の方の数字を少し増やして桁和が$8$となるよう調整すれば良い。
17+ ``` cpp
18+ #include " template.hpp"
19+ ll keta (ll n) {
20+ ll res = 0;
21+ while(n > 0) {
22+ res += n % 10;
23+ n /= 10;
24+ }
25+ return res;
26+ }
27+ void naive(ll n) {
28+ ll original = n;
29+ while(1) {
30+ if(n + 1 > original * 2) {
31+ print(-1);
32+ return;
33+ }
34+ if(n % keta(n) == 0 and (n + 1) % keta(n + 1) == 0) {
35+ print(n);
36+ return;
37+ }
38+ n++;
39+ }
40+ }
41+ void solve() {
42+ STR(s);
43+ ll n = ssize(s);
44+ if(n <= 6) {
45+ naive(stoll(s));
46+ return;
47+ }
48+ ll x = 0;
49+ rep(i, 3) x = 10 * x + (s[ i] - '0');
50+ x++;
51+ while(1) {
52+ if(keta(x) == 8) {
53+ cout << x;
54+ rep(i, n - 3) cout << 0;
55+ print();
56+ return;
57+ }
58+ x++;
59+ }
60+ }
61+
62+ int main() {
63+ ios::sync_with_stdio(false);
64+ std::cin.tie(nullptr);
65+ cout << std::setprecision(16);
66+ int t = 1;
67+ rep(_ , t) {
68+ solve();
69+ }
70+ }
71+
72+ ```
You can’t perform that action at this time.
0 commit comments