200 条题解
-
0
我们可以发现,a 和 b 的数据范围特别大,居然超过了
long long!我们只好拿出秘密武器:高精度。我们可以发现 (a+b)2=a2+b2+2ab(完全平方公式)。所以可以使用**高精度乘法算出来 a2,b2,2ab,再使用高精度开根算出绝对值 a+b**。这时候我们就需要分类讨论。若 a 为正数且 b 为正数,则 a+b 为正数。若 a 为负数且 b 为负数,则 a+b 为负数。若 a 与 b 一正一负,则要比较 a 与 b 的绝对值,用到高精度比较。来人,把朕的代码端上来!!!
#include <bits/stdc++.h> using namespace std; string qf (string a) { if (a[0] == '-') return a.substr (1 , a.size () - 1); return "-" + a; } string operator - (string a) {return qf (a);} string gjc (string a , string b) { if (a[0] == '-' && b[0] == '-') return gjc (- a , - b); else if (a[0] == '-') return - gjc (- a , b); else if (b[0] == '-') return - gjc (a , - b); int ans[a.size () + b.size ()] = {}; reverse (a.begin () , a.end ()); reverse (b.begin () , b.end ()); for (int i = 0;i < a.size ();i ++) for (int j = 0;j < b.size ();j ++) ans[i + j] += (a[i] - '0') * (b[j] - '0'); for (int i = 0;i < a.size () + b.size ();i ++) if (ans[i] > 9) { int x = ans[i] % 10 , y = ans[i] / 10; ans[i] = x; ans[i + 1] += y; } string ans2 = ""; for (int i = 1;i <= a.size () + b.size ();i ++) ans2 += (char) (ans[i - 1] + '0'); if (ans2[ans2.size () - 1] == '0') ans2 = ans2.substr (0 , ans2.size () - 1); reverse (ans2.begin () , ans2.end ()); return ans2; } string max (string a , string b) { if (a[0] == '-' && b[0] == '-') { if (- max (- a , - b) == a) return b; return a; } else if (a[0] == '-') return b; else if (b[0] == '-') return a; else if (a.size () > b.size ()) return a; else if (a.size () < b.size ()) return b; else { for (int i = 0;i < a.size ();i ++) if (a[i] > b[i]) return a; else if (a[i] < b[i]) return b; return a; } } string min (string a , string b) { if (max (a , b) == a) return b; return a; } string gjj (string a , string b); string cut (string a , string b) { if (a[0] == '-' && b[0] == '-') return cut (- b , - a); else if (a[0] == '-') return - gjj (- a , b); else if (b[0] == '-') return gjj (b , - a); else if (max (a , b) != a) return - cut (b , a); reverse (a.begin () , a.end ()); reverse (b.begin () , b.end ()); string ans = ""; for (int i = 1;i <= a.size () + 1;i ++) ans += "0"; for (int i = 0;i < b.size ();i ++) ans[i] += a[i] - b[i]; for (int i = b.size ();i < a.size ();i ++) ans[i] = a[i]; for (int i = 0;i < ans.size ();i ++) if (ans[i] < '0') { ans[i] += 10; ans[i + 1] -= 1; } while (ans.size () > 1 && ans[ans.size () - 1] == '0') ans = ans.substr (0 , ans.size () - 1); reverse (ans.begin () , ans.end ()); return ans; } string gjj (string a , string b) { if (a[0] == '-' && b[0] == '-') return - gjc (- a , - b); else if (a[0] == '-') return cut (b , - a); else if (b[0] == '-') return cut (a , - b); if (a.size () < b.size ()) swap (a , b); reverse (a.begin () , a.end ()); reverse (b.begin () , b.end ()); string ans = ""; for (int i = 1;i <= a.size () + 1;i ++) ans += "0"; for (int i = 0;i < b.size ();i ++) ans[i] += a[i] - '0' + b[i] - '0'; for (int i = b.size ();i < a.size ();i ++) ans[i] = a[i]; for (int i = 0;i < ans.size ();i ++) if (ans[i] > '9') { ans[i] -= 10; ans[i + 1] += 1; } if (ans[ans.size () - 1] == '0') ans = ans.substr (0 , ans.size () - 1); reverse (ans.begin () , ans.end ()); return ans; } string dev (string b , int a) { if (a < 0 && b[0] == '-') return dev (- b , - a); else if (a < 0) return - dev (b , - a); else if (b[0] == '-') return - dev (- b , a); while (a % 10 == 0 && b[b.size () - 1] == '0') { a /= 10; b = b.substr (0 , b.size () - 1); } string ans = ""; int yu = 0; for (int i = 0;i < b.size ();i ++) { yu = yu * 10 + b[i] - '0'; ans += yu / a + '0'; yu %= a; } while (ans.size () > 1 && ans[0] == '0') ans = ans.substr (1); return ans; } string operator / (string a , int b) {return dev (a , b);} string operator * (string a , string b) {return gjc (a , b);} string operator + (string a , string b) {return gjj (a , b);} string operator - (string a , string b) {return cut (a , b);} string sqrt (string s) { if (s == "0") return "0"; if (s == "1") return "1"; string l = "1" , r = "1" , ans , dan = "1"; for (int i = 1;i < s.size () / 2;i ++) l += "0"; for (int i = 1;i <= s.size () / 2 + 1;i ++) r += "0"; while (max (l , r) == r) { string mid = (l + r) / 2; if (max (mid * mid , s) == s) { ans = mid; l = gjj (mid , dan); } else r = mid - dan; } return ans; } signed main () { string a , b , c = "2"; cin >> a >> b; if (a[0] == '-' && b[0] == '-') cout << - sqrt (a * a + b * b + c * a * b); else if (a[0] == '-') { if (max (- a , b) == b) cout << sqrt (a * a + b * b + c * a * b); else cout << - sqrt (a * a + b * b + c * a * b); } else if (b[0] == '-') { if (max (- b , a) == a) cout << sqrt (a * a + b * b + c * a * b); else cout << - sqrt (a * a + b * b + c * a * b); } else cout << sqrt (a * a + b * b + c * a * b); }哔~~

信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 12363
- 已通过
- 5577
- 上传者