å°å ¥
ããã® long double
解æ³ã«ã¤ãã¦ã®è©±ã§ããä¸è¨ã®ãããªè§£æ³ã§ãã
#include <cmath> #include <cstdio> constexpr long double eps = /* ??? */; bool ok(long double a, long double b, long double c) { return std::sqrt(a) + std::sqrt(b) + eps < std::sqrt(c); } int main() { long double a, b, c; scanf("%Lf %Lf %Lf", &a, &b, &c); puts(ok(a, b, c) ? "Yes" : "No"); }
ãã ããlong double
ã®ä»®æ°é¨ã¯ 64-bit ã§ããã¨ãã¾ãï¼AtCoder ç°å¢ã«æºæ ï¼ã
å
¬å¼è§£èª¬ ã§ã¯ 1.0E-14
ã«è¨å®ãã
ãã㧠$\varepsilon = 10^{-14}$ ã®å¤ã大ãããããããå°ããããããããã㨠WA ã«ãªã£ã¦ãã¾ãã¾ãã ãªã $\varepsilon = 10^{-14}$ ã§ããã¨ãã¾ãããã®ãã¯èª¬æã§ããã®ã§ãããããç°¡åã§å®å ¨ãªæ¹æ³ã¯ãå ¨ã¦æ´æ°ã§ãã£ã¦ãã¾ããã¨ã§ãã
ã¨æ¸ããã¦ãã¾ãï¼1.0E-14
㯠double
ã®ãªãã©ã«ã§ãããlong double
ã® 1.0E-14L
ããè¥å¹²ç²¾åº¦ãå£ãã¾ãããããã§ã¯åé¡ã«ãªãã¾ããï¼ãå®éã
eps = 8.8817841970012523238e-16L;
ã§ã¯ WA ã«ãªãã
eps = 8.8817841970012523239e-16L;
ã§ã¯ AC ã«ãªãã¾ããã¾ãã
eps = 1.5099033134902128948e-14L;
ã§ã¯ AC ã«ãªãã
eps = 1.5099033134902128949e-14L;
ã§ã¯ WA ã«ãªãã¾ãã
ãã£ãã 4 æ¡ã§è¦ãã¨ã$8.882\times 10^{-16} \le \text{\texttt{eps}} \le 1.509\times 10^{-14}$ ã®ç¯å²ã§ããã° AC ã«ãªãã¾ãã
æ°å¤ã®ãã£ããã¨ããåºå¦ã¯ã$2^{\floor{\log_2(\sqrt{10^9})}-64} = 2^{-50}$ 㨠$$ \underbrace{(\ceil{\tfrac12(10^9)^{-3/2}\cdot 2^{(64-1)-\floor{\log_2(\sqrt{10^9})}}}-\tfrac12)}_{8.5} \times \underbrace{2^{\floor{\log_2(\sqrt{10^9})}-(64-1)}\vphantom{\tfrac12}}_{2^{-49}} $$ ã§ãï¼ãããã«çã«å«ã¾ããç¯å²ï¼ã
ãããã¡ããã¨èãã¦ããã¾ãããã¨ããã®ãä»åã®ã話ã§ãããªããå¾è¿°ãã¾ãããä¸è¨ã® eps = 1.5099033134902128948e-14L
ã«ãã解æ³ã¯ hack ãããã¨ãå¯è½ã§ãã
- å°å ¥
- æ¹é
- èå¯
- hack ã«ã¤ãã¦
- ã±ã¼ã¹ã«é¢ãã¦
- å·®ã®ä¸çã«ã¤ãã¦
- ã¾ã¨ã
- åè
- ææ
- ãã¾ã
- ããã
å®ç¾©ã»åæç¥è
$\gdef\sqrtf{\text{\texttt{sqrt}}}$ $\gdef\roundf{\operatorname{round}}$
æµ®åå°æ°ç¹æ°åã«ã¤ãã¦ã®åºæ¬çãªç¥èã¯ãããã®ã¨ãã¾ãï¼è¡¨ç¾ã®ä»æ¹ã丸ãã«ãã£ã¦èª¤å·®ãçãããã¨ãªã©ï¼ããªã人ã®ããã®è¨äºã¯ãã®ãã¡æ¸ãäºå®ã§ã¯ããã¾ãã
å®æ° $x$ ã«å¯¾ããï¼å¯¾è±¡ã¨ãã¦ããæµ®åå°æ°ç¹æ°åã§ï¼è¡¨ããæãè¿ãæ°å¤*1ã $\roundf(x)$ ããã³ $\hat x$ ã§è¡¨ãã¾ãï¼$x$ ã®é¨åãé·ãã¨ãã«ã¯åè ã®è¨æ³ã好ãã§ä½¿ãã主ã«ä¸æåã®å¤æ°ã®ã¨ãã«ã¯å¾è ã®è¨æ³ã好ãã§ä½¿ããã¨ã«ãã¾ãï¼ã æµ®åå°æ°ç¹æ°åã®æ°å¤ $x$, $y$ ã«å¯¾ãã¦ã$x\oplus y = \roundf(x+y)$ 㨠$\sqrtf(x) = \roundf(\sqrt{x})$ ã¨æ¸ããã¨ã«ãã¾ãã
åããã³å¹³æ¹æ ¹ã®è¨ç®ã§ã¯ãæ£ç¢ºã«è¨ç®ããå¤ãæ£ç¢ºã«ä¸¸ããå¤ãè¿ããã¨ãå®ãããã¦ããã®ã§ã誤差㯠0.5 ULP ã§ããããªãã¡ãæ£ç¢ºãªå¤ã $2^e$ ä»¥ä¸ $2^{e+1}$ æªæºã®ç¯å²ã«ããã¨ãã誤差ã®çµ¶å¯¾å¤ã¯ $2^{e-p}$ 以ä¸ã§ãã
ããã§ãAtCoder ç°å¢ã«ããã long double
ã«ããã¦ã¯ $p = 64$ ã§ããdouble
ãä¸é¨ç°å¢ã® long double
ã§ã¯ $p = 53$ãå¥ã®ç°å¢ã® long double
ã§ã¯ $p=113$ ã ã£ãããã¾ãã
ã¾ããæ£ã®å®æ° $x$, $y$, $x+y$ ãæ£è¦åæ°ã®ç¯å²ã«å ¥ãã¨ãã$\roundf(x+y)$ 㨠$(\hat x\oplus\hat y)$ ã®å·®ã®çµ¶å¯¾å¤ã¯ 1 ULP 以ä¸ã§ãããã¨ã示ãã¾ããããªãã¡ãã丸ããå¤å士ã足ãã¦ãã丸ããå¤ãã¨ãç¡é精度ã§è¶³ãããã®ãä¸åº¦ã ã丸ããå¤ãã®å·®ã 1 ULP ã§æããããã¨ãããã¨ã§ã*2ãç´æçã«ã¯ã$\roundf(x+y)=(\hat x\oplus\hat y)$ ã§ããããæµ®åå°æ°ç¹æ°åã§è¡¨ãããã¡ã§é£ãåã 2 æ°ã«ãªãã¾ãã
$\gdef\ulp{\operatorname{ulp}}$
å®æ° $x$ ã«å¯¾ãã$\ulp(x) = 2^{\floor{\log_2(|x|)}+1-p}$ ã¨ãã¾ãã$2^i\le x\lt2^{i+1}$ ã®ã¨ãã$\ulp(x) = 2^{i+1-p}$ ã¨ãªãã¾ãã ã¾ãã $$ \ulp(x)\cdot 2^{p-1}\le x\lt \ulp(x)\cdot2^p $$ ã $$ \ulp(x)\cdot 2^{p-1}\le \hat x\le \ulp(x)\cdot2^p $$ ãæãç«ã¡ã¾ãã
æ¹é
誤差ã®ãªãä¸çã§è¨ç®ããçµæã¨ã誤差ã®ããä¸çã§è¨ç®ããçµæã¨ã§ã大å°é¢ä¿ã常ã«åãã ã¨æ¥½ã§ãï¼èª¤å·®ãç¡è¦ãã¦è¨ç®ãã¦çããã°ããã®ã§ï¼ã ããããå®éã«ã¯æ¬¡ã®ãããªã±ã¼ã¹ãåå¨ãã¾ãã
$$ \sqrt{a} + \sqrt{b} = \sqrt{c} \wedge \sqrtf(a) \oplus \sqrtf(b) \lt \sqrtf(c). $$
ããã§ãé©å㪠$\varepsilon$ ã足ããã¨ã§ãä¸è¨ã®ã大å°é¢ä¿ã常ã«åãããæãç«ãããæ¹æ³ãç¥ããã¦ãã¾ãï¼è»æ¬ p. 228 ãªã©ï¼ã $$ \sqrt{a} + \sqrt{b} \lt \sqrt{c} \iff (\sqrtf(a) \oplus \sqrtf(b)) \oplus \varepsilon \lt \sqrtf(c). $$ ããã§ãå¹³æ¹æ ¹ãæ±ããéããããã®åãæ±ããéã ãã§ãªãã$\varepsilon$ ã足ããéã«ã丸ã誤差ãçãããã¨ã«æ³¨æãã¦ããã¾ãããã
é常ã«åããããã§ããããã® $\varepsilon$ ãã¾ã¨ãã«èãã¦è¨å®ãã¦ãã人ã¯ãä¸ç´è 以ä¸ã§ãé常ã«å°ãªãå°è±¡ãããã¾ã*3ã次ã®ãããªãã¨ãèããå¿ è¦ãããã§ãããã
ã¾ãã$\sqrt{a} + \sqrt{b} \ge \sqrt{c}$ ã®ã±ã¼ã¹ã«ããã¦ã$\sqrtf(a) \oplus \sqrtf(b)$ ã $\sqrtf(c)$ ãã©ã®ç¨åº¦ä¸åãããèããå¿ è¦ãããã¾ãã ã$\sqrt{a}+\sqrt{b}\ge\sqrt{c}$ ã®ã±ã¼ã¹ã§ No ã¨çããããã«ãã©ã®ç¨åº¦ $\varepsilon$ ã大ããããå¿ è¦ããããï¼ãã¨ãããã¨ã§ãã
ã¾ãã$\sqrt{a} + \sqrt{b} \lt \sqrt{c}$ ã®ã±ã¼ã¹ã«ããã¦ã$\sqrtf(a) \oplus \sqrtf(b)$ ã $\sqrtf(c)$ ã«ã©ã®ç¨åº¦è¿ã¥ãããèããå¿ è¦ãããã¾ãã ãã©ã®ç¨åº¦ãªã $\varepsilon$ ã大ãããã¦ãã$\sqrt{a}+\sqrt{b}\lt\sqrt{c}$ ã®ã±ã¼ã¹ã§ Yes ã¨çããã¾ã¾ã§ãããããï¼ãã¨ãããã¨ã§ãã
æªç¥ã® $\varepsilon_L$, $\varepsilon_U$ ã«å¯¾ã㦠$\varepsilon_L \le \varepsilon \le \varepsilon_U$ ã®ç¯å²ã§ AC ã§ããã¨ããã¨ãã«ã$\varepsilon_L$ ã®ä¸ç $\varepsilon_L'$ ãæ±ããã®ãåè ã§ã$\varepsilon_U$ ã®ä¸ç $\varepsilon_U'$ ãæ±ããã®ãå¾è ã§ãã$\varepsilon_L'\le\varepsilon_U'$ ã¨ãªãããã«æ±ããããã°ããã®ç¯å²ã® $\varepsilon$ ã使ããã¨ã§ååã¨ãªãã¾ãã
éã«ãããããåºé $[\varepsilon_L\ldots\varepsilon_U]$ ã空ã®å ´åã¯ãã®è§£æ³ã§ã¯æ£çã§ããªããã¨ã«ãªãã¾ãã
èå¯
ä¸è¬æ§ã失ããã$a\le b$ ã¨ãã¾ããã¾ããå¶ç´ã®ç¯å²ã«ãã㦠$a$, $b$, $c$ ã¯æ³¨ç®ãã¦ããæµ®åå°æ°ç¹æ°åã§æ£ç¢ºã«è¡¨ããã®ã§ããã®ãã¨ãä»®å®ãã¾ãã
$\varepsilon_L'$ ã®ä¸ç
$\sqrt{a}+\sqrt{b}\ge\sqrt{c}$ ã¨ãã$(\sqrtf(a)\oplus\sqrtf(b))-\sqrtf(c)$ ãä¸ããæãã¾ãã 丸ããä¸åº¦ããè¡ããªãå ´åã®å¤ã¨é£ãåã£ãçµæãå¾ããããã¨ããULP ã®æ§è³ªãªã©ã«æ³¨æãã¾ãã
ã¾ãã$\ulp(\sqrt{a}+\sqrt{b})\ge 4\ulp(\sqrt{c})$ ã®å ´åã $$ \ulp(\sqrt{a}+\sqrt{b})\cdot 2^{p-1}\le \sqrt{a}+\sqrt{b} $$ ãã $$ \begin{aligned} \sqrtf(a)\oplus\sqrtf(b) &\ge \ulp(\sqrt{a}+\sqrt{b})\cdot 2^{p-1}-\tfrac12\ulp(\sqrt{a}+\sqrt{b}) \\ &\gt \ulp(\sqrt{a}+\sqrt{b})\cdot 2^{p-2} \\ &= \ulp(\sqrt{c})\cdot 2^{p} \\ &\ge \sqrtf(c) \end{aligned} $$ ãªã®ã§ã$(\sqrtf(a)\oplus\sqrtf(b))-\sqrtf(c)\ge 0$ ã¨ãªãã¾ãã
次ã«ã$\ulp(\sqrt{a}+\sqrt{b})=2\ulp(\sqrt{c})$ ã®å ´åã $$ \begin{aligned} \sqrtf(a)\oplus\sqrtf(b) &\ge \ulp(\sqrt{a}+\sqrt{b})\cdot 2^{p-1}-\tfrac12\ulp(\sqrt{a}+\sqrt{b}) \\ &= \ulp(\sqrt{c})\cdot 2^p - \ulp(\sqrt{c}) \\ &\ge \sqrtf(c) - \ulp(\sqrt{c}) \end{aligned} $$ ãªã®ã§ã$(\sqrtf(a)\oplus\sqrtf(b))-\sqrtf(c)\ge -\ulp(\sqrt{c})$ ã¨ãªãã¾ãã
æå¾ã« $\ulp(\sqrt{a}+\sqrt{b})=\ulp(\sqrt{c})$ ã®å ´åã
$$ \begin{aligned} \sqrtf(a)\oplus\sqrtf(b) &\ge \roundf(\sqrt{a}+\sqrt{b}) - \ulp(\sqrt{a}+\sqrt{b}) \\ &\ge \roundf(\sqrt{c}) - \ulp(\sqrt{a}+\sqrt{b}) \\ &= \sqrtf(c) - \ulp(\sqrt{c}) \end{aligned} $$ ãªã®ã§ããã¡ãã $(\sqrtf(a)\oplus\sqrtf(b))-\sqrtf(c)\ge -\ulp(\sqrt{c})$ ã¨ãªãã¾ãã
ã¾ãã$\sqrtf(a)\oplus\sqrtf(b)\lt \sqrtf(c)$ ã®åæã§ã¯ $$\ulp(\sqrt{c})\cdot2^{p-1} \le \sqrtf(a)\oplus\sqrtf(b)\le \ulp(\sqrt{c})\cdot2^p$$ ã¨ãªãã¾ãï¼å»ã¿å¹ ã $\ulp(\sqrt{c})$ ã®åºéã«ããã¨ãããã¨ï¼ã
ãã£ã¦ãå·®ã $\ulp(\sqrt{c})$ 以ä¸ã§ãããã¨ããã$(1-\tfrac12)\ulp(\sqrt{c})=\tfrac12\ulp(\sqrt{c})$ ããçã«å¤§ããå¤ã $\sqrtf(a)\oplus\sqrtf(b)$ ã«è¶³ããã¨ã§ã$\sqrtf(c)$ 以ä¸ã®å¤ã«ä¸¸ãããã¾ãã çã«å¤§ããããçç±ã¯ã丸ãã® tiebreak ã§æå³ããªãæ¹ã«ä¸¸ããããã®ãé²ãããã§ãã
$c = 10^9$ ã㤠$p = 64$ ã®ä¸ã§ã¯ããããæºããæå°ã®å¤ã¯ $2^{\floor{\log_2(\sqrt{10^9})}-64}\cdot(1+2^{-63})$ ã§ããã $$ {\footnotesize 8.881784197001252324} {\scriptsize 3520183169} {\scriptsize 2018042652} {\tiny 79889712924636592690508241076940976199693977832794189453125} \times 10^{-16} $$ ã§ãã
ãªãããªãã©ã«ã¨ãã¦æ¸ããéã«ãæ£ç¢ºã«ä¸¸ããããããã$2^{\floor{\log_2(\sqrt{10^9})}-64}\cdot(1+2^{-64})$ ããå°ãã§ã大ããå¤ã§ããã°ä¸è¨ã®å¤ã«ä¸¸ãããã¾ãã大ããããã¨å¥ã®å¤ã«ä¸¸ããããã®ã§æ³¨æã§ãã
constexpr long double eps = 8.8817841970012523238705358308233714632639944856462318296345254120538470488099846988916397094726562500000000000000000000000001e-16L; /* çãã«æ¸ããã人åã */ constexpr long double eps = 8.881784197001252324e-16L; constexpr long double eps = 8881784197001252324e-34L;
ãã¡ãããä½ããã®äºæ
ããªãéãã¯ãããããã®å¤ãè¨å®ããå¿
è¦ã¯ãªãããã9e-16L
ãªã©ã¨ç°¡åãªå¤ã使ãããã§ãã
ãã®ããã大ãããã¦ãåé¡ãªããã¨ã示ãããã次ã®ç¯ãéè¦ã§ãã
$\varepsilon_U'$ ã®ä¸ç
$\sqrt{a}+\sqrt{b}\lt\sqrt{c}$ ãèãã¾ãã ã¾ãã誤差ã®ãªãä¸çã§ã®è¨ç®ãèãã¾ãã
$$ (\sqrt a+\sqrt b+\sqrt c)(\sqrt a+\sqrt b-\sqrt c)(\sqrt a-\sqrt b+\sqrt c)(\sqrt a-\sqrt b-\sqrt c) $$ ã¯æ´æ°ã¨ãªãã¾ããåå åã®ç¬¦å·ã¯ $$ \underbrace{(\sqrt a+\sqrt b+\sqrt c)}_{\gt0}\underbrace{(\sqrt a+\sqrt b-\sqrt c)}_{\lt0}\underbrace{(\sqrt a-\sqrt b+\sqrt c)}_{\gt 0}\underbrace{(\sqrt a-\sqrt b-\sqrt c)}_{\lt0} $$ ã§ãããæ´æ°æ§ã¨åãã㦠$1$ 以ä¸ã§ãããã¨ããããã¾ãã符å·ãè² ã®ãã®ã¯çµ¶å¯¾å¤ãèããã¨ãã¦ã $$ |\sqrt a+\sqrt b-\sqrt c| \ge \frac1{(\sqrt a+\sqrt b+\sqrt c)(\sqrt a-\sqrt b+\sqrt c)\cdot|\sqrt a-\sqrt b-\sqrt c|} $$ ã§ãããå³è¾ºã®åæ¯ãä¸ããè©ä¾¡ãããã§ãã
ã¾ãã$(\sqrt a+\sqrt b+\sqrt c)\lt 2\sqrt c$ ã§ãã 次ã«ãç¸å ç¸ä¹å¹³åã®é¢ä¿ã«æ°ãã¤ãã¤ã¤ $$ \begin{aligned} (\sqrt a-\sqrt b+\sqrt c)\cdot|\sqrt a-\sqrt b-\sqrt c| &= -(\sqrt a-\sqrt b+\sqrt c)(\sqrt a-\sqrt b-\sqrt c) \\ &= -( (a+b-2\sqrt{ab})-c) \\ &= c - (a+b) + 2\sqrt{ab} \\ &\le c - (a+b) + (a+b) = c \end{aligned} $$ ã¨ãããã¾ãããã£ã¦ã $$ \sqrt c-(\sqrt a+\sqrt b) \gt \frac1{2c\sqrt c} $$ ã¨ãªãã¾ããããªãã¡ã $$ \sqrt a+\sqrt b\lt \sqrt c - \frac1{2c\sqrt c} $$ ã§ãã
$$ \begin{aligned} \sqrtf(a)\oplus\sqrtf(b) &\le \roundf(\sqrt{a}+\sqrt{b}) + \ulp(\sqrt{a}+\sqrt{b}) \\ &\le (\sqrt{a}+\sqrt{b})+\tfrac32\ulp(\sqrt{a}+\sqrt{b}) \\ &\lt \left(\sqrt c - \frac1{2c\sqrt c}\right) + \tfrac32\ulp(\sqrt{c}) \\ &= \sqrt c - \frac1{2c\sqrt c} + 3\cdot2^{\floor{\log_2(\sqrt{c})}-p}, \\ \sqrtf(c) &\ge \sqrt{c} - \tfrac12\ulp(\sqrt{c}) \\ &\ge \sqrt{c} - 2^{\floor{\log_2(\sqrt{c})}-p} \end{aligned} $$ ãªã®ã§ããããã®å·®ã¯ $$ \begin{aligned} &\phantom{{}={}} \sqrtf(c) - (\sqrtf(a)\oplus\sqrtf(b)) \\ &\gt (\sqrt{c} - 2^{\floor{\log_2(\sqrt{c})}-p}) - \left(\sqrt c - \frac1{2c\sqrt c} + 3\cdot2^{\floor{\log_2(\sqrt{c})}-p}\right) \\ &= \frac1{2c\sqrt c} - 4\cdot2^{\floor{\log_2(\sqrt{c})}-p} \end{aligned} $$ ã¨ãªãã¾ãããã®å·®ã $k\ulp(\sqrt{c})$ ã®ã¨ãã丸ã誤差ã«ãã£ã¦ $$ (\sqrtf(a)+\sqrtf(b))\oplus\underbrace{(k-\tfrac12)\ulp(\sqrt{c})}_{\varepsilon} = \sqrtf(c) $$ ã«ãªããã¾ãã$\varepsilon$ ã $(k-\tfrac12)\ulp(\sqrt{c})$ ãããããã«å°ããè¨å®ããã°å¤§ä¸å¤«ããã§ãã
ã¨ãããã¨ã§ã$c = 10^9$, $p = 64$ ã§è¨ç®ãã¦ã¿ã㨠$$ \begin{aligned} \frac1{2c\sqrt c} - 4\cdot2^{\floor{\log_2(\sqrt{c})}-p} &= \tfrac12(10^9)^{-3/2} - 4\cdot 2^{\floor{\log_2(\sqrt{10^9})}-64} \\ &= \tfrac12\cdot 10^{-27/2} - 4\cdot 2^{-50} \\ &\approx 1.403 \times 10^{-14} \end{aligned} $$ ã¨ãªãã¾ãã $$ \underbrace{7.5\ulp(\sqrt{10^9})}_{\gt 1.332\times10^{-14}} \lt 1.403\times 10^{-14} \lt \underbrace{8\ulp(\sqrt{10^9})}_{\lt 1.422\times10^{-14}} $$ ãªã®ã§ã$2k$ ã®æ´æ°æ§ãã $8\ulp(\sqrt{10^9})$ ã®å·®ã§ä¸ããæãããããã§ã*4ã $7.5\ulp(\sqrt{10^9})$ æªæºã®æ大ã®æ£è¦åæ°ã¯ $(1.875 - 2^{-63})\times 2^{16-63}$ ã§ããã $$ {\footnotesize 1.332267629550187848} {\scriptsize 43132080393349494087} {\tiny 7760882296602907258475934071384472190402448177337646484375} % {\footnotesize 1.332267629550187848} % {\scriptsize 4698394028} % {\scriptsize 2123965793} % {\tiny 88804411483014536292379670356922360952012240886688232421875} \times 10^{-14} $$ ã§ãã
ãã¡ããããªãã©ã«ã¨ãã¦æ¸ããéã®ä¸¸ããèæ ®ããã¨ã$(1.875 - 2^{-64})\times 2^{16-63}$ ããå°ãã§ãå°ããå¤ã§ããã°ä¸è¨ã®å¤ã«ä¸¸ãããã¾ãã
constexpr long double eps = 1.332267629550187848469839402821239657938880441148301453629237967035692236095201224088668823242187499999999999999999999999999e-14L; /* çãã«æ¸ããã人åã */ constexpr long double eps = 1.3322676295501878484e-14L; constexpr long double eps = 13322676295501878484e-33L;
hack ã«ã¤ãã¦
åé ã§ã$8.5 \ulp(10^9)$ ãããããã«å°ããå¤ 1.5099033134902128948e-14L
ã使ã£ã解æ³ã¯ hack å¯è½ã¨æ¸ãã¾ããã
hack ã§ããã±ã¼ã¹ã®ä¾ã¯ä¸è¨ã§ãã
239488342 239488343 957953370 247729794 247784320 991028225 248961062 248992620 995907363 249514080 249568802 998165761 249999995 249999996 999999982
ãã以å¤ã«ããããããã£ã¦ãè¦ã¤ããéãã§ã 649,152 åããã¾ããã
ã±ã¼ã¹ã«é¢ãã¦
ãã¼ã«ã«ç°å¢ã M2 Mac ãªã®ã§ãããlong double
ã®ç²¾åº¦ã double
ã¨åã $p=53$ ã§ã¤ããã§ãã
æ°åãããã³ã¼ãã AtCoder ã®ã³ã¼ããã¹ãã«æããããã«ããããªãã®ã§ãDocker ã§ããããç°å¢ãä½ã£ã¦å¯¾å¦ãã¾ããã$p=113$ ã®ç°å¢ãä½ããã¨ãã§ããã®ã§æºè¶³ã§ãã
$\sqrt a+\sqrt b=\sqrt c$ ã㤠$\sqrtf(a)\oplus\sqrtf(b)\lt\sqrtf(c)$ ã®ä¸ã§ã誤差ã大ãããªãã±ã¼ã¹ã«ã¤ãã¦ãæ¢ç´¢ãã¾ããã $\sqrt{c}\in\N$ ã¨ãªãã±ã¼ã¹ã§ã¯ï¼å¶ç´ã®ç¯å²ã§ã¯ï¼èª¤å·®ãåºãªãã®ã§ãsquare-free ãªæ´æ° $m\ge 2$ ã«å¯¾ã㦠$i_a\sqrt m+i_b\sqrt m=(i_a+i_b)\sqrt m$ ã®ã±ã¼ã¹ãå ¨æ¢ç´¢ãã¾ããã
8 268609842 268702562 8 799840008 800000000 1002528 236661768 268470792 99904500 467737920 999980820 100017228 400692747 901090683 100026368 400218632 900407048 107717610 430870440 969458490 109996887 439987548 989971983 249961152 250015923 999954147
ãªã©ã235,172,054 åã®ã±ã¼ã¹ã§ã$2^{-49} \approx 1.776\times 10^{-15}$ ã®èª¤å·®ãåºããã¨ãã§ãã¾ããã
ä¸è¨ã® $\sqrt{a}+\sqrt{b}=\sqrt{c}$ ã¨ãªãã±ã¼ã¹ (2,809,012,244 å) ã¨ãå¾è¿°ã® $\sqrt{a}+\sqrt{b}\lt\sqrt{c}$ ã®å·®ã極å°ã¨ãªãã±ã¼ã¹ (250,056,424 å) ãã¹ã¦ã«ã¤ãã¦ãä»åè¨å®ãã eps
ãæ£ããå¤å®ã§ãã¦ãããã¨ã確èªãã¾ãããåè
㯠11 åãå¾è
㯠1 åç¨åº¦ã§æ¸ã¿ã¾ããã
å·®ã®ä¸çã«ã¤ãã¦
$\sqrt{a}+\sqrt{b}\lt\sqrt{c}$ ã®ã±ã¼ã¹ã«ããã $\sqrt{c}-(\sqrt{a}+\sqrt{b})$ ã®ä¸çã«ã¤ãã¦ã§ãã
ãã®ç¯ã®å
容㯠eps
ã®è¨å®ã«ã¯ç´æ¥é¢ä¿ãªãï¼ç¯å²ãçµãã ããªãåã®ç¯ã ãã§ååï¼ã§ãããèªåã§è§£æ±ºã§ããªãã£ãã®ã§è¼ãã¦ããã¾ããæ°å¦ã«èªä¿¡ã®ãããã©ãã®ããå¾
ã¡ãã¦ããã¾ãã
$$ f(c) = \min_{\substack{\sqrt{a}+\sqrt{b}\lt\sqrt{c'}\\c'\le c\\a, b, c\in\N}} \sqrt{c}-(\sqrt{a}+\sqrt{b}) $$ ãèãã¾ãã
Conjecture 1: $c\le 10^5$ ãããã§å®é¨ããã¨ããã$c$ ãæé ã«è¦ã¦ãã£ãã¨ãã« $f(c)$ ã®å¤ãæ´æ°ãããã®ã¯ã$c$ ãä¸è¨ã®å½¢ã§æ¸ããã¨ã (if and only if) ã«ãªãããã§ãã
$c$ | $a$ | $b$ | $c\bmod 4$ | $f(c)$ |
---|---|---|---|---|
$2(2k+1)$ | $k$ | $k+1$ | $2$ | $\varepsilon_0(c)$ |
$(2k+1)(2k+3)$ | $k(k+1)$ | $(k+1)(k+2)$ | $3$ | $\varepsilon_1(c)$ |
$2(k+1)(k+2)$ | $\tfrac12 k(k+1)$ | $\tfrac12(k+2)(k+3)$ | $0$ | $\varepsilon_2(c)$ |
$(2k+1)(6k+3\pm2)$ | $k(3k\pm1)$ | $(k+1)(3(k+1)\pm1)$ | $1$ | $\varepsilon_3(c)$ |
ãã ãã$\varepsilon_i(x)$ 㯠$x\ge i$ ã§å®ç¾©ãããé¢æ° $$ \varepsilon_i(x) = \sqrt{x} - \frac12\left(\sqrt{x+i-2\sqrt{ix+1}} + \sqrt{x+i+2\sqrt{ix+1}}\right) $$ ã§ãã
Conjecture 2: ä»»æã® $i$ ã«ã¤ã㦠$\varepsilon_i(x) \sim \tfrac12x^{-3/2}$ ãæãç«ã¡ããã§ãã
Wolfram|Alpha çã«ã¯åã£ã¦ãããã§ããããæ°å¦åã足ãããèªåã§ã¯ç¤ºãã¾ããã§ããã
Conjecture 3: ã¾ããä»»æã® $i$ ã«ã¤ã㦠$x\ge i+1$ ãªãã° $\varepsilon_i(x) \lt \varepsilon_{i+1}(x)$ ãæãç«ã¡ããã§ãã
ã°ã©ããããããããæãã¯åã£ã¦ãããã§ããããæè¨ç®ã¯ãã¾ãããããæãåºãã¦ãã¾ãã¾ããã
ã¾ã¨ã
Sqrt Inequality ã«é¢ãã¦ã$c\le 10^9$ ã㤠$p=64$ ã§ããã° $$(1+2^{-63})\times 2^{-50} \le \text{\texttt{eps}} \le (1.875 - 2^{-63})\times 2^{-47}$$ ã®ç¯å²ã§è¨å®ããã°ããããã¨ãããã¨ããããã¾ããã10 é² 4 æ¡ã 㨠$$8.882\times 10^{-16} \le \text{\texttt{eps}} \le 1.332\times 10^{-14}$$ ã¨ãªãã¾ãã
ã¾ããã¸ã£ãã¸ä¸ã§ AC ã¨ãªãã³ã¼ãã hack ã§ãããã±ã¼ã¹ãããããè¦ã¤ãããã¨ãã§ãã¾ããã
æµ®åå°æ°ç¹æ°ã®èª¤å·®ã®è©ä¾¡ã大å¤ã¨ãããã¨ããããã¾ããï¼å®éã«ã¯ãã£ã¨ããã£ã¨è©ä¾¡ãã¦ããé¨åãããã°ã£ã¦è©°ãããã¨ãããããå¿ è¦ä»¥ä¸ã«å¤§å¤ã«ãªã£ãã¨ããé¨åã¯ããã¾ãï¼ãããããããã£ã¦ããããã¨æãã¾ããã
åè
ããããã®ãä¸ããã®è©ä¾¡ã®æ¹ãä¸ããã®è©ä¾¡ãããé£ãããªãããããéæ°ãä¸ããè©ä¾¡ãã¾ãããã
— maspy (@maspy_stars) 2020å¹´3æ14æ¥
x_0 = \sqrt{a}+\sqrt{b}+\sqrt{c}
x_1 = \sqrt{a}+\sqrt{b}-\sqrt{c}
x_2 = \sqrt{a}-\sqrt{b}+\sqrt{c}
x_3 = \sqrt{a}-\sqrt{b}-\sqrt{c}
ã¨ããã¨ãx_0x_1x_2x_3 ã¯æ´æ°ã«ãªãã¾ãã
ãã®ãã¤ã¼ãã§ã¯ $$\sqrt{c}-(\sqrt{a}+\sqrt{b})\ge \tfrac1{27}\cdot (10^9)^{-3/2}$$ ã ãã示ãã¦ãã¾ãããæ¹éã§è©°ã¾ã£ã¦ããã¨ãã«èªãã§ã¨ã¦ãåèã«ãªãã¾ããããã£ã±ãæ°å¦ã«æããã¦çµé¨ãããæ¹ã¯éããªã¨æãã¾ããã
ã½ãããã¡ãããªã¹ãã¯ãã
ææ
æµ®åå°æ°ç¹æ°ã®èª¤å·®ã«ã¤ãã¦ã¯æªè¨¼æã§ããã£ãã¼ã¿ãããªé¢¨æ½®ããã¾ãããã
æªè¨¼æ貪欲ã«ã¤ãã¦ããããè¨ã£ã¦ãã人ã double
ã§é©å½ã«è¨ç®ã㦠AC ãããã¨ã«å¯¾ãã¦å¹³æ°ãªã®ã¯ããã«ã¹ã¿ã³ãã¼ããªã®ã§ã¯ãªãã§ããããã
å®ç¨çã«ãã°ãä¾ãã¾ã æãã¤ãã¦ããªãã®ã§ãããããè¿°èª $P(x)$ ããç¡é精度ã§è¨ç®ããã¨ãã¯ã$x\in(-\infty\ldots \alpha]$ ã§ã¯ true ã¨ãªãã$x\in(\alpha\ldots{+\infty})$ ã§ã¯ false ã¨ãªããã¨ããæ¡ä»¶ãæºããã¨ãã¦ãã$P(x)$ ãæµ®åå°æ°ç¹æ°åã§è¨ç®ããå ´åã«ãã®æ§è³ªãæºããããã¨ã¯éãã¾ããã
ãã¨ãã° $x \le 0 \vee (\tfrac 1x)\cdot x = 1$ ã¨ããæ¡ä»¶ã¯ä¸è¨ã®æ§è³ªãæºããã¾ã ($\alpha=+\infty$) ããæµ®åå°æ°ç¹æ°åã§ã¯å¤ãã®åä¾ãããã¾ãã
$p$ | å | åä¾ |
---|---|---|
$24$ | float (32-bit) |
$41$ |
$53$ | double (64-bit) |
$49$ |
$64$ | long double (80-bit) |
$41$ |
$113$ | long double (128-bit) |
$43$ |
ã¾ãã$[1\ldots 2)$ ã®ç¯å²ã®æå°ã®åä¾ã¯ãä¸è¨ã® $p$ ã®é ã«æ¬¡ã®ããã«ãªãã¾ãã$p=113$ ã«ã¤ãã¦ã¯æ¢ç´¢ä¸ã§ã*5ã
$$
\begin{aligned}
\texttt{801467}_{(16)}\times2^{-23} &= {\footnotesize 1.000622630119323730}{\scriptsize 46875}, \\
\texttt{1000000f5cbf2a}_{(16)}\times2^{-52} &= {\footnotesize 1.000000057228997096}{\scriptsize 81428248586598783731}{\tiny 4605712890625}, \\
\texttt{800000005a82799a}_{(16)}\times 2^{-63} &= {\footnotesize 1.000000000164636126}{\scriptsize 99697816044164255799}{\tiny 842067062854766845703125}.
\end{aligned}
$$
$p = 113$ ã¯ãå ¨æ¢ç´¢ã ã¨ç¡çããã ã£ãã®ã§ãèãã¦æ±ãã¾ãããå¥éè¨äºã«ãã¾ãã$1+(2^{57}+\varepsilon)\cdot 2^{-112}$ ãããã ã£ãã®ã§ã$1$ ããæé ã«æ±ããã®ãããã¦æ£è§£ã§ããã $$ \begin{aligned} &\phantom{{}={}} \texttt{1000000000000022df0668c89bf13}_{(16)}\times 2^{-112} \\ &= {\footnotesize 1.000000000000000030}{\scriptsize 24593730708203819677}{\tiny 89344748946071241091121526548576685378133532822175766341388225555419921875}. \end{aligned} $$
C/C++ ã§è¨ç®ããã¨ãfloat
ãåæã« double
ã«å¤æããã (floating-point promotion) ã®ã§èª¤å·®ãçããªãããã«è¦ãã¾ãããã$p=24$ ã§è¨ç®ãã¦ããªãã®ã§ä¸æ£ã§ããã
ãªããåå¤ãæ£è¦åæ°ã¨ãªãç¯å²ã«ããã¦ã¯ãä¸è¨ã®ããã« $(1.0\oslash x)\otimes x = 1-2^{-p}$ ã«ãªããã¨ã¯ãããã®ã®ãä¸æ¹åã«ãºã¬ã¦ $1+2^{1-p}$ ã¨ã¯ãªããªããã¨ã示ãã¾ãï¼å¥ã®è¨äºã«æ¸ãããï¼ï¼éæ£è¦åæ°ã«ã¤ãã¦ã¯ã¾ã èãã¦ãã¾ããï¼ã
â 追è¨ï¼ä¸è¨äºã¤ã®ãããã¯ã«å¯¾ãã¦æ¸ãã¾ãã â
äºåæ¢ç´¢ã§ä½ãããã®è¨ç®ããã¦ãã¦ãçå¤ããªå¤ã§æå³ããªã true/false ãè¿ã£ã¦ãã¦å¤ãªãã¨ã«ãªãå¯è½æ§ã¯å ¨ç¶ããã¨æã£ã¦ãã¦ããããããã¨ã«ç¡é çãªã®ã¯ã©ããªã®ï¼ã¨ããæ°ãæè¿ãã¦ãã¾ãã ãããã®ã§ãæè¿ã¯ãå¯è½ãªéãæçæ°ã§äºåæ¢ç´¢ããããã«ãã¦ãã¾ãã
ã¨ã¯ãããè¨ç®éä¸ã§ãªã¼ãã¼ããã¼ããªããã¨ã®æ ä¿ãã âéæ¬è³ªãªç´°é¨â ã®ããã«æã£ã¦ãã人ãå¤æ°ããããªæãããã¾ãããè¦æ±ãããã§ããããã å®éããããã®è©ä¾¡ãã³ã³ãã¹ãä¸ã«ããã¨è¨ãããããããã楽ããã¨ã¯ãã¾ãæããªãããã§ãï¼ã¨ãããæéã足ããªãããï¼ï¼å¥ã®ã¸ã£ã³ã«ã®ç«¶æã¨ãã¦ã¯ããããããã¾ãããï¼ã
ããã¯ããã¨ãã¦ãå ã®åé¡ã®è§£èª¬ã§ãæ´æ°ã§ããã¾ããããã§æ¸ã¾ããã®ã§ã¯ãªãã$\varepsilon=10^{-14}$ ã ã¨ãã¾ããããã¨ã説æã§ãããã¨æ¸ãã¦ããã®ããã£ã±ãããããªã¨æãã¾ããã
ABC 169 C ãªã©ã誤差ã«é¢ããæè²çãªåé¡ã§ããã³ã³ãã¹ãå½æ㯠long double
ã§è§£ãã ã㧠AC ã«ãªã£ã¦ãã¾ãããã§ããããç¾å¨ã¯ã±ã¼ã¹ã追å ããã¦ããããã§ãããã¡ãã«ã¤ãã¦ã誤差è©ä¾¡ãèãã¦ã¿ã¦ã¯ãããã§ããããã
ãã¾ã
Python ã® Decimal
ã§ç²¾åº¦ãä¸ãã¦è¨ç®ããå ´åããåæ§ã«ããããã®ã±ã¼ã¹ã§ hack ãå¯è½ã§ãã
2 2 8 8 8 32 2 8 18
ãã®åãã¡ã¯ãå°ããã±ã¼ã¹ãªãããå¤ãã®ç²¾åº¦ã»ä¸¸ãè¨å®ã«ããã¦èª¤å¤å®ããããã¨ãã§ãã¾ããã
ããã
ãç²ããã¾ã§ããã
*1:è¤æ°ããå ´åã¯ä»®æ°é¨ãå¶æ°ã§ãããã®ã¨ãã¾ãããããã roundTiesToEven ã§ãã
*2:$\roundf(x+y)$ 㨠$\hat x\oplus\hat y$ ã® ULP ã¯ç°ãªãã±ã¼ã¹ãããã¾ãããããã§ã¯å°ããæ¹ãæ¡ç¨ããã¨ãã¾ãã
*3:å¤ãã®åé¡ã¯ããã®ãã㪠âç´°é¨â ã«æ°ãæããã«è§£ããããã«ãªã£ã¦ãã¾ããããã®ããã§ãããã¯è·äººãããªã§æ±ºããå¤ã ããããããããªããé©å½ã«æ¸ãã¦ããã°ãããã¨æèåæ¢ãã¦ãã人ãå¤ãã®ã¯ãã¾ã好ã¾ãããã¨ã§ã¯ãªãæ°ããã¾ãã
*4:$2\ulp(\sqrt{a}+\sqrt{b})=\ulp(\sqrt{c})$ ã¨ãªãã±ã¼ã¹ã§ $k$ ã¯åæ´æ°ã¨ãªããã¨ãããããã§ãã
*5:$1.9\times 10^{12}$ å調ã¹ã¾ãããã¾ã åºã¦ãã¾ãããè¦ã¤ãã次第追è¨ãã¾ããããããã¦ãã $1+2^{-56}$ ãããæªæºã«ã¯åå¨ããªãã£ãããã¾ããï¼