| Run ID | 作者 | 问题 | 语言 | 测评结果 | 时间 | 内存 | 代码长度 | 提交时间 |
|---|---|---|---|---|---|---|---|---|
| 91815 | sh25_shenpy | 矩阵翻硬币 | C++ | 运行超时 | 1000 MS | 252 KB | 2951 | 2026-06-17 06:43:22 |
#include <iostream> #include <string> #include <algorithm> using namespace std; // 比较大数字符串 a 和 b:a < b 返回 true bool less_str(const string &a, const string &b) { if (a.size() != b.size()) return a.size() < b.size(); return a < b; } // 大数乘法:a * b,返回字符串 string mul(const string &a, const string &b) { int n = a.size(), m = b.size(); string res(n + m, '0'); for (int i = n-1; i >= 0; --i) { int carry = 0; int da = a[i] - '0'; for (int j = m-1; j >= 0; --j) { int db = b[j] - '0'; int sum = (res[i+j+1]-'0') + da*db + carry; res[i+j+1] = sum % 10 + '0'; carry = sum / 10; } res[i] += carry; } size_t start = res.find_first_not_of('0'); if (start == string::npos) return "0"; return res.substr(start); } // 下面是辅助函数:大数加法、减法、除以2 string add(const string &a, const string &b) { string res; int i = a.size()-1, j = b.size()-1, carry = 0; while (i >= 0 || j >= 0 || carry) { int da = (i >= 0) ? a[i--]-'0' : 0; int db = (j >= 0) ? b[j--]-'0' : 0; int s = da + db + carry; res.push_back(s%10 + '0'); carry = s / 10; } reverse(res.begin(), res.end()); return res; } string sub(const string &a, const string &b) { // a >= b string res; int i = a.size()-1, j = b.size()-1, borrow = 0; while (i >= 0) { int da = a[i--]-'0' - borrow; int db = (j >= 0) ? b[j--]-'0' : 0; borrow = 0; if (da < db) { da += 10; borrow = 1; } res.push_back(da - db + '0'); } reverse(res.begin(), res.end()); size_t pos = res.find_first_not_of('0'); if (pos == string::npos) return "0"; return res.substr(pos); } string div2(const string &a) { string res; int rem = 0; for (char c : a) { int cur = rem * 10 + (c - '0'); res.push_back(cur / 2 + '0'); rem = cur % 2; } size_t pos = res.find_first_not_of('0'); if (pos == string::npos) return "0"; return res.substr(pos); } string add(const string &a, int k) { return add(a, to_string(k)); } // 二分求 floor(sqrt(x_str)) string big_sqrt(string x) { string l = "1", r = x, ans = "0"; string two = "2"; while (!less_str(r, l)) { // mid = (l + r) / 2 string sum = add(l, r); string mid = div2(sum); string sq = mul(mid, mid); if (!less_str(x, sq)) { ans = mid; l = add(mid, "1"); } else { r = add(mid, "-1"); } } return ans; } int main() { string n, m; cin >> n >> m; string s1 = big_sqrt(n); string s2 = big_sqrt(m); cout << mul(s1, s2) << endl; return 0; }