实数gcd
在计算几何的题目中,有时会需要你算两个角度的最大公因数(会有误差),具体操作和整数的gcd类似,但要注意输入可能会有0。
模板如下:
double fgcd(double a,double b)
{
if(fabs(a)<eps) return b;
if(fabs(b)<eps) return a;
return fgcd(b,fmod(a,b));
}//fmod为math库的小数取余函数
大数快速乘
适用于那些中间变量会越界的乘法,快速乘和快速幂的想法基本类似,相当于降一级运算。
模板如下:
LL pow_mul(LL a,LL n,LL m)
{
if(n == 0) return 1;
LL x = pow_mul(a,n>>1,m);
LL ans = (x+x)%m;
if(n&1) ans = (ans+a)%m;
return ans;
}
组合数取模:逆元+lucas定理+CRT+extend_欧几里得算法
题目:2015长春网络赛hdoj5446
地址:http://acm.hdu.edu.cn/showproblem.php?pid=5446
代码:
#include <iostream>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int类型最大值INT_MAX,short最大值为SHORT_MAX
long long最大值为LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[4][2