QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#103891 | #4045. 排列 | zaneyu# | 20 | 774ms | 63184kb | C++23 | 3.3kb | 2023-05-07 19:43:09 | 2024-05-26 02:52:20 |
Judging History
answer
/*input
2
3
1 2 3
3
2 3 1
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=5e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
const ld PI=acos(-1);
const ld eps=1e-4;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<P.f<<' '<<P.s;
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?" ":"");
return out;
}
ll mult(ll a,ll b){
return a*b%MOD;
}
ll mypow(ll a,ll b){
a%=MOD;
if(a==0) return 0;
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
int arr[maxn];
int vis[maxn];
multiset<int> mx[maxn];
int lpf[maxn];
int cnt[maxn];
int inv[maxn];
ll ans=1;
void add(int x){
while(x>1){
int z=lpf[x];
int c=0;
while(x%z==0){
x/=z;
++c;
}
int pv=(*prev(mx[z].end()));
mx[z].insert(c);
if(c<=pv) continue;
ans=ans*mypow(z,c-pv);
}
}
void del(int x){
while(x>1){
int z=lpf[x];
int c=0;
int stf=1;
while(x%z==0){
x/=z;
stf*=z;
++c;
}
ans=ans*inv[stf]%MOD;
mx[z].erase(mx[z].find(c));
int k=(*prev(mx[z].end()));
ans=ans*mypow(z,k)%MOD;
}
}
void solve(){
int n;
cin>>n;
REP(i,n){
cin>>arr[i],--arr[i];
mx[i+1].insert(0);
}
int cur=1;
vector<int> v;
REP(i,n){
if(vis[i]) continue;
int x=i;
int len=0;
while(!vis[x]){
vis[x]=cur;
x=arr[x];
++len;
}
cnt[len]++;
v.pb(len);
add(len);
++cur;
}
SORT_UNIQUE(v);
ll tot=0;
REP(i,sz(v)){
del(v[i]);
for(int j=i+1;j<sz(v);j++){
del(v[j]);
add(v[i]+v[j]);
tot+=ans*2*cnt[v[i]]%MOD*cnt[v[j]]%MOD*v[i]%MOD*v[j]%MOD;
del(v[i]+v[j]);
add(v[j]);
}
if(cnt[v[i]]>1){
del(v[i]);
add(2*v[i]);
tot+=ans*cnt[v[i]]%MOD*(cnt[v[i]]-1)%MOD*v[i]%MOD*v[i]%MOD;
del(2*v[i]);
add(v[i]);
}
tot%=MOD;
}
cout<<tot<<'\n';
REP(i,n) vis[i]=0,mx[i+1].clear(),cnt[i+1]=0;
}
int main(){
for(int i=2;i<maxn;i++){
for(int j=i;j<maxn;j+=i){
if(!lpf[j]) lpf[j]=i;
}
}
REP1(i,maxn-1) inv[i]=mypow(i,MOD-2);
int t;
cin>>t;
while(t--) solve();
}
詳細信息
Test #1:
score: 10
Accepted
time: 146ms
memory: 39712kb
input:
5 98005 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 10...
output:
0 0 0 0 0
result:
ok 5 number(s): "0 0 0 0 0"
Test #2:
score: 10
Accepted
time: 144ms
memory: 39656kb
input:
5 96008 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 10...
output:
0 0 0 0 0
result:
ok 5 number(s): "0 0 0 0 0"
Test #3:
score: 0
Wrong Answer
time: 58ms
memory: 34396kb
input:
5 35 18 10 30 13 20 32 33 34 27 28 9 6 3 8 1 26 4 2 17 25 7 12 31 5 24 35 11 15 29 23 19 16 21 22 14 35 31 6 24 28 25 20 18 17 30 12 4 15 11 8 22 19 29 34 5 27 33 13 14 7 21 9 35 2 16 10 1 26 32 23 3 35 14 32 27 2 4 21 9 8 24 15 11 33 22 6 19 1 31 26 28 23 16 3 18 25 10 35 17 20 34 30 29 7 13 12 5 3...
output:
300294905 763362397 103100312 -775176126 359488926
result:
wrong answer 1st numbers differ - expected: '264720', found: '300294905'
Test #4:
score: 0
Wrong Answer
time: 62ms
memory: 33780kb
input:
5 197 185 145 76 108 112 8 83 176 171 141 162 38 102 136 122 152 166 11 1 135 130 42 87 69 113 115 138 37 30 110 164 137 82 43 183 14 64 99 49 51 153 182 146 59 24 197 40 60 2 117 128 16 191 28 196 179 97 161 192 57 32 85 58 175 13 89 187 54 75 168 103 68 62 180 100 70 53 140 111 23 50 116 159 12 94...
output:
4238652 608963226 -257597982 -114242482 285497154
result:
wrong answer 2nd numbers differ - expected: '353540880', found: '608963226'
Test #5:
score: 0
Wrong Answer
time: 68ms
memory: 33580kb
input:
5 2458 1924 1946 1771 473 1896 756 722 87 85 245 1408 605 1410 313 495 127 213 915 964 1358 1599 2356 1509 1820 844 1299 809 1525 64 1356 1067 857 1865 1637 519 2012 338 1516 1949 309 2259 182 921 73 1862 1014 1541 2125 1608 1416 930 2338 981 1193 1858 901 2392 277 2288 68 1253 28 365 760 1811 2127 ...
output:
491572428 865641717 77693745 -128217900 127951163
result:
wrong answer 1st numbers differ - expected: '978052529', found: '491572428'
Test #6:
score: 0
Wrong Answer
time: 176ms
memory: 40056kb
input:
5 99185 26839 61121 45992 14403 65124 24864 24199 33689 29856 380 33507 43200 19519 25796 37723 6125 75456 95552 69200 20 41718 90058 1095 17598 26502 48916 17135 71629 47605 87901 81017 8068 294 5119 6766 13238 94076 81234 66485 97356 8495 7138 67357 78520 22079 28469 62820 10519 29849 85393 94754 ...
output:
387548982 576230416 -186953655 -543855782 790704306
result:
wrong answer 1st numbers differ - expected: '30625137', found: '387548982'
Test #7:
score: 0
Wrong Answer
time: 148ms
memory: 40276kb
input:
5 97719 54330 7287 42502 46748 89696 26766 73785 45779 78745 77009 12843 68346 70268 86363 9338 37991 8250 42092 65636 42830 94460 20829 70115 51051 42361 42787 40552 28200 31627 39620 62229 5783 63051 71265 23966 55290 91270 15488 51871 9502 27171 8529 18288 96742 2458 91419 92000 90167 41768 64543...
output:
-732066205 384885766 847545426 304798309 -636746311
result:
wrong answer 1st numbers differ - expected: '557314000', found: '-732066205'
Test #8:
score: 0
Wrong Answer
time: 167ms
memory: 40544kb
input:
5 98247 5710 49877 43452 94001 96873 64582 12275 27479 10333 15298 40636 16574 45960 24260 22082 52701 48364 72346 40713 39476 96997 18048 73992 61216 33184 17676 17070 52531 51967 94266 8416 14666 54237 24907 7626 5477 15042 60115 6627 43141 86307 20699 88929 51763 78632 31451 77977 60138 54936 181...
output:
-489563612 -712534531 -161560789 -82398209 272820782
result:
wrong answer 1st numbers differ - expected: '53218185', found: '-489563612'
Test #9:
score: 0
Wrong Answer
time: 742ms
memory: 63184kb
input:
5 478810 212740 317409 24031 371981 433384 170780 64048 152611 228849 320502 138270 124681 107152 366209 195005 386784 25774 409661 283786 33634 385169 225292 244770 159952 51564 417197 308732 216201 439365 95461 275072 103286 175283 107672 361963 178996 73879 117794 111044 215371 301840 447509 4055...
output:
-362894055 247754914 17139345 755804517 -317206590
result:
wrong answer 1st numbers differ - expected: '432340839', found: '-362894055'
Test #10:
score: 0
Wrong Answer
time: 774ms
memory: 61828kb
input:
5 495437 15542 448191 294627 367556 66964 387494 217813 228809 59154 222553 220684 195206 161876 108475 341708 249137 200490 465901 171655 463006 292802 300999 366933 308286 396686 54874 133267 369496 478958 401677 469352 472285 110117 5694 408189 446558 384897 110566 281732 348029 92707 278977 4789...
output:
738371827 -898788480 -313631722 211440115 -435847165
result:
wrong answer 1st numbers differ - expected: '255314907', found: '738371827'