QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#297990 | #4045. 排列 | -Ofast | 100 ✓ | 777ms | 38756kb | C++14 | 4.4kb | 2024-01-05 15:22:55 | 2024-01-05 15:22:56 |
Judging History
answer
#include <bits/stdc++.h>
#define mp make_pair
#define fir first
#define sec second
#define mt make_tuple
#define int long long
#define fin(File) freopen(File".in","r",stdin)
#define fout(File) freopen(File".out","w",stdout)
using namespace std;
namespace FastIO {
//char *p1, *p2, buf[1 << 14];
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, (1 << 14), stdin), p1 == p2) ? EOF : *p1++)
template <typename T>
inline void read(T& x) {
x = 0;
register int t = 1;
register char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
t = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9'){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= t;
}
template <typename T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename T>
inline void writeln(T x, char sep = '\n') {
write(x);
putchar(sep);
}
}
const int N=1e6+10;
const int mod=1e9+7;
const int B=1010;
int isp[N],pre[N],inv[N];
vector <int> prime;
int qpow(int a,int b){
a%=mod;int res=1;
while(b){
if(b&1)res=(res*a)%mod;
b>>=1;a=(a*a)%mod;
}return res;
}
void init(int n){
for(int i=2;i<=n;i++){
inv[i]=qpow(i,mod-2);
if(!isp[i]){
prime.push_back(i);
pre[i]=i;
}
for(auto j:prime){
if(i*j>n)break;
isp[i*j]=1;pre[i*j]=j;
if(i%j==0)break;
}
}
}
int T,n,p[N],vis[N],sz;
void C(int x){
if(vis[x])return;
vis[x]=1;sz++;C(p[x]);
}
map <int,map<int,int>> pw;
int Max(int x){
map <int,int> &G=pw[x];
if(!G.size())return 0;
auto it=G.end();it--;
while(G.size()&&it->sec==0){
G.erase(it);
if(!G.size())return 0;
it=G.end();it--;
}
if(G.size()){
return G.rbegin()->fir;
}else return 0;
}
using namespace FastIO;
void solve(){
read(n);pw.clear();
for(int i=1;i<=n;i++)
read(p[i]),vis[i]=0;
map <int,int> m;
int sum=0;
for(int i=1;i<=n;i++){
if(vis[i])continue;
sz=0;C(i);m[sz]++;sum+=sz;
}
vector <int> a,b;
for(auto it:m){
a.push_back(it.fir);
b.push_back(it.sec);
}
sz=a.size();
for(int i=0;i<sz;i++){
for(int j=1;j<=b[i];j++){
int tmp=a[i];
map <int,int> now;
while(tmp!=1){
now[pre[tmp]]++;
tmp/=pre[tmp];
}
for(auto it:now){
pw[it.fir][it.sec]++;
}
}
}
int res=0,lcm=1;
for(auto it:pw){
for(int i=1;i<=Max(it.fir);i++)
lcm*=it.fir,lcm%=mod;
}
for(int i=0;i<sz;i++){
for(int j=0;j<sz;j++){
int sum=a[i]+a[j],X=0;
if(i==j){
X=(b[i]*a[i]%mod)*((b[i]-1+mod)*a[i]%mod)%mod;
}else{
X=(b[i]*a[i]%mod)*(b[j]*a[j]%mod)%mod;
}
map <int,int> now;
int tmp=sum;
while(tmp!=1){
now[pre[tmp]]++;
tmp/=pre[tmp];
}
vector <tuple<int,int,int>> undo;
undo.reserve(50);
for(auto it:now){
undo.push_back(mt(it.fir,it.sec,-1));
int lst=Max(it.fir);
pw[it.fir][it.sec]++;
int nxt=Max(it.fir);
if(nxt-lst>=0){
lcm*=qpow(it.fir,nxt-lst);
}else{
lcm*=qpow(inv[it.fir],lst-nxt);
}
lcm%=mod;
}
tmp=a[i];now.clear();
while(tmp!=1){
now[pre[tmp]]++;
tmp/=pre[tmp];
}
for(auto it:now){
undo.push_back(mt(it.fir,it.sec,1));
int lst=Max(it.fir);
pw[it.fir][it.sec]--;
int nxt=Max(it.fir);
if(nxt-lst>=0){
lcm*=qpow(it.fir,nxt-lst);
}else{
lcm*=qpow(inv[it.fir],lst-nxt);
}
lcm%=mod;
}
tmp=a[j];now.clear();
while(tmp!=1){
now[pre[tmp]]++;
tmp/=pre[tmp];
}
for(auto it:now){
undo.push_back(mt(it.fir,it.sec,1));
int lst=Max(it.fir);
pw[it.fir][it.sec]--;
int nxt=Max(it.fir);
if(nxt-lst>=0){
lcm*=qpow(it.fir,nxt-lst);
}else{
lcm*=qpow(inv[it.fir],lst-nxt);
}
lcm%=mod;
}
res+=X*lcm%mod;res%=mod;
reverse(undo.begin(),undo.end());
for(auto it:undo){
int x=get<0>(it),y=get<1>(it),z=get<2>(it);
int lst=Max(x);
pw[x][y]+=z;
int nxt=Max(x);
if(nxt-lst>=0){
lcm*=qpow(x,nxt-lst);
}else{
lcm*=qpow(inv[x],lst-nxt);
}
lcm%=mod;
}
}
}
writeln(res);
}
signed main(){
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//fin("perm");fout("perm");
init(1e6);read(T);
while(T--)solve();
//cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
return 0;
}
詳細信息
Test #1:
score: 10
Accepted
time: 106ms
memory: 32056kb
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: 117ms
memory: 32832kb
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: 10
Accepted
time: 101ms
memory: 29772kb
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:
264720 4620 171700 43512 118560
result:
ok 5 number(s): "264720 4620 171700 43512 118560"
Test #4:
score: 10
Accepted
time: 114ms
memory: 30444kb
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 353540880 230360760 352397593 962380654
result:
ok 5 number(s): "4238652 353540880 230360760 352397593 962380654"
Test #5:
score: 10
Accepted
time: 108ms
memory: 30864kb
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:
978052529 242234656 117060060 927865953 609414491
result:
ok 5 number(s): "978052529 242234656 117060060 927865953 609414491"
Test #6:
score: 10
Accepted
time: 124ms
memory: 30396kb
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:
30625137 354057431 257356181 679385499 743281292
result:
ok 5 number(s): "30625137 354057431 257356181 679385499 743281292"
Test #7:
score: 10
Accepted
time: 121ms
memory: 30208kb
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:
557314000 693919687 209770156 961373841 829047726
result:
ok 5 number(s): "557314000 693919687 209770156 961373841 829047726"
Test #8:
score: 10
Accepted
time: 184ms
memory: 34544kb
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:
53218185 842910290 715332616 936936023 802603566
result:
ok 5 number(s): "53218185 842910290 715332616 936936023 802603566"
Test #9:
score: 10
Accepted
time: 629ms
memory: 38756kb
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:
432340839 538243127 736567741 789874152 81799729
result:
ok 5 number(s): "432340839 538243127 736567741 789874152 81799729"
Test #10:
score: 10
Accepted
time: 777ms
memory: 38652kb
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:
255314907 598285290 204270639 686469720 457337304
result:
ok 5 number(s): "255314907 598285290 204270639 686469720 457337304"
Extra Test:
score: 0
Extra Test Passed