QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#711858 | #2472. Counting Haybales | _8_8_# | 0 | 27ms | 14068kb | C++23 | 2.7kb | 2024-11-05 13:47:45 | 2024-11-05 13:47:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e3 + 12, MOD = (int)1e9 + 7;
int n, h[N], k;
ll binpow(ll a, ll b) {
ll ret = 1, k = a;
while(b) {
if(b & 1) {
ret *= k;
ret %= MOD;
}
k *= k;
k %= MOD;
b /= 2;
}
return ret;
}
ll fact[N], fact1[N];
void init() {
fact[0] = fact1[0] = 1;
for(int i = 1; i < N; i++) {
fact[i] = fact[i - 1] * 1ll * i % MOD;
fact1[i] = binpow(fact[i], MOD - 2);
}
}
ll ceis(int n, int k) {
if(k > n) return 0;
ll ob = fact1[k] * fact1[n - k] % MOD;
return fact[n] * ob % MOD;
}
void make() {
for(int i = 1; i <= n; i++) {
int l, r, f;
l = r = h[i];
f = i;
for(int j = i + 1; j <= n; j++) {
if(h[j] < h[f]) {
if(abs(l - h[j]) <= 1 && abs(r - h[j]) <= 1) {
f = j;
}
}
l = min(l, h[j]);
r = max(r, h[j]);
}
while(f != i) {
swap(h[f], h[f - 1]);
--f;
}
}
}
int dp[N][N];
void add(int &x, int y) {
x += y;
if(x >= MOD) x -= MOD;
}
int solve(vector<int> a) {
vector<int> b;
int m = (int)a.size();
for(int i = 0; i < m; ) {
int j = i + 1;
while(j < m && a[j] == a[j - 1]) {
j++;
}
b.push_back(j - i);
i = j;
}
m = (int)b.size();
dp[0][b[0]] = 1;
for(int i = 0; i < b[0]; i++) {
dp[0][i] = 0;
}
for(int i = 1; i < m; i++) {
for(int j = 0; j <= b[i]; j++) {
dp[i][j] = 0;
for(int l = 0; l <= b[i - 1]; l++) {
if(j != b[i] && !l) continue;
add(dp[i][j], dp[i - 1][l] * 1ll * ceis(b[i] - j + l - 1, b[i] - j) % MOD);
}
}
}
int ret = 0;
for(int f = 0; f < m; f++) {
for(int i = 0; i <= b[f]; i++) {
add(ret, dp[f][i]);
}
}
ret--;
if(ret < 0) ret += MOD;
return ret;
}
void test() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> h[i];
}
make();
int res = 1;
for(int i = 1; i <= n; ) {
int j = i + 1;
vector<int> a(1, h[i]);
while(j <= n && h[j] <= h[j - 1] + 1) {
a.push_back(h[j]);
j++;
}
res = res * 1ll * solve(a) % MOD;
i = j;
}
cout << res << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
init();
int t = 1;
cin >> t;
while(t--) {
test();
}
}
详细
Pretests
Final Tests
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3808kb
input:
7 4 2 2 2 3 4 3 3 1 2 4 5 3 4 2 6 3 3 1 1 2 2 6 1 3 3 4 1 2 6 4 1 2 3 5 4 10 1 5 6 6 6 4 2 3 2 5
output:
4 8 6 18 0 10 0
result:
wrong answer 2nd lines differ - expected: '4', found: '8'
Test #2:
score: 0
Wrong Answer
time: 0ms
memory: 5732kb
input:
10 10 447773962 773442532 122816 137572579 324627123 157577940 253498609 99147813 425825313 199995380 10 416515986 416515986 416515987 416515987 416515988 416515988 416515989 416515989 416515988 416515989 10 563229302 563229301 563229301 563229302 563229301 563229300 563229300 563229301 563229302 56...
output:
0 144 0 131 60 0 150 78 120 70
result:
wrong answer 1st lines differ - expected: '1', found: '0'
Test #3:
score: 0
Wrong Answer
time: 1ms
memory: 3700kb
input:
10 10 468145963 198730372 27838076 590195590 467423861 520495379 451366491 344173378 354694313 165814381 10 219739800 219739801 219739801 219739800 219739799 219739799 219739798 219739798 219739798 219739799 10 568161994 568161995 568161994 568161994 568161994 568161994 568161993 568161994 568161995...
output:
64 144 0 11 26 62 252 55 136 276
result:
wrong answer 1st lines differ - expected: '1', found: '64'
Test #4:
score: 0
Wrong Answer
time: 16ms
memory: 3744kb
input:
1 5000 2 1 3 3 1 1 2 2 3 2 3 1 1 1 1 1 1 1 2 3 1 1 2 2 2 1 1 2 3 2 2 3 1 3 2 3 1 2 2 2 1 1 3 2 2 1 1 2 2 3 3 3 1 1 1 3 2 3 3 1 3 1 3 1 3 3 3 2 2 2 1 2 2 3 2 3 2 1 1 1 2 2 2 3 1 2 1 1 2 2 3 2 2 3 1 3 1 2 2 1 1 3 1 1 2 3 1 3 2 2 2 2 3 2 3 2 3 3 3 1 2 3 3 2 2 2 2 3 1 2 2 1 1 1 1 3 3 1 1 1 3 2 2 1 3 2 2...
output:
0
result:
wrong answer 1st lines differ - expected: '493836655', found: '0'
Test #5:
score: 0
Wrong Answer
time: 4ms
memory: 14068kb
input:
10 500 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1...
output:
998 0 0 0 0 0 0 0 0 0
result:
wrong answer 1st lines differ - expected: '523068127', found: '998'
Test #6:
score: 0
Wrong Answer
time: 0ms
memory: 14048kb
input:
10 500 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...
output:
998 0 0 0 0 0 0 0 0 0
result:
wrong answer 1st lines differ - expected: '523068127', found: '998'
Test #7:
score: 0
Wrong Answer
time: 12ms
memory: 3996kb
input:
1 5000 2 3 2 3 6 5 6 8 8 11 11 11 13 14 15 16 17 18 19 21 21 23 24 24 25 25 27 27 29 31 32 33 34 34 35 37 38 39 40 40 40 43 44 43 46 46 48 48 50 50 51 52 52 53 56 55 57 57 58 61 62 62 64 64 64 67 68 69 69 71 72 73 74 75 75 77 76 77 79 81 82 82 84 84 85 87 88 88 89 91 91 93 94 93 94 97 97 97 100 101 ...
output:
0
result:
wrong answer 1st lines differ - expected: '290956746', found: '0'
Test #8:
score: 0
Wrong Answer
time: 1ms
memory: 3756kb
input:
10 100 1 1 4 2 2 4 3 2 4 2 3 2 3 1 1 2 2 1 2 3 3 4 4 2 2 2 1 3 2 3 4 3 1 2 2 4 4 2 1 4 3 2 4 4 1 1 3 4 4 4 1 2 3 2 4 2 3 1 2 1 1 2 2 3 2 4 2 2 2 2 4 2 4 4 2 1 3 1 2 1 2 3 2 2 3 2 2 4 2 2 3 2 2 4 4 4 1 2 3 2 100 2 3 2 3 3 3 3 2 2 2 1 3 1 2 3 4 3 3 3 3 4 2 3 4 1 1 2 3 3 3 1 4 4 1 4 2 4 3 2 3 4 4 1 3 2...
output:
0 482162404 0 0 644325541 0 191006757 889374014 1734015 0
result:
wrong answer 1st lines differ - expected: '762655378', found: '0'
Test #9:
score: 0
Wrong Answer
time: 1ms
memory: 3892kb
input:
10 100 4 1 4 1 4 2 3 4 1 2 1 3 2 4 2 2 3 4 2 4 3 1 3 3 4 2 3 4 3 1 2 2 2 2 4 4 2 1 1 2 1 2 4 3 1 2 2 1 1 2 1 3 2 4 4 3 2 3 4 1 1 2 2 2 2 3 4 3 4 2 2 1 2 1 2 3 4 1 3 2 1 3 3 1 4 3 1 1 2 4 4 1 4 3 3 2 1 1 2 2 100 1 2 3 2 1 4 1 4 3 3 1 2 3 3 4 3 4 2 2 3 3 2 3 3 3 3 2 2 1 1 1 1 2 2 2 4 1 4 3 1 3 3 3 4 2...
output:
489686469 216242631 149556728 0 0 953515462 0 0 0 863456744
result:
wrong answer 1st lines differ - expected: '46441250', found: '489686469'
Test #10:
score: 0
Wrong Answer
time: 1ms
memory: 3700kb
input:
10 100 1 2 3 2 2 1 1 2 1 1 1 4 2 1 1 1 1 2 2 2 1 2 4 2 4 3 1 4 3 3 3 1 4 1 3 4 3 3 3 4 2 3 3 3 4 3 2 4 4 2 4 4 4 1 1 1 3 1 4 2 3 1 4 4 4 4 1 3 1 3 4 4 4 1 4 3 2 1 2 1 3 2 2 3 2 1 2 4 4 4 1 4 3 2 3 2 4 2 1 4 100 3 1 3 1 2 2 1 2 1 3 1 1 4 1 1 1 1 1 2 3 2 4 3 2 4 1 2 4 4 2 1 1 2 4 1 4 2 1 2 1 2 3 2 2 1...
output:
0 421847748 0 970560858 0 0 647885557 832131450 443542049 425582313
result:
wrong answer 1st lines differ - expected: '621199694', found: '0'
Test #11:
score: 0
Wrong Answer
time: 1ms
memory: 3604kb
input:
10 100 8 3 1 5 7 5 8 9 5 6 3 1 2 8 8 3 2 3 1 2 7 7 10 9 1 4 6 9 9 2 7 1 8 9 3 8 10 6 8 2 6 5 2 8 4 6 7 2 5 9 5 2 7 3 6 1 7 1 7 8 9 1 6 7 10 4 4 6 1 4 4 5 2 1 9 9 1 5 7 2 6 8 9 2 2 8 9 9 4 4 8 6 3 9 9 10 4 5 2 9 100 4 3 1 2 9 9 2 7 4 6 10 5 10 4 5 6 9 2 3 2 6 4 10 7 6 4 6 1 1 6 4 2 1 1 4 3 4 7 8 3 1 ...
output:
0 0 0 0 0 0 0 0 0 0
result:
wrong answer 1st lines differ - expected: '2488320', found: '0'
Test #12:
score: 0
Wrong Answer
time: 0ms
memory: 3944kb
input:
10 100 193563111 193563110 193563109 193563108 193563109 193563108 193563109 193563109 193563110 193563111 193563110 193563110 193563109 193563111 193563110 193563109 193563108 193563110 193563109 193563107 193563108 193563110 193563110 193563110 193563109 193563110 193563108 193563107 193563106 193...
output:
251880754 0 209078544 181673361 253859497 0 320509008 713539022 5001 742404204
result:
wrong answer 1st lines differ - expected: '725199468', found: '251880754'
Test #13:
score: 0
Wrong Answer
time: 1ms
memory: 3948kb
input:
10 100 165531093 165531092 165531090 165531090 165531088 165531089 165531089 165531090 165531091 165531090 165531090 165531091 165531089 165531089 165531089 165531087 165531088 165531088 165531086 165531086 165531085 165531084 165531084 165531083 165531084 165531083 165531084 165531084 165531085 165...
output:
921266218 13796369 35357821 318255244 43848940 382289091 966578998 137205613 723302395 166873797
result:
wrong answer 1st lines differ - expected: '977126526', found: '921266218'
Test #14:
score: 0
Wrong Answer
time: 6ms
memory: 5836kb
input:
5 1000 835051605 835051606 835051607 835051608 835051609 835051609 835051608 835051608 835051609 835051610 835051609 835051609 835051609 835051610 835051610 835051609 835051609 835051608 835051607 835051607 835051606 835051606 835051606 835051606 835051605 835051606 835051606 835051606 835051604 835...
output:
0 0 63211581 0 0
result:
wrong answer 1st lines differ - expected: '93384863', found: '0'
Test #15:
score: 0
Wrong Answer
time: 6ms
memory: 3716kb
input:
5 1000 551842461 551842460 551842459 551842459 551842457 551842457 551842459 551842458 551842456 551842455 551842456 551842458 551842459 551842457 551842457 551842458 551842456 551842456 551842455 551842454 551842452 551842451 551842453 551842451 551842453 551842453 551842453 551842454 551842453 551...
output:
0 783555656 299389274 801293956 189433890
result:
wrong answer 1st lines differ - expected: '167959558', found: '0'
Test #16:
score: 0
Wrong Answer
time: 7ms
memory: 3828kb
input:
5 1000 911411059 911411057 911411057 911411055 911411055 911411053 911411054 911411055 911411056 911411055 911411055 911411055 911411054 911411055 911411056 911411058 911411058 911411058 911411057 911411058 911411059 911411059 911411060 911411060 911411060 911411059 911411059 911411059 911411060 911...
output:
0 132258874 43769748 508683902 229257102
result:
wrong answer 1st lines differ - expected: '749921974', found: '0'
Test #17:
score: 0
Wrong Answer
time: 8ms
memory: 5968kb
input:
5 1000 239756971 239756970 239756971 239756970 239756969 239756970 239756969 239756970 239756969 239756968 239756969 239756968 239756969 239756969 239756968 239756967 239756966 239756966 239756967 239756966 239756966 239756966 239756966 239756966 239756967 239756965 239756966 239756967 239756967 239...
output:
4285436 720943336 452620577 425431808 451682286
result:
wrong answer 1st lines differ - expected: '205752624', found: '4285436'
Test #18:
score: 0
Wrong Answer
time: 14ms
memory: 3780kb
input:
1 5000 316394141 316394140 316394140 316394141 316394141 316394141 316394141 316394140 316394140 316394141 316394140 316394141 316394141 316394140 316394141 316394141 316394141 316394140 316394140 316394139 316394140 316394139 316394139 316394139 316394139 316394140 316394140 316394140 316394139 316...
output:
879833239
result:
wrong answer 1st lines differ - expected: '651398981', found: '879833239'
Test #19:
score: 0
Wrong Answer
time: 21ms
memory: 3980kb
input:
1 5000 698334028 698334028 698334027 698334028 698334028 698334028 698334028 698334027 698334027 698334028 698334028 698334028 698334027 698334027 698334027 698334028 698334028 698334027 698334028 698334028 698334027 698334028 698334028 698334027 698334028 698334027 698334028 698334027 698334028 698...
output:
588758410
result:
wrong answer 1st lines differ - expected: '192833632', found: '588758410'
Test #20:
score: 0
Wrong Answer
time: 22ms
memory: 3660kb
input:
1 5000 104725913 104725912 104725912 104725912 104725912 104725913 104725913 104725913 104725913 104725912 104725913 104725913 104725912 104725912 104725912 104725912 104725913 104725912 104725913 104725912 104725913 104725913 104725913 104725913 104725913 104725912 104725913 104725912 104725913 104...
output:
464289173
result:
wrong answer 1st lines differ - expected: '433813305', found: '464289173'
Test #21:
score: 0
Wrong Answer
time: 27ms
memory: 3796kb
input:
1 5000 631500634 631500634 631500634 631500633 631500634 631500633 631500634 631500634 631500633 631500633 631500634 631500634 631500634 631500634 631500633 631500633 631500635 631500634 631500634 631500634 631500635 631500635 631500634 631500634 631500634 631500634 631500635 631500634 631500634 631...
output:
0
result:
wrong answer 1st lines differ - expected: '218954931', found: '0'