temp
例题1:T750199 九个太阳
题意
给定
做法
其中
:::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int n, k, now = 1, mul, ans;
int qpow(int x, int y) {
if (!y) return 1;
int now = qpow(x, y >> 1);
if (!(y & 1)) return now * now % mod;
return now * now % mod * x % mod;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> k, mul = qpow(3, (mod - 1) / k);
for (int i = 1; i <= k; i++) ans = (ans + qpow((
now + 1) % mod, n)) % mod, now = now * mul % mod;
cout << ans * qpow(k, mod - 2) % mod;
return 0;
}
:::
例题2:T750203 LJJ 学二项式定理
题意
给定
做法
:::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int t, w, n, s, a[4];
int qpow(int x, int y) {
if (!y) return 1;
int now = qpow(x, y >> 1);
if (!(y & 1)) return now * now % mod;
return now * now % mod * x % mod;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t, w = qpow(3, (mod - 1) / 4);
while (t--) {
cin >> n >> s;
for (int i = 0; i < 4; i++) cin >> a[i];
int ans = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
int mul = a[i] * qpow(w, (-i * j % 4 + 4) %
4) % mod, now = (s * qpow(w, j) + 1) % mod;
ans = (ans + mul * qpow(now, n) % mod) % mod;
}
cout << ans * qpow(4, mod - 2) % mod << '\n';
}
return 0;
}
:::
这里注意到
这样就转化为矩阵的二项式定理
因此
这里定义
:::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define int long long
int t, g, w, n, k, p;
struct mat {
int a, b, c, d;
mat operator * (const mat y) const {
return {(a * y.a + b * y.c) % p,
(a * y.b + b * y.d) % p, (c * y.a +
d * y.c) % p, (c * y.b + d * y.d) % p};
}
};
mat mpow(mat x, int y) {
mat ans = {1, 0, 0, 1};
while (y) {
if (y & 1) ans = ans * x;
x = x * x, y >>= 1;
}
return ans;
}
int qpow(int x, int y) {
if (!y) return 1;
int now = qpow(x, y >> 1);
if (!(y & 1)) return now * now % p;
return now * now % p * x % p;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> k >> p;
vector <int> cur;
int tmp = p - 1;
for (int i = 2; i * i <= tmp; i++)
while (!(tmp % i)) cur.push_back(i), tmp /= i;
if (tmp > 1) cur.push_back(tmp);
for (int i = 1; i <= p; i++) {
bool fl = 1;
for (int j = 0; j < cur.size(); j++)
fl = fl && (qpow(i, (p - 1) / cur[j]) != 1);
if (fl) { g = i; break; }
}
w = qpow(g, (p - 1) / k);
int ans = 0;
for (int i = 0; i < k; i++) {
int now = qpow(w, i);
ans = (ans + mpow({(now + 1) % p,
now, now, 1}, n).a) % p;
}
cout << ans * qpow(k, p - 2) % p << '\n';
}
return 0;
}
:::
:::info[代码]
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int mod = 998244353;
int n, p, k;
int qpow(int x, int y) {
if (!y) return 1;
int now = qpow(x, y >> 1);
if (!(y & 1)) return 1ll * now * now % mod;
return 1ll * now * now % mod * x % mod;
}
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> p >> k;
int w = qpow(3, (mod - 1) / k), ans = 1ll *
n * p % mod * qpow(p + 1, n - 1) % mod;
for (int i = 1; i < k; i++) {
int num = qpow(w, i), mul = 1ll * num * qpow(num -
1, mod - 2) % mod, now = (qpow((1ll * p * num +
1) % mod, n) - qpow((p + 1) % mod, n) + mod) % mod;
ans = (ans + 1ll * mul * now % mod) % mod;
}
cout << 1ll * ans * qpow(k, mod - 2) % mod;
return 0;
}
::: 可以直接求。