Unverified Commit cb44e25f authored by ir1d's avatar ir1d Committed by GitHub
Browse files

Update 快速幂.md (#1357)

Update 快速幂.md
parents 4f4d9e40 c2ebfc4a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ bitset<1000> bs; // a bitset with 1000 bits
### 运算符

-    `operator []` : 访问其特定的一位。
-    `operator ==/!=` : 比较两个 `bitset` 内容是否完全一样。
-    `operator ==/=` : 比较两个 `bitset` 内容是否完全一样。
-    `operator &/&=/|/| =/^/^=/~` : 进行按位与/或/异或/取反操作。 ** `bitset` 只能与 `bitset` 进行位运算** ,若要和整型进行位运算,要先将整型转换为 `bitset`
-    `operator <</>>/<<=/>>=` : 进行二进制左移/右移。
-    `operator <</>>` : 流运算符,这意味着你可以通过 `cin/cout` 进行输入输出。
+55 −0
Original line number Diff line number Diff line
@@ -87,3 +87,58 @@ long long qpow(long long a, long long b, long long p) {
??? note "例题"

    做一做[Luogu P1226](https://www.luogu.org/problemnew/show/P1226)

## 高精度快速幂

??? note "前置技能"
     请先学习[高精度](https://oi-wiki.org/math/bignum/)

??? note " 例题【NOIP2003 普及组改编·麦森数】([原题在此](https://www.luogu.org/problemnew/show/P1045))"
     题目大意:从文件中输入 P(1000&lt;P&lt;3100000),计算 $2^P−1$ 的最后 100 位数字(用十进制高精度数表示),不足 100 位时高位补 0。

### 例题代码

```cpp
#include <bits/stdc++.h>
using namespace std;
int a[505], b[505], t[505], i, j;
int mult(int x[], int y[])  //高精度乘法
{
  memset(t, 0, sizeof(t));
  for (i = 1; i <= x[0]; i++) {
    for (j = 1; j <= y[0]; j++) {
      if (i + j - 1 > 100) continue;
      t[i + j - 1] += x[i] * y[j];
      t[i + j] += t[i + j - 1] / 10;
      t[i + j - 1] %= 10;
      t[0] = i + j;
    }
  }
  memcpy(b, t, sizeof(b));
}
void ksm(int p)  //快速幂
{
  if (p == 1) {
    memcpy(b, a, sizeof(b));
    return;
  }
  ksm(p / 2);
  mult(b, b);
  if (p % 2 == 1) mult(b, a);
}
int main() {
  int p;
  scanf("%d", &p);
  a[0] = 1;
  a[1] = 2;
  b[0] = 1;
  b[1] = 1;
  ksm(p);
  for (i = 100; i >= 1; i--) {
    if (i == 1) {
      printf("%d\n", b[i] - 1);
    } else
      printf("%d", b[i]);
  }
}
```
+4 −4
Original line number Diff line number Diff line
@@ -6,13 +6,13 @@

## 实现过程

1\. 构造空间树。
1.  构造空间树。

2\. 进行遍历。
2.  进行遍历。

3\. 如遇到边界条件,即不再向下搜索,转而搜索另一条链。
3.  如遇到边界条件,即不再向下搜索,转而搜索另一条链。

4\. 达到目标条件,输出结果。
4.  达到目标条件,输出结果。

## 经典例题:

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+4 −4

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+2 −2

File changed.

Contains only whitespace changes.

Loading