我的算法模板1(快速幂、逆元、组合数)
快速幂
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;ll qmi(ll a, ll k, ll p){ll res = 1;a = a % p;while(k){if(k & 1)res = res * a % p;k >>= 1;a = a * a % p;}return res;
}
signed main()
{ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int t; cin >> t;while(t--){ll a, b, p; cin >> a >> b >> p;cout << qmi(a, b, p) << endl;}return 0;
}
扩展欧几里得求逆元
#include<bits/stdc++.h>
using namespace std;
#define int long longint exgcd(int a, int b, int &x, int &y){if(b == 0){x = 1, y = 0;return a;}int d = exgcd(b, a % b, y, x);y -= a / b * x;return d;
}
int inv(int a, int b){int x, int y;int d = exgcd(a, p, x, y);if(d != 1)return -1;return (x % p + p) % p;
}signed main()
{ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int t; cin >> t;while(t--){int a, p; cin >> a >> p;int ans = inv(a, p);cout << ans << endl;}return 0;
}
组合数求模
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5000010, p = 998244353;
ll f[N], inv[N];
ll qmi(ll a, ll k, ll p){ll res = 1;a %= p;while(k){if(k & 1)res = res * a % p;a = a * a % p;k >>= 1;}return res;
}
ll C(ll n, ll m){if(m < 0 || m > n)return 0;return f[n] * inv[m] % p * inv[n - m] % p;
}
signed main()
{ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);f[0] = 1;for(int i = 1; i < N; i++)f[i] = f[i - 1] * i % p;inv[N - 1] = qmi(f[N - 1], p - 2, p); for(int i = N - 2; i >= 0; i--){inv[i] = inv[i + 1] * (i + 1) % p;} int t, x; cin >> t >> x;ll ans = 0; while(t--){int n, m; cin >> n >> m;ans ^= C(n, m);} cout << ans << endl;return 0;
}