问题

image.png

напиши на c++

思考

Validating with test inputs and considering performance

The algorithm works for lengths up to 6, and the logic should hold for larger inputs too. However, we should consider if the 't' parameter affects test constraints. Still, the general algorithm should pass all tests unless memory or time limits are exceeded, even with larger sizes.

Preparing C++ code solution

It looks like the task includes sample test details, but they’re not directly relevant to our approach. I’ll focus on implementing the algorithm in C++, keeping it short and to the point. The explanation will be minimal, mostly just the C++ code, since that’s what was requested.

Handling large numbers and implementing edge cases

I need to process numbers up to length 1e5, so converting to integers isn’t an option. Instead, I’ll treat the number as a string. I'll calculate the alternating sum modulo 11 using integers, making sure to keep it manageable by performing modulo at each step. I'll also handle edge cases appropriately like when the length is 1.

Managing alternating sum and modulo adjustments

For each digit, I’ll count occurrences in either the odd or even slots. Then, I’ll check combinations of values, adjusting for modulo 11 and ensuring consistency is maintained throughout. We could compute the entire alternating sum upfront as a long long, but using modulo operations keeps things simpler and manageable.

Setting up environment and optimizing code

For the edge-case, I’ll treat digits properly, but avoid assuming too much. To ensure portability, I'll include essential libraries like <bits/stdc++.h> for quick compilation but stick to standard, generally accepted ones when possible. Also, I’ll optimize with faster I/O settings for better performance, especially when handling multiple test cases and large inputs. I'll also take care of spacing and output format, making sure to handle newlines appropriately.

Thought for 2m 17s

回答

cpp
#include <bits/stdc++.h> using namespace std; // Обмен цифр. Проверяем, можно ли одной перестановкой двух цифр // сделать число кратным 11. int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; // номер теста (не используется в решении) if (!(cin >> t)) return 0; int k; cin >> k; while (k--) { string s; cin >> s; int n = (int)s.size(); // Одной цифры недостаточно, чтобы что-то менять if (n == 1) { cout << "no\n"; continue; } // X = сумма цифр на нечетных позициях - на четных (1-based) int X = 0; for (int i = 0; i < n; ++i) { int d = s[i] - '0'; if (i % 2 == 0) X += d; // позиция i+1 нечетная else X -= d; // позиция i+1 четная } X %= 11; if (X < 0) X += 11; // Если число уже кратно 11 и длина >= 2, всегда можно // переставить какую-то пару цифр (в т.ч. одинаковых). if (X == 0) { cout << "yes\n"; continue; } // Нужно менять цифры разных четностей позиций. int cntOdd[10] = {0}; // цифры на нечетных позициях int cntEven[10] = {0}; // цифры на четных позициях for (int i = 0; i < n; ++i) { int d = s[i] - '0'; if (i % 2 == 0) cntOdd[d]++; else cntEven[d]++; } bool ok = false; // Подбираем цифры d1 (нечетная позиция) и d2 (четная), // такие что после обмена X + 2*(d2 - d1) ≡ 0 (mod 11). for (int d1 = 1; d1 <= 9 && !ok; ++d1) if (cntOdd[d1]) { for (int d2 = 1; d2 <= 9; ++d2) if (cntEven[d2]) { int val = X + 2 * (d2 - d1); val %= 11; if (val < 0) val += 11; if (val == 0) { ok = true; break; } } } cout << (ok ? "yes\n" : "no\n"); } return 0; }

分享这个问答