快速幂

O(logan)的时间复杂度
代码示例

#include <bits/stdc++.h>
using namespace std;

#define int long long

#define mod 998244353

int a, b;

int ksm(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1)res = res * a;// % mod;
		a *= a;// % mod;
		b >>= 1;
	}
	return res;// % mod;
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> a >> b;
	cout << ksm(a, b) % mod << '\n';
	return 0;
}
posted @ 2024-02-04 11:25  BreadCheese  阅读(7)  评论(0)    收藏  举报