ç¹ã« Advent Calendar ã¯é¢ä¿ãªã 12 æ 13 æ¥ã®è¨äºã§ãã
ããã®è§£èª¬ã§ãã
以ä¸ã®äºã¤ãåèã«ããªããèãããã¨ãã¾ã¨ãã¾ãã
ACL ã® floor_sum ã®ã³ã¼ã (ç¹ã« x_max ã¾ãã) ãä½ãã£ã¦ããããããªãã¦èªåã§èãç´ãã¦ãããx_max ãå¿ è¦ãªããªã£ã¦ã¤ãã§ã«30%ãããé«éãªãã¾ããð¤https://t.co/uZ7GUND73d
— fura (@fura_2) 2020å¹´10æ30æ¥
æ°å¼ãå¤ãã§ãããããã㦠MathJax ãã KaTeX ã«ç§»è¡ããã¹ãã§ããï¼
ï¼è¿½è¨ 2021/01/22ï¼\(\KaTeX\) ã«ç§»è¡ãã¾ããï¼
åé¡æ¦è¦
ä¸æ¬¡é¢æ° \(f(x) = ax+b\) ã®åºåã \(m\) ã§åãæ¨ã¦é¤ç®ããå¤ã \(x\in[0, n)\) ã®æ´æ°ã«ã¤ãã¦è¶³ãããã®ãæ±ãã¦ãã
ããªãã¡ã以ä¸ã§å®ç¾©ããã \(g(n, m, a, b)\) ãåºåãã¦ãã \[ g(n, m, a, b) = \sum_{i=0}^{n-1} \left\lfloor\frac{ai+b}{m}\right\rfloor = \sum_{i=0}^{n-1} \left\lfloor\frac{f(i)}{m}\right\rfloor. \]
éè¦ãªè¦³å¯
\(g(n, m, a-m, b)\) 㨠\(g(n, m, a, b-m)\) ã«ã¤ãã¦èª¿ã¹ã¦ã¿ã¾ãã \[ \begin{aligned} g(n, m, a-m, b) &= \sum_{i=0}^{n-1} \left\lfloor\frac{(a-m)\cdot i+b}{m}\right\rfloor \\ &= \sum_{i=0}^{n-1} \left\lfloor\frac{ai+b-im}{m}\right\rfloor \\ &= \sum_{i=0}^{n-1} \left(\left\lfloor\frac{ai+b}{m}\right\rfloor -i\right) \\ &= g(n, m, a, b) - \frac{n(n-1)}{2}. \end{aligned} \] \[ \begin{aligned} g(n, m, a, b-m) &= \sum_{i=0}^{n-1} \left\lfloor\frac{ai+(b-m)}{m}\right\rfloor \\ &= \sum_{i=0}^{n-1} \left(\left\lfloor\frac{ai+b}{m}\right\rfloor -1\right) \\ &= g(n, m, a, b) - n. \end{aligned} \]
ãã¦ãããã«ãã£ã¦ä»¥ä¸ã®ãã¨ããããã¾ãï¼ææã®åæ°ã ãä¸è¨ãé©ç¨ããï¼ã \[ \begin{aligned} g(n, m, a\bmod m, b\bmod m) &= g(n, m, a, b) - \left\lfloor\frac{a}{m}\right\rfloor \cdot \frac{n(n-1)}{2} - \left\lfloor\frac{b}{m}\right\rfloor \cdot n. \end{aligned} \]
ããªãã¡ã\(0\le a\lt m\) ããã³ \(0\le b\lt m\) ã«å¸°çã§ãã¾ãã ãã¨ã¯ãããã«ã¤ãã¦èãã¦ããã¾ãã
èå¯
æ¹éãå ã«æ¸ãã¨ãå°ããã±ã¼ã¹ã«å¸°çããã¦å帰çã«è§£ããã¨ãç®æãã¾ãã ãã¼ã¹ã±ã¼ã¹ã¯ã\(g(0, m, a, b) = 0\) ã§ãã
ãããã contribution technique ã¨ã 主客転å ã¨ãå¼ã°ãã¦ãããã¯ããã¯ã§ç·åããã©ãã¦ããã¾ããããå¤ãè¦ãã¨ãã«ããããä½åç·åã«å¯ä¸ããããæ°ãããã¤ã§ãã \(\gdef\Y{y_{\text{max}}}\)
\(\Y = \lfloor f(n-1)/m\rfloor = \lfloor (a(n-1)+b)/m\rfloor\) ã¨ããã¾ãã
ï¼è¿½è¨ 2020/12/29ï¼ããã㯠\(\Y = \lfloor f(n)/m\rfloor\) ã¨ããã¾ãã両æ¹è©¦ãã¦ã¿ã¾ãããï¼
\[ \begin{aligned} g(n, m, a, b) &= \sum_{i=0}^{n-1} \left\lfloor\frac{f(i)}{m}\right\rfloor \\ &= \sum_{j=1}^{\Y} \sum_{\substack{0\le i\lt n\\ \lfloor f(i)/m \rfloor=j}} j. \\ \end{aligned} \]
ããã§ã\(k_j\) ã \(\lfloor f(i)/m\rfloor = j\) ãªãæå°ã® \(i\) ã¨ãã*1ã¨ã次ã®ããã«æ¸ãã¾ãã
ï¼è¿½è¨ 2021/01/10ï¼è¡éãå°ãããã¾ããï¼
\[ \begin{aligned} g(n, m, a, b) &= \sum_{j=1}^{\Y} \sum_{\substack{0\le i\lt n\\ \lfloor f(i)/m \rfloor=j}} j \\ &= \sum_{j=1}^{\Y-1} j\cdot(k_{j+1}-k_j) + \Y\cdot(n-k_{\Y}) \\ &= \sum_{j=1}^{\Y-1} j\cdot k_{j+1} - \sum_{j=1}^{\Y-1} j\cdot k_j + \Y\cdot n - \Y\cdot k_{\Y} \\ &= \sum_{j=2}^{\Y} \,(j-1)\cdot k_j - \sum_{j=1}^{\Y-1} j\cdot k_j + \Y\cdot n - \Y\cdot k_{\Y} \\ &= \sum_{j=2}^{\Y} \,(j-1)\cdot k_j - \sum_{j=1}^{\Y} j\cdot k_j + \Y\cdot n \\ &= \sum_{j=2}^{\Y} \,(j-1)\cdot k_j - 1\cdot k_1 - \sum_{j=2}^{\Y} j\cdot k_j + \Y\cdot n \\ &= - 1\cdot k_1 - \sum_{j=2}^{\Y} - k_j + \Y\cdot n \\ &= \sum_{j=1}^{\Y} -k_j + n\cdot\Y. \end{aligned} \]
ãã¦ã\(k_j\) ãæ±ãã¾ãã å®ç¾©ããã以ä¸ãæãç«ã¡ã¾ãã
\[ \left\lfloor\frac{f(k_j-1)}{m}\right\rfloor \lt j = \left\lfloor\frac{f(k_j)}{m}\right\rfloor \le \frac{f(k_j)}{m}. \]
ãã㧠\(j \le f(k_j-1)/m\) ã¨ä»®å®ããã¨ã\(j\) ã¯æ´æ°ãªã®ã§ \(\lfloor f(k_j-1)/m\rfloor \ge j\) ã¨ãªãã\(\lfloor f(k_j-1)/m\rfloor \lt j\) ã«çç¾ãã¾ãã ãã£ã¦ã以ä¸ãå¾ã¾ãã
ï¼è¿½è¨ 2021/01/10ï¼2 è¡ç®ãã 3 è¡ç®ã¯ãå·¦ã¨å³ã®ä¸çå¼ããããã \(k_j\) ã«ã¤ãã¦å¤å½¢ãã¦ã¾ã¨ãã¾ããï¼
\[ \begin{aligned} \frac{f(k_j-1)}{m} \lt j \le \frac{f(k_j)}{m}; \\ \frac{a(k_j-1)+b}{m} \lt j \le \frac{ak_j+b}{m}; \\ \frac{mj-b}{a} \le k_j \lt \frac{mj-b}{a}+1; \\ k_j = \left\lceil \frac{mj-b}{a} \right\rceil. \end{aligned} \]
ãã¨ã¯ããããå ã®å¼ã«å ¥ãã¦ããã°ãã¾ãã \(\lceil x\rceil = -\lfloor -x\rfloor\) ã使ã£ãããæ·»åãå ¥ãæ¿ããããã¾ã*2ã
\[ \begin{aligned} g(n, m, a, b) &= \sum_{j=1}^{\Y} -k_j + n\cdot\Y \\ &= n\cdot \Y + \sum_{j=1}^{\Y} - \left\lceil \frac{mj-b}{a} \right\rceil \\ &= n\cdot \Y + \sum_{j=1}^{\Y} + \left\lfloor \frac{-mj+b}{a} \right\rfloor \\ &= n\cdot \Y + \sum_{j=0}^{\Y-1} + \left\lfloor \frac{-m(\Y-j)+b}{a} \right\rfloor \\ &= n\cdot \Y + \sum_{j=0}^{\Y-1} + \left\lfloor \frac{mj+b-m\cdot\Y}{a} \right\rfloor \\ &= n\cdot \Y + g(\Y, a, m, b-m\cdot\Y). \end{aligned} \]
ããå°ããã±ã¼ã¹ã«å¸°çã§ãã¾ããã 念ã®ãããå°ãããªã£ã¦ãããã¨ã確èªãã¦ããã¾ãã\(0\le a\lt m\)ã\(0\le b\lt m\)ã\(1\le n\) ã«æ³¨æã§ãã
ï¼è¿½è¨ 2021/01/10ï¼ããã®è°è«ã¯ã\(\Y = \lfloor f(n)/m\rfloor\) ã®ã¨ãã¯å°ãå¤ããã¨æãã¾ããï¼
\[ \begin{aligned} n - \Y &= n-\left\lfloor\frac{a(n-1)+b}{m}\right\rfloor \\ &\ge n - \frac{a(n-1)+b}{m} \\ &= \frac{mn-an+a-b}{m} \\ &\gt \frac{mn-an+a-m}{m} \\ &= \frac{(m-a)(n-1)}{m} \ge 0; \\ n &\gt \Y. \end{aligned} \]
ããã§çµãã£ã¦ãããã®ã§ãããããä¸å·¥å¤«ã§ãã
ä¸ã®éè¦ãªè¦³å¯ã®ã¨ããã§ç¤ºããããã«ã第 4 å¼æ°ãã第 2 å¼æ°ã好ããªåæ°ã ãå¼ããå¤ãè¨ç®ã§ããã®ã§ãããããã¦ã¿ã¾ãã å ·ä½çã«ã¯ã\(-(n-1)\) åã ãå¼ãã¾ãã
\[ g(\Y, a, m, a(n-1)+b-m\cdot\Y) = g(\Y, a, m, b-m\cdot\Y) + (n-1)\cdot\Y. \]
ããªãã¡ã次ã®å¼ãå¾ã¾ãã\(\Y = \lfloor f(n-1)/m\rfloor\) ãæãåºãã¾ãããã
ï¼è¿½è¨ 2021/01/10ï¼\(x\bmod y = x - \lfloor x/y\rfloor\cdot y\) ã使ãã¾ããï¼
\[ \begin{aligned} g(\Y, a, m, b-m\cdot\Y) &= g(\Y, a, m, a(n-1)+b-m\cdot\Y) - (n-1)\cdot\Y \\ &= g(\Y, a, m, f(n-1)-m\cdot\lfloor f(n-1)/m\rfloor) - (n-1) \cdot \Y \\ &= g(\Y, a, m, f(n-1)\bmod m) -(n-1)\cdot\Y. \end{aligned} \]
ãããå ã®å¼ã«æ»ãã¦ã´ã¼ã«ã§ãã
\[ \begin{aligned} g(n, m, a, b) &= n\cdot \Y + g(\Y, a, m, b-m\cdot\Y) \\ &= n\cdot \Y + g(\Y, a, m, f(n-1)\bmod m) -(n-1)\cdot\Y \\ &= \lfloor f(n-1)/m\rfloor + g(\lfloor f(n-1)/m\rfloor, a, m, f(n-1)\bmod m). \end{aligned} \]
\(\lfloor f(n-1)/m\rfloor\) 㨠\(f(n-1) \bmod m\) ã¯ãã³ã³ãã¤ã©ãä¿¡ããã°é¤ç®å½ä»¤ 1 åã§ãã£ã¦ãããã¨æãã¾ãã
å®è£
fn floor_sum(n: i64, m: i64, mut a: i64, mut b: i64) -> i64 { let mut res = 0; if a >= m { res += n * (n - 1) / 2 * (a / m); a %= m; }; if b >= m { res += n * (b / m); b %= m; }; let last = a * (n - 1) + b; if last >= m { let last_div = last / m; let last_mod = last % m; res += last_div + floor_sum(last_div, a, m, last_mod); } res }
é©å½ãªã«ã¼ãããã¦éå帰ã«æ¸ãç´ããã¨ã¯å¯è½ã ã¨æãã¾ããããã®ããããªãã³ã³ãã¤ã©ã«ä»»ããæ¹ãããããããã¾ããã ã«ã¼ãã«ãã¦ã大ãã¦éããªãããç¡é§ã«å¯èªæ§ãè½ã¨ããã¨ãäºæ³ããã¾ãã
ï¼è¿½è¨ï¼2020/12/16ï¼
ããèããã¨ãlast
ã®ã¨ãã㯠a * n + b
ã«ã§ãã¦ããããã㨠last_div
ã足ãå¿
è¦ããªããªã£ã¦ã次ã®ããã«ã§ãã¾ãã
\(\Y\) ãä¸ã®å¼å¤å½¢ã§ã§ã¦ãã \(n-1\) ã \(n\) ã«ç½®ãæãã¦ããã®ã§ã
fn floor_sum(n: i64, m: i64, mut a: i64, mut b: i64) -> i64 { let mut res = 0; if a >= m { res += n * (n - 1) / 2 * (a / m); a %= m; }; if b >= m { res += n * (b / m); b %= m; }; let last = a * n + b; if last >= m { res += floor_sum(last / m, a, m, last % m); } res }
ãªãã§ãããéããªã£ã¦ããã¾ããã§ããï¼ä»¥ä¸ã®æåºã³ã¼ãã¯è«¸äºæ
㧠C++ ã§ãï¼ã
æåºãç´ãããéããªã£ãããã¦ãããã¾ãããã¸ã£ãã¸ã®æ°åå±ããã
- ä¸ã®ãã¤: 70 ms
- ä¸ã®ãã¤: 75 ms
åæ¢æ§ã®è¨¼æã ãæ¸ãç´ãã¾ããï¼è¨æ£ 2021/01/10ï¼ããã¾ããï¼
å·éã«èããã¨ã\(n-\Y\gt 0\) ã示ããªãã¦ããè¨ç®éã®ã¨ããã«æ¸ãããã« \(a\) 㨠\(m\) ã®äºé¤æ³ããé·ãã¯ã«ã¼ãããªããã¨ããããã®ã§ååãªæ°ããã¾ãã
ï¼è¿½è¨ 2021/01/10ï¼è² æ°ã«å¯¾å¿ãããå ´åã¯ã/
ãåãæ¨ã¦é¤ç®ã§ãªãè¨èªã§ã¯æ³¨æãå¿
è¦ã§ããï¼
è¨ç®é
ï¼è¿½è¨ï¼2020/12/14ï¼
\(m, a\) ã®ã¿ã«æ³¨ç®ããã¨äºé¤æ³ã®ããã«ãªã£ã¦ãã¦ãææªã±ã¼ã¹ã§ã¯ \(\log_{\varphi}(\min\{m, a\})\) åç¨åº¦ã®å帰å¼ã³åºãããã¾ãã äºé¤æ³ãçµãã£ãã¨ãã® \(\Y\) ã®å¤ãèããã¨ã\(a = 0\) ã㤠\(b\lt m\) ãªã®ã§ããã以ä¸ã®å¼ã³åºãã¯ããªããã¨ããããã¾ãã \(\varphi\) ã¯é»éæ¯ã§ãã
ã¾ãåè¿°ã®ããã«ãå¼ã³åºããã¨ã« \(n\) ãå°ãªãã¨ã \(\frac{(m-a)(n-1)}{m}\) æ¸ãã¾ãã äºé¤æ³ã®ææªã±ã¼ã¹ã§ã¯ \(a/m\) 㯠\(1/\varphi\) ãããã«ãªã£ã¦ããã®ã§ããã£ããé«ã \(\log_{\varphi}(n)\) åç¨åº¦å帰å¼ã³åºãããããªæ°ããã¾ãã æªãããããããã¦ãã
ãã詳細ã«è¦ç©ããæ¹æ³ã身ã«ã¤ãããã§ããå¤æ°ããã£ã±ããã£ã¦å¤§å¤ã
çµå±ãå帰å¼ã³åºã㯠\(\log_{\varphi}(\min\{m, a, n\})\) åç¨åº¦ã§æãããã¦ãåã 㯠\(O(1)\) æéãªã®ã§ãå ¨ä½ã¨ã㦠\(O(\log(\min\{m, a, n\}))\) æéã ã¨æãã¾ãã
big-O ã ã¨é ããã¡ãããã©ãããã¯ããã¨ãã¦ã«ã¼ãåæ°ã詳ãã解æããã®ã¯æ¥½ããããªã®ã§ãã§ããããã«ãªãããã§ãã
\(n\) ã«é¢ããè©ä¾¡ã®ããã¡
ï¼è¿½è¨ 2021/01/10ï¼
Fibonacci æ°åã \(\{F_i\}\) ã¨ãã¾ãã
\(a\) 㨠\(m\) ã®äºé¤æ³ã®ææªã±ã¼ã¹ã§ã¯ã \( (F_i, F_{i-1}) \dots, (F_2, F_1), (F_1, F_0)\) ã®ããã«åãã¾ãã ãã®ã¨ãã\(j\) ã¹ãããå¾ã® \(n\) ã¯æ¬¡ã®ããã«ãªã£ã¦ããã¯ãã§ãï¼\(\pm 1\) ã¨ãã¯è¿½ã èããå¿ è¦ãããã¾ãï¼ã
\[ \left\lfloor \frac{F_{i-j}}{F_{i-j+1}} \dots \left\lfloor \frac{F_{i-1}}{F_i}\cdot n\right\rfloor\dots\right\rfloor \approx \left\lfloor \frac{F_{i-j}}{F_i}\cdot n \right\rfloor. \]
\(n\) ãååå°ãããã°ã\(j=i\) ã¨ãªãåã«ãã㯠\(0\) ã«ãªãã¯ãã§ãã \(F_i ã \varphi^i\) ãããã§ãããã¨ãèããã¨ã\(\log_{\varphi}(n)\) ã¹ãããããã㧠\(0\) ã«ãªãããã§ãã
\(a\) 㨠\(m\) ã«ã¤ãã¦ã¯äºé¤æ³ã®è°è«ãã \(\log_{\varphi}(\min\{a, m\})\) ã¹ãããããã㧠\(0\) ã«ãªãã®ã§ããããã® \(\min\) ã®ãªã¼ãã¼ã«ãªãããã§ãã
ããã
ãç²ããã¾ã§ããã
ãããã
ãã®é«éåã® PR ã ACL ã«ãã¼ã¸ããã¾ããã
ãããã 2
yosupo ããã ACL Practice Contest 㮠解説 ã«è¼ãã¦ããã¾ããã