备战NOIP——模板复习24

6 篇文章 1 订阅
5 篇文章 0 订阅

这里只有模板,并不作讲解,仅为路过的各位做一个参考以及用做自己复习的资料,转载注明出处。




逆元

费马小定理

EX-GCD

线性递推




逆元

费马小定理

/*Copyright: Copyright (c) 2018
*Created on 2018-11-07
*Author: 十甫
*Version 1.0 
*Title: 逆元-费马小定理
*Time: 2 mins
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;

inline ll mul(ll a, ll b, ll mod) {
	ll res = 1;
	while(b) {
		if(b & 1) res = ((res % mod) * (a % mod));
		a = ((a % mod) * (a % mod));
		b /= 2;
	}
	return res;
}

int main() {
	ll n, k;
	scanf("%lld%lld", &n, &k);
	printf("%lld\n", mul(n, k - 2, k));
	return 0;
}

EX-GCD

/*Copyright: Copyright (c) 2018
*Created on 2018-11-07
*Author: 十甫
*Version 1.0 
*Title: 逆元-EX-GCD
*Time: 2 mins
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;

inline void exgcd(ll a, ll b, ll &x, ll &y, ll mod) {
	if(!b) {
		x = 1, y = 0;
		return;
	}
	exgcd(b, a % b, y, x, mod);
	y -= (a / b) * x;
	y = (y + mod) % mod;
}

int main() {
	ll n, k;
	scanf("%lld%lld", &n, &k);
	ll a, b;
	exgcd(n, k, a, b, k);
	printf("%lld\n", a);
	return 0;
}

线性递推

/*Copyright: Copyright (c) 2018
*Created on 2018-11-07
*Author: 十甫
*Version 1.0 
*Title: 逆元-线性递推
*Time: inf mins
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int size = 10005;

int inv[size];
inline void make(int n, int p) {
	inv[1] = 1;
	for(int i = 2;i <= n;i++) {
		inv[i] = inv[p % i] * (p - p / i) % p;
	}
}

int main() {
	int n, p;
	scanf("%d%d", &n, &p);
	make(n, p);
	printf("%d\n", inv[n]);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值