C++の練習を兼ねて, AtCoder Regular Contest 127 の 問題A (Leading 1s) ~ 問題B (Ternary Strings) を解いてみた.
■感想.
1. 問題A, B ともに, 方針が見えなかったので, 解説を参考に実装したところ AC版に到達できた.
2. 個人的には, 両問とも, 不思議な印象を持つ問題に見え, 非常に面白く感じた.
3. 引き続き, 時間を見つけて, 過去問の学習を進めていきたいと思う.
本家のサイト AtCoder Regular Contest 127 解説 の 各リンク を ご覧下さい.
■C++版プログラム(問題A/AC版).
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 |
// 解き直し. // https://atcoder.jp/contests/arc127/editorial/2686 // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using LL = long long; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) #define pob pop_back #define pub push_back int main(){ // 1. 入力情報. LL N; scanf("%lld", &N); // 2. 集計(解説通り). LL ans = 0; repx(i, 1, 16){ // 範囲を作成. // ex. // 先頭が, 11111 の 場合. // [11111, 11112) // [111110, 111120) // [1111100, 1111200) // [11111000, 11112000) // ..... string s(i, '1'); string g = s; g.pob(); g.pub('2'); rep(j, 17 - i){ // '0' を 追加. string cz(j, '0'); string cs = s + cz; string cg = g + cz; // 数値化. LL ns = stoll(cs); LL ng = stoll(cg); // N以下の部分を集計. ans += max(0LL, min(N - ns + 1, ng - ns)); } } // 3. 出力. printf("%lld\n", ans); return 0; } |
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 |
[入力例] 11 [出力例] 4 ※AtCoderのテストケースより [入力例] 120 [出力例] 44 ※AtCoderのテストケースより [入力例] 987654321 [出力例] 123456789 ※AtCoderのテストケースより [入力例] 1010 [出力例] 134 [入力例] 202109252000 [出力例] 123456790122 [入力例] 101010101010101 [出力例] 13355780022446 |
■C++版プログラム(問題B/AC版).
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 |
// 解き直し. // https://atcoder.jp/contests/arc127/editorial/2687 // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using vs = vector<string>; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) #define pb push_back #define all(x) x.begin(), x.end() int main(){ // 1. 入力情報. int N, L; scanf("%d %d", &N, &L); // 2. 基準とする三進数(解説通り). vs v1; int b = 2, idx = 0; while(++idx < L) b *= 3; rep(i, N){ // 対象とする整数. int cur = b + i; // 三進数作成. string s = ""; while(cur){ int r = cur % 3; s.pb(r + '0'); cur /= 3; } // 反転. reverse(all(s)); // 保存. v1.pb(s); } // 3. 三進数(別パターン). // '0' -> '1' // '1' -> '2' // '2' -> '0' vs v2; for(auto &p : v1){ // 変換. string q = ""; rep(i, L){ if(p[i] == '0') q.pb('1'); if(p[i] == '1') q.pb('2'); if(p[i] == '2') q.pb('0'); } // 保存. v2.pb(q); } // 4. 三進数(別パターン). // '0' -> '2' // '1' -> '0' // '2' -> '1' vs v3; for(auto &p : v1){ // 変換. string q = ""; rep(i, L){ if(p[i] == '0') q.pb('2'); if(p[i] == '1') q.pb('0'); if(p[i] == '2') q.pb('1'); } // 保存. v3.pb(q); } // 5. sort(不要に見えるが念のため). vs ans; for(auto &p : v1) ans.pb(p); for(auto &p : v2) ans.pb(p); for(auto &p : v3) ans.pb(p); sort(all(ans)); // 6. 出力. for(auto &p : ans) printf("%s\n", p.c_str()); return 0; } |
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 |
[入力例] 2 2 [出力例] 00 02 11 12 20 21 ※AtCoderのテストケースより ※ 但し, 上記のプログラムでは, 以下の内容が出力される. 01 02 10 12 20 21 [入力例] 3 2 [出力例] 00 01 02 10 11 12 20 21 22 [入力例] 4 3 [出力例] 010 011 012 021 102 120 121 122 200 201 202 210 [入力例] 5 3 [出力例] 010 011 012 021 022 100 102 120 121 122 200 201 202 210 211 [入力例] 6 5 [出力例] 01110 01111 01112 01120 01121 01122 12200 12201 12202 12220 12221 12222 20000 20001 20002 20010 20011 20012 |
■参照サイト
AtCoder Regular Contest 127