一些常见函数的写法
LL 被宏定义为 long long
1.GCD(非递归)
```c
inline int gcd(register int x,register int y)
{
if(x == 0 || y == 0) return x | y;
register int t;
while(t = x % y) {x = y;y = t;}
return y;
}
```
2.GCD(递归)
```c
inline LL Gcd(const LL a,const LL b){return b ? Gcd(b,a % b) : a;}
```
3.龟速乘
```c
inline LL Multiply(LL x,LL y,const LL& mod)
{
LL res = 0;
for(; x ; x >>= 1,y = (y << 1) % mod)
if(x & 1) res = (res + y) % mod;
return res;
}
```
4.快速幂(防溢出)
```c
inline LL Fast_Pow(LL x,LL p,const LL& mod)
{
LL res = 1;
for(; p ; p >>= 1,x = Multiply(x,x,mod))
if(p & 1) res = Multiply(res,x,mod);
return res;
}
```
5.扩展欧几里德
```c
inline LL Exgcd(const LL a,const LL b,LL& x,LL& y)
{
if(b == 0) x = 1;
else {
Exgcd(b,a % b,y,x);
y -= a / b * x;
}
}
```
--2020/5/4
6.二分(lower_bound)
```c
template<class T>
inline int find(const T a[],const int n,const T key)
{
register int l = 0,r = n;
while(l < r)
{
register int mid = (l + r)>>1;
if(a[mid] <= key) r = mid;
else l = mid + 1;
}
}
```
6.二分(upper_bound)
```c
template<class T>
inline int find(const T a[],const int n,const T key)
{
register int l = 0,r = n;
while(l < r)
{
register int mid = (l + r)>>1;
if(a[mid] < key) r = mid;
else l = mid + 1;
}
}
```
2020/7/1
方形矩阵
```c
struct matrix
{
int len;
int Matrix[105][105];
matrix(){memset(Matrix,0,sizeof(Matrix));len = 0;}
inline void clear(){memset(Matrix,0,sizeof(Matrix));len = 0;}
inline void init()
{
memset(Matrix,0,sizeof(Matrix));
for(reg int i = 1; i <= len; ++i) Matrix[i][i] = 1;
}
inline matrix operator * (const matrix &rhs) const
{
matrix res;res.len = len;
for (reg int i = 1; i <= len; ++i)
for (reg int j = 1; j <= len; ++j){
for (reg int k = 1; k <= len; ++k) res.Matrix[i][j] += Matrix[i][k] * rhs.Matrix[k][j];
res.Matrix[i][j] %= 2009;
}
return res;
}
inline matrix operator ^ (int p) const
{
matrix res,x = *this; res.len = len;res.init();
for (; p; p >>= 1, x = x * x)
if (p & 1) res = res * x;
return res;
}
} ma;
```
快读(非负整数)
```c
inline void in(int& x)
{
x = 0; char ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + (ch ^ 48);ch = getchar();}
}
inline int Read()
{
reg int x = 0;reg char ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + (ch ^ 48);ch = getchar();}
return x;
}
```
2020/7/7
矩阵
```
struct Matrix
{
int width,length;
int matrix[42][42];
Matrix(const int w=0,const int l=0):width(w),length(l){}
};
Matrix operator * (const Matrix& a,const Matrix& b)
{
Matrix res = Matrix(a.width,b.length);
for(reg int i = 1; i <= a.width; ++i)
for(reg int j = 1; j <= b.length; ++j)
for(reg int k = 1; k <= a.length; ++k)
res.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
return res;
}
```
2020/7/10

浙公网安备 33010602011771号