离散数学实验四

好像有一个没写博客,那也不写了

实验四:

扩展欧几里得求同余方程:

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void exgcd(LL a, LL b, LL& x, LL& y, LL& d)
{
 if (b == 0) {
 x = 1; y = 0; d = a; return;
 }
 exgcd(b, a % b, x, y, d);
 LL t;
 t = x;
 x = y;
 y = t - (a / b) * y;
}
int main()
{
 ios::sync_with_stdio(false);
 LL a, b, c;
 cin >> a >> b >> c;
 LL x, y, d;
 exgcd(a, b, x, y, d);
 LL g = b / d;
 x = x * (c / d);
 x = (x % g + g) % g;
 cout << x << endl;
 return 0;
}

 高木同学的因子:

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b)
{
 if (b == 0) return a;
 else return(gcd(b, a % b));
}
int main()
{
 LL a, b;
 cin >> a >> b;
 LL tmp = gcd(a, b);
 LL ans = 0;
 for (int i = 1; i * i < tmp; i++) {
 if (tmp % i == 0) ans += 2;
 }
 if (pow(sqrt(tmp), 2) == tmp) ans++;
 cout << ans << endl;
 return 0;
}

费马小定理:

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
LL ksm(LL a, LL b, LL c)
{
	LL ans = 1;
	while (b > 0) {
		if (b & 1)
		{
			ans = ans * a % c;
		}
		b >>= 1;
		a = (a * a) % c;
	}
	return ans;
}
int main()
{
	LL a, b, c;
	while (cin >> a >> b >> c) {
		LL tmp;
		tmp = ksm(b, c, MOD - 1);
		tmp = ksm(a, tmp, MOD);
		cout << tmp << endl;
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值