Skip to content

Commit

Permalink
add BinaryGCD.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tatyam-prime committed May 31, 2024
1 parent 84a8bf6 commit 9151361
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 36 deletions.
104 changes: 73 additions & 31 deletions .verify-helper/docs/static/notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h1 id="title">ICPC Notebook</h1>
</ol>
<li><a class="section" href="#math">math</a></li>
<ol>
<li><a href="#BinaryGCD.hpp">BinaryGCD.hpp</a></li>
</ol>
<li><a class="section" href="#modint">modint</a></li>
<ol>
Expand Down Expand Up @@ -223,6 +224,26 @@ <h2 id="FastSet.hpp">FastSet.hpp</h2>

<h2 class="section" id="math">math</h2>

<div class="filename">
<h2 id="BinaryGCD.hpp">BinaryGCD.hpp</h2>
md5: f3ab31

</div>
<div class="content content-hpp">
<pre><code>u64 <span class="token function">ctz</span><span class="token punctuation">(</span>u64 x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">countr_zero</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
u64 <span class="token function">binary_gcd</span><span class="token punctuation">(</span>u64 x<span class="token punctuation">,</span> u64 y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>x <span class="token operator">||</span> <span class="token operator">!</span>y<span class="token punctuation">)</span> <span class="token keyword">return</span> x <span class="token operator">|</span> y<span class="token punctuation">;</span>
u64 n <span class="token operator">=</span> <span class="token function">ctz</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">,</span> m <span class="token operator">=</span> <span class="token function">ctz</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
x <span class="token operator">>>=</span> n<span class="token punctuation">,</span> y <span class="token operator">>>=</span> m<span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>x <span class="token operator">!=</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>x <span class="token operator">></span> y<span class="token punctuation">)</span> x <span class="token operator">=</span> <span class="token punctuation">(</span>x <span class="token operator">-</span> y<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token function">ctz</span><span class="token punctuation">(</span>x <span class="token operator">-</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span> y <span class="token operator">=</span> <span class="token punctuation">(</span>y <span class="token operator">-</span> x<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token function">ctz</span><span class="token punctuation">(</span>y <span class="token operator">-</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> x <span class="token operator">&lt;&lt;</span> <span class="token function">min</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</div>

<h2 class="section" id="modint">modint</h2>

<div class="filename">
Expand Down Expand Up @@ -663,16 +684,6 @@ <h4>高度合成数</h4>
<td>768</td>
<td>1344</td>
</tr>
<tr>
<td>factor</td>
<td>3111</td>
<td>3311</td>
<td>33111</td>
<td>421111</td>
<td>631111</td>
<td>5311111</td>
<td>6321111</td>
</tr>
</tbody></table>
<table>
<thead>
Expand All @@ -684,6 +695,9 @@ <h4>高度合成数</h4>
<th>$10^{13}$</th>
<th>$10^{14}$</th>
<th>$10^{15}$</th>
<th>$10^{16}$</th>
<th>$10^{17}$</th>
<th>$10^{18}$</th>
</tr>
</thead>
<tbody><tr>
Expand All @@ -694,37 +708,65 @@ <h4>高度合成数</h4>
<td>10752</td>
<td>17280</td>
<td>26880</td>
</tr>
<tr>
<td>factor</td>
<td>53211111</td>
<td>63221111</td>
<td>642111111</td>
<td>6321111111</td>
<td>5422111111</td>
<td>64211111111</td>
<td>41472</td>
<td>64512</td>
<td>103680</td>
</tr>
</tbody></table>
<h4>素数階乗</h4>
<table>
<thead>
<tr>
<th>$≤n$</th>
<th>$10^{16}$</th>
<th>$10^{17}$</th>
<th>$10^{18}$</th>
<th>$n$</th>
<th>$2$</th>
<th>$3$</th>
<th>$5$</th>
<th>$7$</th>
<th>$11$</th>
<th>$13$</th>
<th>$17$</th>
<th>$19$</th>
<th>$23$</th>
<th>$29$</th>
</tr>
</thead>
<tbody><tr>
<td>$d^0(x)$</td>
<td>41472</td>
<td>64512</td>
<td>103680</td>
<td>$n\#$</td>
<td>2</td>
<td>6</td>
<td>30</td>
<td>210</td>
<td>2310</td>
<td>30030</td>
<td>510510</td>
<td>9699690</td>
<td>$2.2 \times 10^8$</td>
<td>$6.5 \times 10^9$</td>
</tr>
</tbody></table>
<h4>階乗</h4>
<table>
<thead>
<tr>
<td>factor</td>
<td>83221111111</td>
<td>632211111111</td>
<td>842211111111</td>
<th>$5!$</th>
<th>$6!$</th>
<th>$7!$</th>
<th>$8!$</th>
<th>$9!$</th>
<th>$10!$</th>
<th>$11!$</th>
<th>$12!$</th>
</tr>
</thead>
<tbody><tr>
<td>120</td>
<td>720</td>
<td>5040</td>
<td>40320</td>
<td>362880</td>
<td>3628800</td>
<td>$4.0 \times 10^7$</td>
<td>$4.8 \times 10^8$</td>
</tr>
</tbody></table>
</div>
Expand Down
Binary file modified .verify-helper/docs/static/notebook.pdf
Binary file not shown.
21 changes: 21 additions & 0 deletions build/notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h1 id="title">ICPC Notebook</h1>
</ol>
<li><a class="section" href="#math">math</a></li>
<ol>
<li><a href="#BinaryGCD.hpp">BinaryGCD.hpp</a></li>
</ol>
<li><a class="section" href="#modint">modint</a></li>
<ol>
Expand Down Expand Up @@ -223,6 +224,26 @@ <h2 id="FastSet.hpp">FastSet.hpp</h2>

<h2 class="section" id="math">math</h2>

<div class="filename">
<h2 id="BinaryGCD.hpp">BinaryGCD.hpp</h2>
md5: f3ab31

</div>
<div class="content content-hpp">
<pre><code>u64 <span class="token function">ctz</span><span class="token punctuation">(</span>u64 x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">countr_zero</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
u64 <span class="token function">binary_gcd</span><span class="token punctuation">(</span>u64 x<span class="token punctuation">,</span> u64 y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>x <span class="token operator">||</span> <span class="token operator">!</span>y<span class="token punctuation">)</span> <span class="token keyword">return</span> x <span class="token operator">|</span> y<span class="token punctuation">;</span>
u64 n <span class="token operator">=</span> <span class="token function">ctz</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">,</span> m <span class="token operator">=</span> <span class="token function">ctz</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
x <span class="token operator">>>=</span> n<span class="token punctuation">,</span> y <span class="token operator">>>=</span> m<span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>x <span class="token operator">!=</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>x <span class="token operator">></span> y<span class="token punctuation">)</span> x <span class="token operator">=</span> <span class="token punctuation">(</span>x <span class="token operator">-</span> y<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token function">ctz</span><span class="token punctuation">(</span>x <span class="token operator">-</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span> y <span class="token operator">=</span> <span class="token punctuation">(</span>y <span class="token operator">-</span> x<span class="token punctuation">)</span> <span class="token operator">>></span> <span class="token function">ctz</span><span class="token punctuation">(</span>y <span class="token operator">-</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> x <span class="token operator">&lt;&lt;</span> <span class="token function">min</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> m<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</div>

<h2 class="section" id="modint">modint</h2>

<div class="filename">
Expand Down
11 changes: 11 additions & 0 deletions docs/math/BinaryGCD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Binary GCD
documentation_of: //src/math/BinaryGCD.hpp
---

割り算を使わない高速な GCD

## 使い方

- `u64 binary_gcd(u64 x, u64 y)`:$x$ と $y$ の最大公約数を計算する.
- $O(\log(x + y))$ 時間
3 changes: 2 additions & 1 deletion docs/modint/BarrettReduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
title: Barrett Reduction
documentation_of: //src/modint/BarrettReduction.hpp
---
- 参考実装:[AC Library](https://github.com/atcoder/ac-library/blob/master/atcoder/internal_math.hpp)

同じ mod でたくさん計算するとき,剰余算を掛け算に変換して高速化する.

## 使い方

- `Barrett br(mod)`:Barrett Reduction を準備する.
- 制約:`mod < 2^32`
- `br.mul(u64 a, u64 b)``a * b % mod` を計算する.
- `u64 br.mul(u64 a, u64 b)``a * b % mod` を計算する.
- 制約:`a * b < 2^64`

## 仕組み
Expand Down
1 change: 1 addition & 0 deletions docs/modint/modint.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Modint
documentation_of: //src/modint/modint.hpp
---
- 参考実装:[AC Library (初期実装)](https://github.com/atcoder/ac-library/blob/8250de484ae0ab597391db58040a602e0dc1a419/atcoder/convolution.hpp)

- タイプ速度重視の簡易 modint
- 速度が欲しいとき:[32 bit で加減算をちゃんと書いた Modint](../extra/modint_fast.hpp)
Expand Down
Binary file modified notebook.pdf
Binary file not shown.
File renamed without changes.
10 changes: 6 additions & 4 deletions test/math/FastGCD.test.cpp → test/math/BinaryGCD.test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include "test/template.hpp"
using u64 = uint64_t;
#include "src/math/FastGCD.hpp"
#include "src/math/BinaryGCD.hpp"

int main() {
mt19937_64 rnd;
rep(i, 0, (ll)1e7) {
u64 a = rnd(), b = rnd();
assert(gcd(a, b) == binary_gcd(a, b));
rep(shift, 0, 64) {
rep(i, 0, (ll)1e5) {
u64 a = rnd() >> shift, b = rnd() >> shift;
assert(gcd(a, b) == binary_gcd(a, b));
}
}
puts("Hello World");
}

0 comments on commit 9151361

Please sign in to comment.