# 从BZOJ2242看数论基础算法：快速幂，gcd，exgcd，BSGS

### 2.gcd和exgcd

gcd就是所谓的辗转相除法，在这里用取模的形式体现出来
$gcd(a,b)$，因为b中的a对答案没有贡献，考虑把b变成$b-(b/a)*a$答案是一样的

exgcd就是解线性方程$ax+by=c$

$ax+by=gcd(a,b)=gcd(b,a\%b)=bx'+(a\%b)y'$

$ax+by=bx'+ay'-(a/b)*b*y'$

$x = x' + b/gcd(a,b)*t$
$y = y' - a/gcd(a,b)*t$

### 3.BSGS

$a^x=b(mod\ c)$，给出a，b，c求x

//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
template <typename T>
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
int y, z, P;
int add(int a, int b) {
return (a += b) >= P ? a - P : a;
}
int sub(int a, int b) {
return (a -= b) < 0 ? a + P : a;
}
int mul(int a, int b) {
return 1ll * a * b % P;
}
int fast_pow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = mul(res, a);
b >>= 1;
a = mul(a, a);
}
return res;
}
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
void exgcd(int a, int b, ll &x, ll &y) {
if (!b) {x = 1, y = 0; return;}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
void work1() {
Write(fast_pow(y, z));
putchar('\n');
}
void work2() {
int g = gcd(y, P);
if (z % g) {
printf("Orz, I cannot find x!\n");
} else {
ll a, b;
exgcd(y, P, a, b);
a *= z / g;
a = (a % (P / g) + P / g) % (P / g);
Write(a);
putchar('\n');
}
}
map<int, int> mp;
void work3() {
y %= P, z %= P;
if (!y) {
printf("Orz, I cannot find x!\n");
return;
}
int w = sqrt(P), now = z;
mp.clear();
fu(i, 0, w) {
mp[now] = i;
now = mul(now, y);
}
now = fast_pow(y, w);
int tmp = now;
fu(i, 1, w) {
if (mp.count(tmp)) {
Write(i * w - mp[tmp]);
putchar('\n');
return;
}
tmp = mul(tmp, now);
}
printf("Orz, I cannot find x!\n");
}
int main() {
int T, op;