QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#616320 | #8023. The Journey of Geor Autumn | tkt0506 | TL | 139ms | 39752kb | C++14 | 1.4kb | 2024-10-06 01:44:17 | 2024-10-06 01:44:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353; // bruh
ll pow(ll x, ll y){
ll res = 1;
x = x%MOD;
while(y > 0){
if(y&1) res = (res*x)%MOD;
y = y >> 1;
x = (x*x)%MOD;
}
return res;
}
ll inv(ll x){
return pow(x, MOD-2);
}
vector<ll> fact{1}, invfact{1};
ll C(ll n, ll r){
if(n < r) return 0;
return ((fact[n]*invfact[r])%MOD*invfact[n-r])%MOD;
}
void precompute(ll n){
for(ll i = fact.size(); i<=n; i++){
fact.push_back((fact.back()*i)%MOD);
invfact.push_back(inv(fact.back()));
}
}
int main(){
ll n, k;
cin >> n >> k;
precompute(n);
vector<ll>dp(n+1,0), sum(n+1,0);
dp[0] = sum[0] = 1;
for(ll i=1; i<=n; i++){
dp[i] = sum[i-1];
if(i>k)dp[i] = (dp[i]-sum[i-k-1]+MOD)%MOD;
dp[i] *= fact[i-1];
dp[i] %= MOD;
sum[i] = (sum[i-1]+(dp[i]*invfact[i])%MOD)%MOD;
}
cout << dp[n];
}
/*
let dp[i] = valid permutation with len i
we can only place 1 on [1,k], assume we place on pos j
[1,j-1] can place any element
permutation = (i-1)C(j-1) * (j-1)!
for remain [j,n] part, it is actually a sub-problem
permutation = dp[i-j]
for(int j=0; j<=min(i,k); j++){
dp[i] = dp[i-j]*(i-1)C(j-1)*(j-1)!
}
dp[i-j]*(i-1)C(j-1)*(j-1)!
= dp[i-j]*(i-1)!/(i-j)!
= (i-1)! *dp[i-j]/(i-j)!
prefix sum store
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3524kb
input:
1 1
output:
1
result:
ok "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
1 2
output:
1
result:
ok "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3460kb
input:
1 3
output:
1
result:
ok "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3748kb
input:
1 4
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
2 1
output:
1
result:
ok "1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3756kb
input:
2 2
output:
2
result:
ok "2"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3716kb
input:
2 3
output:
2
result:
ok "2"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
2 4
output:
2
result:
ok "2"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
3 1
output:
1
result:
ok "1"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3780kb
input:
3 2
output:
4
result:
ok "4"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
3 3
output:
6
result:
ok "6"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
3 4
output:
6
result:
ok "6"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
4 1
output:
1
result:
ok "1"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3752kb
input:
4 2
output:
10
result:
ok "10"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3448kb
input:
4 3
output:
18
result:
ok "18"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
4 4
output:
24
result:
ok "24"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
99 50
output:
955866606
result:
ok "955866606"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
99 70
output:
296999003
result:
ok "296999003"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
1034 998
output:
637688669
result:
ok "637688669"
Test #20:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
1099 997
output:
712935289
result:
ok "712935289"
Test #21:
score: 0
Accepted
time: 2ms
memory: 3908kb
input:
10314 998
output:
224695890
result:
ok "224695890"
Test #22:
score: 0
Accepted
time: 2ms
memory: 3844kb
input:
10929 9974
output:
160291286
result:
ok "160291286"
Test #23:
score: 0
Accepted
time: 15ms
memory: 6488kb
input:
103124 99448
output:
695932649
result:
ok "695932649"
Test #24:
score: 0
Accepted
time: 16ms
memory: 6780kb
input:
109139 9937
output:
268916696
result:
ok "268916696"
Test #25:
score: 0
Accepted
time: 135ms
memory: 35380kb
input:
1031234 99238
output:
441457721
result:
ok "441457721"
Test #26:
score: 0
Accepted
time: 139ms
memory: 39752kb
input:
1091239 991237
output:
61047495
result:
ok "61047495"
Test #27:
score: -100
Time Limit Exceeded
input:
10000000 9982443