QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#300454#7792. Tree Topological Order Countingzyc07041945 61ms51020kbC++141.9kb2024-01-08 11:59:562024-01-08 11:59:57

Judging History

你现在查看的是最新测评结果

  • [2024-01-08 11:59:57]
  • 评测
  • 测评结果:45
  • 用时:61ms
  • 内存:51020kb
  • [2024-01-08 11:59:56]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
const int mod = 1e9 + 7;

inline int add(int x, int y) {x += y; return x >= mod ? x - mod : x;}
inline int del(int x, int y) {x -= y; return x < 0 ? x + mod : x;}
inline void Add(int &x, int y) {x = add(x, y);}
inline void Del(int &x, int y) {x = del(x, y);}

int n, fa[N], sz[N], dp[N][N], fac[N], inv[N], depth[N], a[N], pre[N];

inline int C(int x, int y) {return (x < 0 || y < 0 || x < y) ? 0 : 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod;}
inline int iC(int x, int y) {return 1ll * inv[x] * fac[y] % mod * fac[x - y] % mod;}
inline int qpow(int x, int y) {
    int res = 1;
    while (y) {
        if (y & 1) res = 1ll * res * x % mod;
        x = 1ll * x * x % mod;
        y >>= 1;
    }
    return res;
}

int main() {
    scanf("%d", &n); depth[1] = 1;
    fac[0] = 1;
    for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
    inv[n] = qpow(fac[n], mod - 2);
    for (int i = n; i >= 1; --i) inv[i - 1] = 1ll * inv[i] * i % mod;
    for (int i = 2; i <= n; ++i) scanf("%d", &fa[i]), depth[i] = depth[fa[i]] + 1;
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    for (int i = n; i >= 1; --i) sz[i]++, sz[fa[i]] += sz[i];
    dp[1][1] = fac[n];
    for (int i = 1; i <= n; ++i) dp[1][1] = 1ll * dp[1][1] * qpow(sz[i], mod - 2) % mod;
    for (int i = 2, res = 0; i <= n; ++i) {
        pre[depth[i] - 2] = 0;
        for (int j = depth[i] - 1; j <= n; ++j)
            pre[j] = add(pre[j - 1], 1ll * dp[fa[i]][j] * fac[n - j - sz[i]] % mod * inv[n - j] % mod);
        for (int j = depth[i]; j <= n; ++j)
            Add(dp[i][j], 1ll * pre[j - 1] * fac[n - j] % mod * inv[n - j - sz[i] + 1] % mod * sz[i] % mod);
    }
    for (int i = 1, res; i <= n; ++i) {
        res = 0;
        for (int j = 1; j <= n; ++j) Add(res, 1ll * dp[i][j] * a[j] % mod);
        printf("%d%c", res, i == n ? '\n' : ' ');
    }
    return 0;
}

详细

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 1ms
memory: 3696kb

input:

10
1 2 2 4 2 5 3 2 3
421487749 899442061 973943239 478653728 122912827 681073947 761973567 862016787 295035337 313094543

output:

132551152 750202542 844925059 844925059 320019016 837104208 346368542 333193779 837104208 333193779

result:

ok 10 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

10
1 2 2 4 5 4 3 7 1
226457597 222460848 126601784 27445914 811511381 52803670 776934531 832659037 955599897 927944188

output:

802913206 192357888 80729172 288305444 609011662 689888737 609011662 673713322 689888737 479113328

result:

ok 10 numbers

Subtask #2:

score: 10
Accepted

Dependency #1:

100%
Accepted

Test #3:

score: 10
Accepted
time: 0ms
memory: 3672kb

input:

20
1 1 1 4 2 5 2 5 4 6 4 7 11 11 2 9 7 2 9
305559508 68914843 154933697 736516347 250860247 264123902 632865608 32202124 861284820 505164728 564475479 136404892 645017283 837805203 802302363 50345521 511083407 292719502 356887812 390453540

output:

4420887 270322976 277088290 558845717 651457501 42292358 346897100 706626195 346897100 468004133 556186525 468004133 982694311 614012945 614012945 706626195 982694311 982694311 706626195 982694311

result:

ok 20 numbers

Test #4:

score: 0
Accepted
time: 1ms
memory: 3796kb

input:

20
1 1 2 1 3 4 4 4 6 10 2 12 4 8 14 9 7 16 15
691946539 440256927 968885281 472566141 20270022 183480725 684128023 668848432 366950038 421741447 88719369 679972163 897823307 376002875 147228549 89583310 930341312 627165235 998616162 46722211

output:

245904036 40491473 208122274 756122338 801039801 614916526 509450260 868456290 509450260 693529082 687591315 99754271 773178914 868456290 791438207 791438207 599551778 599551778 3608560 3608560

result:

ok 20 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 3732kb

input:

20
1 1 1 1 1 1 1 1 1 1 1 10 10 6 15 9 17 7 4
288280511 16879884 428044913 511311984 847245966 72822732 803411803 664704151 974477820 303440613 592210469 341084543 683099992 713223966 417890386 967156434 18771827 81816553 193725635 158295200

output:

207513772 350702711 350702711 146536641 350702711 195759251 146536641 350702711 195759251 195759251 350702711 350702711 428174441 428174441 48091421 808257461 48091421 808257461 554868781 554868781

result:

ok 20 numbers

Subtask #3:

score: 15
Accepted

Dependency #2:

100%
Accepted

Test #6:

score: 15
Accepted
time: 1ms
memory: 4160kb

input:

100
1 1 1 1 5 2 2 8 8 2 11 1 8 6 8 15 15 8 11 7 14 12 10 12 15 4 12 2 11 2 1 12 6 9 12 36 1 20 15 22 20 11 28 35 8 6 47 26 1 50 26 52 20 51 5 16 7 22 30 25 9 40 4 11 9 35 49 32 3 68 35 50 70 52 2 26 14 15 26 37 72 23 30 41 9 31 32 73 16 13 40 39 32 36 83 23 42 48 8
499370580 226348607 730860505 6961...

output:

378504482 152270968 588856846 588856846 451796857 507053458 485397893 343836957 802668753 637426492 386300892 166332977 516615074 434547603 230818994 362763740 197266323 197266323 619220872 369540656 981420869 809135769 398229189 601015252 83277609 749643676 903903231 83277609 816079877 802671417 29...

result:

ok 100 numbers

Test #7:

score: 0
Accepted
time: 0ms
memory: 4340kb

input:

100
1 2 3 4 3 3 7 8 9 10 10 12 12 10 7 16 16 18 19 20 21 21 20 24 24 26 27 19 29 30 31 18 33 34 35 36 34 38 39 40 41 42 43 42 42 41 39 48 49 48 48 52 53 38 55 18 57 58 59 60 58 62 63 64 65 66 65 68 69 70 70 65 73 74 64 76 77 78 78 80 78 78 83 84 76 86 62 88 89 90 89 57 93 94 95 95 97 7 3 577441326 8...

output:

24745333 332372584 221230837 905244609 871647923 888446266 767512902 623574378 656235660 415629913 849746614 719923753 414658041 414658041 849746614 402205168 526141339 849958506 971879651 443767080 822469942 831691587 831691587 58824397 437732695 467938249 631371835 213888001 566609539 751780666 27...

result:

ok 100 numbers

Test #8:

score: 0
Accepted
time: 0ms
memory: 4052kb

input:

100
1 1 1 2 5 1 5 6 6 3 6 5 13 14 15 16 6 17 19 1 3 20 3 19 23 1 21 28 26 22 30 27 32 29 31 34 10 11 12 37 36 41 43 21 44 46 29 47 49 22 50 52 53 42 16 54 57 35 27 58 61 17 22 30 62 59 66 19 14 55 56 68 67 74 75 76 73 78 77 79 80 42 82 81 85 86 87 71 88 84 91 12 92 89 94 90 37 54 89
605221076 121774...

output:

191199508 4617006 808901507 566404623 124140965 162213750 566404623 864721314 393650962 90732392 258608983 518925993 633131612 162920052 325151707 741164232 456177120 393650962 156411701 632463088 874398001 118929519 953485256 16904827 758670923 629168678 312375353 29155254 13159381 848358851 402682...

result:

ok 100 numbers

Test #9:

score: 0
Accepted
time: 1ms
memory: 4096kb

input:

100
1 1 2 4 5 3 7 6 6 10 6 11 13 6 14 9 16 8 18 20 21 20 19 14 22 26 24 7 8 28 27 32 31 28 34 36 33 38 39 40 41 37 42 44 45 46 47 48 43 13 49 52 53 50 45 54 57 55 59 58 61 31 30 50 60 62 18 68 67 70 22 7 73 74 20 71 77 78 79 80 5 81 66 83 85 16 35 71 86 33 90 84 92 94 95 75 93 51 99
167324337 235996...

output:

960069110 565410671 378415579 83061265 251033682 837557317 949670974 227661063 135094654 196352221 204104597 467559995 962078051 500880733 467559995 820376189 800025336 137849548 861007463 253383929 672706993 444274725 686270519 26681153 853944822 838947777 356454261 823074879 449754211 242297513 79...

result:

ok 100 numbers

Test #10:

score: 0
Accepted
time: 1ms
memory: 4108kb

input:

100
1 2 2 3 3 4 4 5 1 10 10 11 11 12 12 13 1 18 18 19 19 20 20 21 1 26 26 27 27 28 28 29 1 34 34 35 35 36 36 37 1 42 42 43 43 44 44 45 1 50 50 51 51 52 52 53 1 58 58 59 59 60 60 61 35 37 53 33 38 12 47 4 12 31 39 43 71 28 3 44 26 4 56 10 36 23 48 75 23 33 64 37 61 58 72 6 19 80 76
624084541 43803720...

output:

945263113 879084197 191295526 536463760 604811148 604811148 247555643 247555643 910543971 917658385 625486473 820518005 529128244 491159688 165586058 165586058 453191132 65531338 567934672 567934672 188036476 316886889 299902261 316886889 445737302 586518136 331015734 331015734 70230621 250098181 40...

result:

ok 100 numbers

Test #11:

score: 0
Accepted
time: 1ms
memory: 4092kb

input:

100
1 2 3 4 5 6 7 8 8 9 9 10 10 1 15 16 17 18 19 20 21 21 22 22 23 23 1 28 29 30 31 32 33 34 34 35 35 36 36 1 41 42 43 44 45 46 47 47 48 48 49 49 1 54 55 56 57 58 59 60 60 61 61 62 62 1 67 68 69 70 71 72 73 73 74 74 75 75 1 80 81 82 83 84 85 86 86 87 87 88 88 69 92 58 43 66 60 77 52
390699613 651233...

output:

65621655 812092562 932235668 4003220 261644586 935856346 722411218 785121698 535820036 535820036 416920411 416920411 416920411 416920411 812092562 932235668 4003220 261644586 935856346 722411218 785121698 535820036 535820036 416920411 416920411 416920411 416920411 812092562 932235668 4003220 2616445...

result:

ok 100 numbers

Test #12:

score: 0
Accepted
time: 1ms
memory: 4092kb

input:

100
1 2 3 3 3 3 2 2 9 1 4 4 2 7 13 5 5 8 17 16 12 16 10 6 11 24 16 23 25 12 24 16 22 26 34 30 19 23 24 38 25 31 30 28 1 37 41 45 43 15 47 37 42 39 27 53 28 13 43 53 27 53 32 62 60 2 26 14 14 14 58 22 7 5 69 34 28 54 32 14 1 77 37 31 19 35 28 25 15 59 20 39 73 60 70 15 88 75 23
746243501 189811510 61...

output:

598979168 872241254 50503028 74493216 383279680 569030711 148205445 957505231 371091216 128352247 791181979 82141350 404645980 135863973 283785015 732518603 793451753 976035964 897131493 436880141 157275854 157217864 445433968 636821036 727813431 691029440 742701790 572299015 499644234 203584758 157...

result:

ok 100 numbers

Test #13:

score: 0
Accepted
time: 1ms
memory: 4088kb

input:

100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 52 39 57 88 20 18 42 96 23 95
203758586 574528415 435665491 34474532 638409977 850247931 218570973 588307431 581460307...

output:

669437192 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 513943159 193584960 513943159 193584960 513943159 513943159 496316739 513943159 513943159 513943159 513943159 513943159 513943159 513943159 ...

result:

ok 100 numbers

Subtask #4:

score: 15
Accepted

Dependency #3:

100%
Accepted

Test #14:

score: 15
Accepted
time: 0ms
memory: 5528kb

input:

350
1 2 2 3 4 1 4 5 4 3 10 4 9 2 1 14 17 5 18 14 12 18 12 21 22 3 9 12 20 19 14 13 29 18 16 16 4 7 3 4 38 4 3 43 36 37 44 1 6 14 40 49 38 46 28 55 42 48 54 21 60 5 35 36 7 1 13 56 9 56 34 6 50 25 37 26 28 26 16 69 54 2 80 18 26 24 47 1 28 15 54 64 69 77 27 90 39 11 58 25 34 48 18 71 63 10 48 64 12 8...

output:

725457781 985623070 549707882 708594727 328876909 317926772 563675054 233391385 624039021 803646801 298937586 524128692 318027847 920041380 86020600 999877879 723757052 769897066 948867117 147083007 167157273 132987474 224599204 684173661 510745536 932881263 631107585 992223949 941708044 40102442 13...

result:

ok 350 numbers

Test #15:

score: 0
Accepted
time: 3ms
memory: 5740kb

input:

350
1 2 3 4 4 6 7 8 8 10 8 12 13 14 15 14 17 17 19 20 20 19 19 8 25 26 27 28 28 30 31 28 33 27 35 3 37 38 39 40 41 41 43 44 45 46 47 48 49 50 51 50 53 54 55 56 57 56 59 60 61 62 63 64 59 66 55 68 69 70 70 68 73 74 75 76 77 78 79 80 79 82 83 84 84 86 87 88 89 88 91 92 92 87 95 95 97 95 99 100 101 100...

output:

188958962 838018032 694894789 823120892 617644783 530274027 200969947 240826756 613389912 179693701 47086116 672985728 107813167 154260189 293594073 914207880 621041278 601452362 721957774 10662830 660695503 660695503 777351281 777351281 860280148 499574250 911926383 445146999 511406743 805488190 81...

result:

ok 350 numbers

Test #16:

score: 0
Accepted
time: 3ms
memory: 5548kb

input:

350
1 1 1 2 1 5 1 4 8 10 11 12 5 7 13 7 15 5 18 16 20 20 21 24 25 13 22 5 3 26 6 30 32 28 2 17 35 38 39 34 15 33 34 31 8 45 47 6 33 48 20 38 44 14 40 51 57 58 21 59 61 62 63 3 56 56 24 11 67 64 43 71 42 73 75 60 60 70 57 76 81 79 12 82 29 35 85 83 88 58 88 92 93 94 89 95 97 98 48 99 96 65 54 3 104 1...

output:

438897019 541316843 201876893 85374434 9104378 420007743 966687576 870883857 675222175 214716492 250600570 57960264 632987803 612496263 3227598 750983699 900689888 944006702 298215074 512176985 862670964 171318357 774442976 288733555 854690910 645160240 87885048 589713021 902647307 703952580 5232017...

result:

ok 350 numbers

Test #17:

score: 0
Accepted
time: 3ms
memory: 5716kb

input:

350
1 1 3 4 5 1 2 2 9 10 11 4 3 6 15 15 13 17 19 7 20 1 22 24 25 21 27 25 28 9 26 32 30 33 35 34 36 37 38 19 41 42 43 44 45 46 47 48 49 49 51 51 53 45 55 43 57 43 59 60 61 62 63 63 65 65 67 65 62 70 71 61 73 74 73 76 76 61 79 80 40 82 83 84 85 20 86 88 89 22 77 87 90 94 95 96 97 98 39 100 99 102 103...

output:

898527269 482984035 686126059 557584585 675389346 420572461 672969385 110990278 400363869 875184105 362664271 839931847 964530344 273367959 210515998 660292792 368931832 633266578 641794985 465418933 977524707 536041134 818989589 337536438 119143856 956853355 436911204 170099986 324969275 20107466 5...

result:

ok 350 numbers

Test #18:

score: 0
Accepted
time: 0ms
memory: 5476kb

input:

350
1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 17 17 18 18 19 19 20 20 21 21 22 22 23 23 1 32 32 33 33 34 34 35 35 36 36 37 37 38 38 1 47 47 48 48 49 49 50 50 51 51 52 52 53 53 1 62 62 63 63 64 64 65 65 66 66 67 67 68 68 1 77 77 78 78 79 79 80 80 81 81 82 82 83 83 1 92 92 93 93 94 94 95 95 96 96 97 97 98 98 1 ...

output:

48923942 23270472 101399992 863794196 357111863 357111863 629259141 359151893 241404561 478445221 530122833 478445221 804943283 443382868 824838671 757685379 382912364 429284275 429284275 55165442 356075503 52631158 356075503 666305018 421620803 445742475 866780580 580229256 580229256 681536014 8667...

result:

ok 350 numbers

Test #19:

score: 0
Accepted
time: 0ms
memory: 5532kb

input:

350
1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 15 16 16 17 17 18 18 19 19 1 27 28 29 30 31 32 33 34 35 36 37 38 39 39 40 40 41 41 42 42 43 43 44 44 1 52 53 54 55 56 57 58 59 60 61 62 63 64 64 65 65 66 66 67 67 68 68 69 69 1 77 78 79 80 81 82 83 84 85 86 87 88 89 89 90 90 91 91 92 92 93 93 94 94 1 102 10...

output:

883509303 979582152 230506070 603418923 629288117 91202416 175046645 406546458 234248100 820358756 149965837 874704455 908347504 75898074 68078909 648992377 569849990 569849990 913524544 975036339 390253478 390253478 390253478 390253478 505792233 505792233 979582152 230506070 603418923 629288117 912...

result:

ok 350 numbers

Test #20:

score: 0
Accepted
time: 3ms
memory: 5464kb

input:

350
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 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64...

output:

628936700 683760690 193765126 909619921 346017938 876319429 673543524 496433333 749368867 357631037 164329002 253087228 78140650 210017699 315466616 602875574 537134073 821763142 210696656 878230841 786231942 997041178 516917338 841390659 975607427 885621439 62184466 85620355 135353876 533113271 843...

result:

ok 350 numbers

Test #21:

score: 0
Accepted
time: 0ms
memory: 5620kb

input:

350
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

556123465 527303785 527303785 527303785 363819617 144927891 527303785 767804502 527303785 527303785 363819617 527303785 363819617 767804502 527303785 527303785 527303785 767804502 527303785 527303785 527303785 527303785 363819617 767804502 527303785 527303785 144927891 767804502 363819617 929215879 ...

result:

ok 350 numbers

Subtask #5:

score: 0
Wrong Answer

Test #22:

score: 15
Accepted
time: 59ms
memory: 51020kb

input:

3000
1 1 3 1 3 6 1 5 3 2 4 9 6 7 2 7 1 1 8 1 4 17 23 2 5 24 15 12 14 28 16 32 33 16 6 1 3 12 17 31 33 19 43 3 33 7 35 42 23 15 30 12 8 21 16 38 53 8 49 56 21 25 30 54 30 14 20 10 35 28 35 55 12 50 10 1 75 76 19 22 8 82 4 68 42 9 57 68 3 67 56 8 11 23 72 68 9 62 32 20 73 39 74 56 88 61 83 78 69 29 29...

output:

16671810 944499931 273506025 607998113 446862295 158970004 442911964 756220225 307986370 536953966 336880452 789379781 300103944 158249160 867664998 83631453 17442425 491509604 588320809 724419565 130536659 790581701 659232050 490335021 392240369 593172088 328309069 347269844 39252086 682905115 5195...

result:

ok 3000 numbers

Test #23:

score: -15
Wrong Answer
time: 61ms
memory: 43936kb

input:

3000
1 1 3 4 4 5 7 8 8 10 3 11 13 14 15 11 16 18 2 3 18 22 4 23 25 26 27 28 29 30 31 32 8 33 35 29 36 38 39 40 18 41 43 44 45 46 47 7 48 50 51 52 9 53 55 16 56 12 58 60 6 49 61 64 65 66 67 68 69 70 71 72 73 74 75 62 76 78 79 80 81 4 82 84 85 86 54 87 89 90 91 92 38 93 95 58 96 98 72 99 101 91 95 102...

output:

44908844 990215062 3710810 12890070 597262724 792637796 851285243 749615395 198435360 845302810 55398266 472399085 605110209 702259509 578607766 166983407 564554453 111303827 203160198 935521273 524591671 317017983 417293658 834238757 381505035 69579981 398729156 243025440 713600523 974953983 588504...

result:

wrong answer 3rd numbers differ - expected: '623923044', found: '3710810'

Subtask #6:

score: 0
Skipped

Dependency #4:

100%
Accepted

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #6:

0%