QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#645479 | #3840. Pass the Ball! | lllei# | WA | 82ms | 11436kb | C++20 | 4.8kb | 2024-10-16 18:38:46 | 2024-10-16 18:38:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int BS = 400;
long long mod = 1e18;
namespace Math
{
inline int pw(int base, int p, const int mod)
{
static int res;
for (res = 1; p; p >>= 1, base = static_cast<long long>(base) * base % mod)
if (p & 1)
res = static_cast<long long>(res) * base % mod;
return res;
}
inline int inv(int x, const int mod) { return pw(x, mod - 2, mod); }
}
const int mod1 = 998244353, mod2 = 1004535809, mod3 = 469762049, G = 3;
const long long mod_1_2 = static_cast<long long>(mod1) * mod2;
const int inv_1 = Math::inv(mod1, mod2), inv_2 = Math::inv(mod_1_2 % mod3, mod3);
struct Int
{
int A, B, C;
explicit inline Int() {}
explicit inline Int(int __num) : A(__num), B(__num), C(__num) {}
explicit inline Int(int __A, int __B, int __C) : A(__A), B(__B), C(__C) {}
static inline Int reduce(const Int &x)
{
return Int(x.A + (x.A >> 31 & mod1), x.B + (x.B >> 31 & mod2), x.C + (x.C >> 31 & mod3));
}
inline friend Int operator+(const Int &lhs, const Int &rhs)
{
return reduce(Int(lhs.A + rhs.A - mod1, lhs.B + rhs.B - mod2, lhs.C + rhs.C - mod3));
}
inline friend Int operator-(const Int &lhs, const Int &rhs)
{
return reduce(Int(lhs.A - rhs.A, lhs.B - rhs.B, lhs.C - rhs.C));
}
inline friend Int operator*(const Int &lhs, const Int &rhs)
{
return Int(static_cast<long long>(lhs.A) * rhs.A % mod1, static_cast<long long>(lhs.B) * rhs.B % mod2, static_cast<long long>(lhs.C) * rhs.C % mod3);
}
inline int get()
{
long long x = static_cast<long long>(B - A + mod2) % mod2 * inv_1 % mod2 * mod1 + A;
return (static_cast<long long>(C - x % mod3 + mod3) % mod3 * inv_2 % mod3 * (mod_1_2 % mod) % mod + x) % mod;
}
};
#define maxn 500005
#define N (maxn << 1)
namespace Poly
{
int lim, s, rev[N];
Int Wn[N | 1];
inline void init(int n)
{
s = -1, lim = 1;
while (lim < n)
lim <<= 1, ++s;
for (int i = 1; i < lim; ++i)
rev[i] = rev[i >> 1] >> 1 | (i & 1) << s;
const Int t(Math::pw(G, (mod1 - 1) / lim, mod1), Math::pw(G, (mod2 - 1) / lim, mod2), Math::pw(G, (mod3 - 1) / lim, mod3));
*Wn = Int(1);
for (Int *i = Wn; i != Wn + lim; ++i)
*(i + 1) = *i * t;
}
inline void NTT(Int *A, const int op = 1)
{
for (int i = 1; i < lim; ++i)
if (i < rev[i])
swap(A[i], A[rev[i]]);
for (int mid = 1; mid < lim; mid <<= 1)
{
const int t = lim / mid >> 1;
for (int i = 0; i < lim; i += mid << 1)
{
for (int j = 0; j < mid; ++j)
{
const Int W = op ? Wn[t * j] : Wn[lim - t * j];
const Int X = A[i + j], Y = A[i + j + mid] * W;
A[i + j] = X + Y, A[i + j + mid] = X - Y;
}
}
}
if (!op)
{
const Int ilim(Math::inv(lim, mod1), Math::inv(lim, mod2), Math::inv(lim, mod3));
for (Int *i = A; i != A + lim; ++i)
*i = (*i) * ilim;
}
}
#undef N
}
Int A[1000005], B[1000005];
int n,q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>q;
vector<int>to(n+1);
vector<int>vis(n+1,0);
vector<vector<long long>>b;
vector<vector<long long>>c(BS,vector<long long>(BS));
for(int i=1;i<=n;i++)cin>>to[i];
for(int i=1;i<=n;i++)
if(!vis[i])
{
vector<int>tmp;
for(int j=i;!vis[j];j=to[j])
{
vis[j]=1;
tmp.push_back(j);
}
if(tmp.size()<BS)
{
int siz=tmp.size();
for(int j=0;j<siz;j++)
for(int k=0;k<siz;k++)
{
int t=(k<j?k+siz-j:k-j);
c[siz][t]+=tmp[j]*1ll*tmp[k];
}
}
else
{
vector<long long>ss;
int siz=tmp.size();
Poly::init(3*siz);
for(int j=0;j<Poly::lim;j++)A[j]=B[j]=Int(0);
for(int j=0;j<siz;j++)A[j]=Int(tmp[j]);
for(int j=siz;j<2*siz;j++)A[j]=Int(tmp[j-siz]);
for(int j=0;j<siz;j++)B[j]=Int(tmp[siz-1-j]);
Poly::NTT(A),Poly::NTT(B);
for(int j=0;j<Poly::lim;j++)A[j]=A[j]*B[j];
Poly::NTT(A,0);
for(int j=siz-1;j<2*siz-1;j++)ss.push_back(A[j].get());
b.push_back(ss);
}
}
while(q--)
{
long long ans=0;
int k;
cin>>k;
for(int i=1;i<BS;i++)ans+=c[i][k%i];
for(auto &i:b)ans+=i[k%i.size()];
cout<<ans<<'\n';
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4452kb
input:
4 4 2 4 1 3 1 2 3 4
output:
25 20 25 30
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 4372kb
input:
3 6 2 3 1 1 2 3 999999998 999999999 1000000000
output:
11 11 14 11 14 11
result:
ok 6 lines
Test #3:
score: 0
Accepted
time: 1ms
memory: 4360kb
input:
3 6 3 1 2 1 2 3 999999998 999999999 1000000000
output:
11 11 14 11 14 11
result:
ok 6 lines
Test #4:
score: 0
Accepted
time: 7ms
memory: 8500kb
input:
1000 10000 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...
output:
333334000 332835500 332338000 331841500 331346000 330851500 330358000 329865500 329374000 328883500 328394000 327905500 327418000 326931500 326446000 325961500 325478000 324995500 324514000 324033500 323554000 323075500 322598000 322121500 321646000 321171500 320698000 320225500 319754000 319283500 ...
result:
ok 10000 lines
Test #5:
score: 0
Accepted
time: 7ms
memory: 8556kb
input:
1000 10000 187 493 316 665 124 40 448 649 657 65 438 730 816 107 789 286 309 469 169 216 488 52 212 111 541 83 990 48 282 867 36 220 676 241 959 372 322 244 481 708 595 957 215 223 120 658 291 176 229 158 431 492 221 986 889 861 606 518 106 349 410 765 745 812 563 998 150 392 358 328 747 793 587 507...
output:
250347169 248662078 245260552 253150328 247096579 249698948 249942589 251180693 248589849 253775352 248472247 248369272 249001282 249611561 251718722 248202949 252492155 251442262 255269934 247070745 248898892 250071493 249262069 247714054 248954719 251676093 251650611 249152315 248608212 249678723 ...
result:
ok 10000 lines
Test #6:
score: 0
Accepted
time: 9ms
memory: 4512kb
input:
1000 10000 301 793 604 129 545 524 625 540 271 616 710 629 682 190 152 287 40 5 921 699 730 427 833 680 514 782 641 754 15 218 725 862 238 468 917 368 850 35 243 339 688 460 597 979 398 748 552 25 189 68 115 76 888 844 890 102 835 581 266 342 571 749 574 156 717 538 440 764 601 831 130 39 136 842 78...
output:
250889096 249771741 249454079 255348475 249559048 257117687 248782164 250050189 254433188 254774148 251601723 250597849 251570596 249861162 256368189 253409601 254026050 249568513 248807065 253946543 254531155 252620668 255570774 257943265 252823304 255566622 252678751 254417015 252540528 256854774 ...
result:
ok 10000 lines
Test #7:
score: 0
Accepted
time: 7ms
memory: 8636kb
input:
1000 10000 163 149 53 93 995 744 452 73 683 474 213 597 903 190 842 487 894 256 339 588 902 930 870 632 992 561 455 248 961 788 188 436 421 887 847 361 311 192 706 636 352 233 987 376 323 32 120 565 546 730 191 587 404 22 817 906 585 420 621 167 132 816 431 200 952 180 402 838 794 284 349 94 427 482...
output:
250865862 251948566 251553098 253385993 250129972 254017302 254493541 251442447 247937015 250833800 249523549 249193386 252059812 249370594 254109165 249397918 247138568 248934855 249807415 251196617 250357758 249395769 248148350 247009170 252163335 249850249 250639740 251827755 247795165 253868762 ...
result:
ok 10000 lines
Test #8:
score: 0
Accepted
time: 9ms
memory: 4388kb
input:
1000 10000 384 881 331 471 166 902 45 90 353 415 895 74 693 69 887 878 807 436 538 328 892 750 294 891 238 527 406 464 23 259 200 731 336 62 30 157 457 243 956 485 670 37 405 16 14 162 484 875 64 38 305 177 391 882 440 649 296 702 990 953 468 109 915 316 622 81 863 286 412 584 659 271 282 984 490 97...
output:
251829371 249579880 249800743 251369774 250567175 248916841 252235114 251856809 249117279 247474406 250612288 247256785 251394491 250004784 252462213 249291071 251938054 251833233 254868301 251496842 242701038 251189092 250051802 248910394 249438626 245833186 250955694 251605815 251188358 253696421 ...
result:
ok 10000 lines
Test #9:
score: 0
Accepted
time: 6ms
memory: 4368kb
input:
1000 10000 791 811 116 51 267 840 647 573 765 859 154 984 879 576 938 308 808 301 287 158 43 453 284 243 945 585 156 648 447 331 930 296 657 820 991 457 969 237 644 784 514 685 487 506 353 854 336 7 111 324 593 4 234 548 680 923 501 186 201 918 821 968 549 236 171 173 553 476 882 569 74 841 910 689 ...
output:
260869097 250249635 252485552 251312537 249827807 247891441 247306872 250706363 246507025 251504505 253441362 252531355 248389010 251206287 251647794 256299060 251419971 256041214 250785378 257137028 247372087 262923110 250837210 248771282 245058822 265251780 251710904 249728123 250029619 252510223 ...
result:
ok 10000 lines
Test #10:
score: 0
Accepted
time: 9ms
memory: 4404kb
input:
1000 10000 107 858 884 149 531 779 189 350 748 934 302 276 957 567 102 902 906 898 373 631 532 743 683 856 625 610 80 863 986 16 322 141 538 352 758 5 656 870 828 180 982 356 308 19 240 422 622 859 850 331 565 472 321 365 962 941 638 188 448 403 864 588 222 22 184 854 300 554 781 711 973 324 283 359...
output:
252498827 250467741 245004582 254567945 248723182 266747940 252248407 250985182 246296872 258740339 250929702 252439407 264653513 256570063 251140559 251878111 245187220 257848088 248480375 257243736 247751395 252036917 250285176 249672867 247804044 247486222 252436982 247016052 254469398 257746086 ...
result:
ok 10000 lines
Test #11:
score: 0
Accepted
time: 9ms
memory: 4384kb
input:
1000 10000 469 92 385 860 536 350 852 471 547 625 369 749 339 169 21 565 312 801 41 996 326 280 660 211 910 75 929 202 768 652 865 4 498 109 179 568 601 167 276 661 297 654 909 958 922 815 515 673 253 771 879 108 682 405 476 418 604 847 225 265 329 270 228 872 571 775 69 180 118 78 35 808 795 412 13...
output:
249086375 263201066 251739073 251359274 253144475 272709870 251828190 252458176 265505472 261406223 268442012 246572070 247852028 276578867 252522304 277213878 270316579 252964972 261401771 275240590 279151202 251157983 259726293 256059004 255499430 253949570 253723754 275131199 250198559 251473808 ...
result:
ok 10000 lines
Test #12:
score: 0
Accepted
time: 6ms
memory: 4376kb
input:
1000 10000 458 237 370 989 962 641 819 68 976 944 204 184 479 612 434 436 765 572 554 959 36 710 891 584 882 521 928 94 263 633 406 833 534 363 513 21 281 745 699 864 669 850 529 315 455 375 292 60 70 685 782 613 344 217 931 862 739 215 691 48 635 940 182 348 188 801 432 8 696 49 236 772 539 757 193...
output:
333833500 333833500 333833500 249545048 333833500 249545048 249545048 249545048 249545048 249545048 249545048 333833500 249545048 333833500 249545048 333833500 249545048 249545048 333833500 333833500 249545048 249545048 333833500 249545048 249545048 249545048 333833500 333833500 249545048 333833500 ...
result:
ok 10000 lines
Test #13:
score: -100
Wrong Answer
time: 82ms
memory: 11436kb
input:
10000 100000 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 1...
output:
-1674109088 -1724094088 -1774069088 -1824034088 -1873989088 -1923934088 -1973869088 -2023794088 -2073709088 -2123614088 2121458208 2071573208 2021698208 1971833208 1921978208 1872133208 1822298208 1772473208 1722658208 1672853208 1623058208 1573273208 1523498208 1473733208 1423978208 1374233208 1324...
result:
wrong answer 1st lines differ - expected: '333333340000', found: '-1674109088'