实数gcd,大数快速乘与组合数取模

这篇博客介绍了在计算几何问题中如何处理实数的最大公因数(gcd)计算,提供了一个误差处理的模板。此外,还讨论了大数快速乘法的实现,用于处理中间结果可能越界的乘法操作。最后提到了组合数取模的解决策略,结合逆元、Lucas定理、CRT和extend_欧几里得算法,给出了相关题目的链接。
摘要由CSDN通过智能技术生成

实数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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值