QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#640679 | #8339. Rooted Tree | kalikari | AC ✓ | 210ms | 81736kb | C++17 | 2.1kb | 2024-10-14 15:12:49 | 2024-10-14 15:12:49 |
Judging History
answer
#define _CRT_SECURE_NO_WARNINGS // 关闭编译器中不安全函数的警告
#include <iostream>
#include <vector>
using namespace std;
#define LL long long // 定义长整型
const int N = 1e7 + 100; // 定义数组大小,最大支持1000万
const LL mod = 1e9 + 9; // 定义模数
const LL INF = 1e18; // 定义长整型的无穷大
LL f[N], a; // 定义数组 f 和变量 a
LL m, k, x; // m 表示某个值,k 表示某个阈值,x 为中间变量
// 快速幂函数,用于快速计算 (a^b) % mod
LL quick(LL a, LL b, LL mod) {
LL ans = 1;
while (b) { // 当 b 不为 0 时
if (b & 1) ans = ans * a % mod; // 如果 b 的二进制表示的最低位是 1,则将当前的 a 乘入结果中
b >>= 1; // b 右移一位,相当于 b 除以 2
a = a * a % mod; // 计算 a 的平方并取模
}
return ans;
}
int main() {
// 输入 m 和 k,m 表示要处理的某个值,k 表示限制的次数
cin >> m >> k;
// 设定初始条件 f[k] = 1
f[k] = 1;
// 从 k-1 开始逆向计算 f[i],其中每一步计算与 (m - 1) 和 i 有关
for (LL i = k - 1; i >= 0; i--)
f[i] = f[i + 1] * ((m - 1) * i % mod + 1) % mod;
// 计算 x 为 f[0] 的模逆元
x = f[0];
x = quick(x, mod - 2, mod); // 利用费马小定理计算模逆元
LL ans = 0; // 用于存储最终的答案
a = 0; // 辅助变量 a 用于存储中间计算结果
// 从 0 到 k-1 的循环,用于逐步累加结果
for (LL i = 0; i <= k - 1; i++) {
// 更新 ans,使用当前的 x 和 f[i+1] 进行相关的计算
ans = (ans + a * x % mod * f[i + 1] % mod + 1) % mod;
// 更新 a,计算与 m 和 f[i+1] 有关的表达式,并模上 mod
a = (a * x % mod * f[i + 1] % mod * ((i + 1) * (m - 1) % mod + 1) % mod + m) % mod;
// 更新 x,递归地调整 x 的值
x = x * (i * (m - 1) % mod + 1) % mod;
}
// 最后将结果乘以 m,并取模
ans = ans * m % mod;
// 输出最终结果
cout << ans << endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3488kb
input:
6 2
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3492kb
input:
2 6
output:
600000038
result:
ok 1 number(s): "600000038"
Test #3:
score: 0
Accepted
time: 11ms
memory: 10360kb
input:
83 613210
output:
424200026
result:
ok 1 number(s): "424200026"
Test #4:
score: 0
Accepted
time: 133ms
memory: 57652kb
input:
48 6713156
output:
198541581
result:
ok 1 number(s): "198541581"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
1 111
output:
6216
result:
ok 1 number(s): "6216"
Test #6:
score: 0
Accepted
time: 158ms
memory: 61668kb
input:
28 7304152
output:
457266679
result:
ok 1 number(s): "457266679"
Test #7:
score: 0
Accepted
time: 84ms
memory: 37440kb
input:
38 4101162
output:
232117382
result:
ok 1 number(s): "232117382"
Test #8:
score: 0
Accepted
time: 204ms
memory: 81004kb
input:
51 9921154
output:
340670552
result:
ok 1 number(s): "340670552"
Test #9:
score: 0
Accepted
time: 41ms
memory: 18616kb
input:
79 1801157
output:
620550406
result:
ok 1 number(s): "620550406"
Test #10:
score: 0
Accepted
time: 116ms
memory: 46208kb
input:
22 5417157
output:
457449071
result:
ok 1 number(s): "457449071"
Test #11:
score: 0
Accepted
time: 63ms
memory: 28804kb
input:
25 3210162
output:
36368303
result:
ok 1 number(s): "36368303"
Test #12:
score: 0
Accepted
time: 61ms
memory: 27304kb
input:
67 2919160
output:
935195555
result:
ok 1 number(s): "935195555"
Test #13:
score: 0
Accepted
time: 179ms
memory: 71140kb
input:
77 8613163
output:
482832472
result:
ok 1 number(s): "482832472"
Test #14:
score: 0
Accepted
time: 210ms
memory: 81620kb
input:
90 10000000
output:
275581651
result:
ok 1 number(s): "275581651"
Test #15:
score: 0
Accepted
time: 205ms
memory: 81728kb
input:
99 9999999
output:
126087169
result:
ok 1 number(s): "126087169"
Test #16:
score: 0
Accepted
time: 205ms
memory: 81736kb
input:
100 10000000
output:
451590067
result:
ok 1 number(s): "451590067"
Extra Test:
score: 0
Extra Test Passed