QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#211224 | #3840. Pass the Ball! | linnins | WA | 6ms | 5808kb | C++20 | 4.0kb | 2023-10-12 12:45:49 | 2023-10-12 12:45:49 |
Judging History
answer
#pragma GCC optimize(3)
#include<cstdio>
#include<vector>
#include<iostream>
#include<map>
#define int __int128
using namespace std;
const int modp = 4179340454199820289;
const int g = 3;
const int gny = 1393113484733273430;
int pos[1000500 + 1000];
int Map[1000500];
int n,m;
int flag;
int check[1000500];
int maxlen, maxbit, inv;
vector< vector<int> > Vec;
vector< vector<int> > Vec2;
vector< vector<int> > Answer;
vector< int > Size;
int read_int();
void print_int(int x);
void Swap(int &a,int &b);
void Make_Ring(int t);
int Cal(int p);
void NTT(vector<int> &A,vector<int> &B);
int Cal_Ring(int t,int p);
int pow_k(int a, int b);
void ntt(vector<int> &A, bool flag);
void checkAnswer();
signed main()
{
n = read_int();
m = read_int();
for(int i=1;i<=n;i++){
int next;
next = read_int();
Map[i] = next;
}
for(int i=1;i<=n;i++){
if(check[i] == 0){
Make_Ring(i);//通过flag来表示前方有几个环
}
}
flag = Vec.size();
for(int i=0;i<flag;i++){
vector<int> Mid;
int p = Vec[i].size();
for(int j=0;j<=p;j++)
Mid.push_back(0);
for(int j=p+1;j<=p*2;j++)
Mid.push_back(Vec[i][p*2-j]);
Vec2.push_back(Mid);
}
for(int i=0;i<flag;i++)
NTT(Vec2[i],Vec[i]);
checkAnswer();
for(int i=1;i<=m;i++){
int p;
p = read_int();
print_int(Cal(p));
putchar('\n');
}
return 0;
}
void checkAnswer(){
map<int,int> Check;
int Point = 0;
for(int i=0;i<Vec2.size();i++){
if(Check[Vec2[i].size()]==0){
Check[Vec2[i].size()] = Point++;
Answer.push_back(Vec2[i]);
}
else{
for(int j=0;j<Answer[Check[Vec2.size()]].size();j++){
Answer[Check[Vec2.size()]][j] += Vec2[i][j];
}
}
}
}
void Make_Ring(int t){
vector <int> Mid;
while(check[t] == 0){
Mid.push_back(t);
check[t] = 1;
t = Map[t];
}
Vec.push_back(Mid);
Size.push_back(Mid.size());
return;
}
void NTT(vector<int> &A,vector<int> &B){
int n = A.size() - 1;
int m = B.size() - 1;
maxlen = 1;
maxbit = 0;
while(maxlen <= n + m)
{
maxlen <<= 1;
++maxbit;
}
for(int i = A.size();i<=maxlen;i++){
A.push_back(0);
}
for(int i = B.size();i<=maxlen;i++){
B.push_back(0);
}
inv = pow_k(maxlen, modp - 2);
for(int i = 1; i < maxlen; ++i)
{
pos[i] = ((pos[(i >> 1)] >> 1) | ((i & 1) << (maxbit - 1)));
}
ntt(A, 1);
ntt(B, 1);
for(int i = 0; i < maxlen; ++i)
{
A[i] = A[i] * B[i] % modp;
}
ntt(A, 0);
for(int i = 0; i < n + m + 1; ++i)
{
A[i] = A[i] * inv % modp;
}
}
int Cal(int p){
int Ans = 0;
for(int i=0;i<Answer.size();i++){
Ans += Cal_Ring(i,p);
}
return Ans;
}
int Cal_Ring(int t,int p){
int n = Size[t];
p = p%n;
return Answer[t][p+2*n] + Answer[t][p+n];
}
int pow_k(int a, int b)
{
int ans = 1;
while(b > 0)
{
if(b & 1)
ans = ans * a % modp;
a = a * a % modp;
b = (b >> 1);
}
return ans;
}
void ntt(vector<int> &A, bool flag)
{
for(int i = 1; i < maxlen; ++i)
{
if(i < pos[i]) Swap(A[pos[i]], A[i]);
}
for(int len = 1; len < maxlen; len <<= 1)
{
int wn = pow_k(flag ? g : gny, (modp - 1) / (len << 1));
for(int i = 0; i < maxlen; i += (len << 1))
{
int w = 1;
for(int j = 0; j < len; ++j)
{
int temp1 = A[i + j], temp2 = w * A[i + j + len] % modp;
A[i + j] = (temp1 + temp2) % modp;
A[i + j + len] = ((temp1 - temp2) % modp + modp) % modp;
w = w * wn % modp;
}
}
}
return ;
}
void Swap(int &a,int &b){
int c;
c = a;
a = b;
b = c;
}
void print_int(int x){
if (x < 0)
{
putchar('-');
x = -x;
}
if (x > 9)
print_int(x / 10);
putchar(x % 10 + '0');
}
int read_int(){
bool flag = 0;
char c;
int x = 0;
while(((c = getchar()) < '0' || c > '9') && c != '-');
if(c == '-')
flag = 1;
else
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = x * 10 + c - '0';
if(flag)
return -x;
else
return x;
}
/*
4 4
2 4 1 3
1
2
3
4
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5552kb
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: 0ms
memory: 5552kb
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: 5468kb
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: 4ms
memory: 5792kb
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: 4ms
memory: 5808kb
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: -100
Wrong Answer
time: 6ms
memory: 5728kb
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:
1735117872442423965050303145963079 1262666482820856726 5906364127907743911 28653067550970160460499 1735117875115891756271171903432240 5205353620030767643905948730167803 1735117875224471201376549321955557 1735117875264077265131952582089896 6940471493998236098821857293830808 69404715023367189937979744...
result:
wrong answer 1st lines differ - expected: '250889096', found: '1735117872442423965050303145963079'