QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#80166 | #1270. The Missing Pet | eyiigjkn | AC ✓ | 696ms | 68780kb | C++14 | 3.6kb | 2023-02-22 20:29:16 | 2023-02-22 20:29:18 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
constexpr int mod=1e9+7,iv[]={0,1,500000004,333333336,250000002};
int n,N=0,X[210],Y[210],val[410],f[210][210];
vi F[210][210],M[410];
bool flag[210][210];
template<typename T>
inline void upd(int &x,const T &y){x=(x+y)%mod;}
int power(int a,int b)
{
int ans=1;
for(;b;b>>=1,a=(ll)a*a%mod)
if(b&1) ans=(ll)ans*a%mod;
return ans;
}
vi operator+(vi A,const vi &B)
{
for(int i=0;i<=N;i++) upd(A[i],B[i]);
return A;
}
vi operator-(vi A,const vi &B)
{
for(int i=0;i<=N;i++) upd(A[i],mod-B[i]);
return A;
}
vi operator*(vi A,int x)
{
for(int &i:A) i=(ll)i*x%mod;
return A;
}
vi &operator+=(vi &A,const vi &B)
{
for(int i=0;i<=N;i++) upd(A[i],B[i]);
return A;
}
inline vi getvi(int x,int y){vi v(N+1);v[x]=y;return v;}
inline int getD(int x,int y){return (x>1)+(x<n)+(y>1)+(y<n);}
inline int getP(int x,int y){return iv[getD(x,y)];}
vi calcP(int x,int y)
{
vi ans=getvi(N,x==X[0] && y==Y[0]);
if(x>1 && !flag[x-1][y]) ans+=F[x-1][y]*getP(x-1,y);
if(x<n && !flag[x+1][y]) ans+=F[x+1][y]*getP(x+1,y);
if(y>1 && !flag[x][y-1]) ans+=F[x][y-1]*getP(x,y-1);
if(y<n && !flag[x][y+1]) ans+=F[x][y+1]*getP(x,y+1);
return ans;
}
vi calcS(int x,int y)
{
vi ans(N+1);
if(x>1 && !flag[x-1][y]) ans+=(F[x-1][y]+getvi(N,f[x-1][y]))*getP(x-1,y);
if(x<n && !flag[x+1][y]) ans+=(F[x+1][y]+getvi(N,f[x+1][y]))*getP(x+1,y);
if(y>1 && !flag[x][y-1]) ans+=(F[x][y-1]+getvi(N,f[x][y-1]))*getP(x,y-1);
if(y<n && !flag[x][y+1]) ans+=(F[x][y+1]+getvi(N,f[x][y+1]))*getP(x,y+1);
return ans;
}
void Gauss()
{
for(int i=0;i<N;i++)
{
for(int j=i;j<N;j++)
if(M[j][i])
{
if(i!=j) swap(M[i],M[j]);
break;
}
int invn=power(M[i][i],mod-2);
for(int j=i;j<=N;j++) M[i][j]=(ll)M[i][j]*invn%mod;
for(int j=i+1;j<N;j++)
{
int t=mod-M[j][i];
for(int k=i;k<=N;k++) upd(M[j][k],(ll)t*M[i][k]);
}
}
for(int i=0;i<N;i++) val[i]=(mod-M[i][N])%mod;
for(int i=N-2;i>=0;i--)
for(int j=i+1;j<N;j++)
upd(val[i],(ll)(mod-M[i][j])*val[j]);
}
int main()
{
int T,K;
cin>>T;
while(T--)
{
int cnt=0,cur=0;
scanf("%d%d",&n,&K);
for(int i=1;i<=K;i++) scanf("%d%d",&X[i],&Y[i]),flag[X[i]][Y[i]]=1;
scanf("%d%d",&X[0],&Y[0]);
N=n;
for(int i=2;i<=n;i++)
for(int j=1;j<=n;j++)
N+=flag[i][j];
for(int i=1;i<=n;i++) fill(F[i]+1,F[i]+n+1,vi(N+1));
for(int i=1;i<=n;i++) F[1][i]=getvi(cnt++,1);
for(int i=1;i<n;i++)
for(int j=1;j<=n;j++)
if(flag[i+1][j]) M[cur++]=F[i][j]-calcP(i,j),F[i+1][j]=getvi(cnt++,1);
else F[i+1][j]=(F[i][j]-calcP(i,j))*getD(i+1,j);
for(int i=1;i<=n;i++) M[cur++]=F[n][i]-calcP(n,i);
Gauss();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
f[i][j]=F[i][j][N];
for(int k=0;k<N;k++) upd(f[i][j],(ll)F[i][j][k]*val[k]);
}
cnt=cur=0;
for(int i=1;i<=n;i++) fill(F[i]+1,F[i]+n+1,vi(N+1));
for(int i=1;i<=n;i++) F[1][i]=getvi(cnt++,1);
for(int i=1;i<n;i++)
for(int j=1;j<=n;j++)
if(flag[i+1][j]) M[cur++]=F[i][j]-calcS(i,j),F[i+1][j]=getvi(cnt++,1);
else F[i+1][j]=(F[i][j]-calcS(i,j))*getD(i+1,j);
for(int i=1;i<=n;i++) M[cur++]=F[n][i]-calcS(n,i);
Gauss();
for(int i=1;i<=K;i++)
{
int ans;
if(f[X[i]][Y[i]])
{
ans=F[X[i]][Y[i]][N];
for(int j=0;j<N;j++) upd(ans,(ll)F[X[i]][Y[i]][j]*val[j]);
ans=(ll)ans*power(f[X[i]][Y[i]],mod-2)%mod;
}
else ans=-1;
printf("%d%c",ans," \n"[i==K]);
}
for(int i=1;i<=n;i++) memset(flag[i]+1,0,sizeof(bool)*n);
for(int i=1;i<=n;i++) memset(f[i]+1,0,sizeof(int)*n);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4664kb
input:
2 3 3 1 1 1 2 2 1 2 2 5 3 5 3 4 1 3 2 4 5
output:
-1 4 4 669185882 381533358 341349117
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 4748kb
input:
2 3 3 1 1 1 2 2 1 2 2 5 5 5 5 4 1 2 1 3 2 4 4 4 5
output:
-1 4 4 142091424 563003915 338226662 981023931 941123612
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 4664kb
input:
20 2 1 2 2 2 1 2 1 1 1 2 2 2 1 2 1 1 1 2 1 2 1 1 2 2 1 1 2 2 2 3 1 1 3 1 1 3 1 1 3 3 1 3 1 1 3 1 1 3 1 3 1 1 2 3 1 2 3 1 2 4 2 2 2 2 3 4 1 4 2 4 1 4 3 2 2 4 2 2 4 4 3 3 3 4 2 4 2 2 2 3 1 4 2 1 1 2 1 2 4 5 5 2 4 5 2 4 4 3 5 3 4 1 2 5 5 4 1 2 1 5 5 3 2 4 4 4 5 5 5 2 5 3 5 2 1 3 2 1 5 1 1 5 5 1 1 3 1 5...
output:
3 4 3 4 3 15 18 15 17 10 319341873 719608280 748388186 433926209 977031797 516623223 890690892 823114863 940425670 309319181 180522066 150382918 86504151 109663593 461221278 563003915 338226662 142091424 981023931 941123612 959280058 146346164 239635721 258223814 541295315 496633273 929115631 162138...
result:
ok 20 lines
Test #4:
score: 0
Accepted
time: 693ms
memory: 68572kb
input:
1 200 200 64 169 49 196 138 149 5 121 53 147 195 178 122 153 61 177 192 50 44 34 200 66 29 2 160 99 40 51 51 15 10 35 151 109 176 108 34 31 151 111 183 126 29 80 156 107 66 62 120 28 74 81 132 180 80 162 194 144 196 116 21 104 192 61 180 81 176 95 3 47 129 187 142 14 156 65 107 36 148 160 57 28 59 9...
output:
878648719 113233125 786380751 181554898 318327728 369781284 360764905 713875112 30840020 453584282 92048242 497643500 707139393 826718028 652739155 396190690 37821269 939899095 561075138 81513812 861036782 854039079 374993909 212643254 61363197 387550273 69392760 966635798 81939496 778573701 3591560...
result:
ok single line: '878648719 113233125 786380751 ...4 663213790 834243123 627472274'
Test #5:
score: 0
Accepted
time: 654ms
memory: 68576kb
input:
1 200 200 129 86 153 87 70 130 47 172 31 158 87 24 52 127 98 137 153 118 166 37 126 151 45 143 6 42 108 97 71 6 110 46 60 169 31 85 79 151 96 174 129 5 56 184 126 96 152 113 90 23 49 86 63 139 3 191 80 67 7 91 182 94 156 182 179 167 92 173 153 76 84 11 106 69 95 43 142 80 121 188 11 41 30 83 170 9 1...
output:
123851113 144741078 975142614 864309471 727374984 810170298 639341353 748929034 770904262 9817011 512903522 766813369 316425327 931383126 872443983 361671859 969963515 260369908 55333683 720518628 838705714 682285865 298436149 573082323 507493363 991573260 922382038 786554831 861807525 226609524 869...
result:
ok single line: '123851113 144741078 975142614 ...4 895488066 527193807 471697939'
Test #6:
score: 0
Accepted
time: 664ms
memory: 68656kb
input:
1 200 200 79 90 15 198 93 165 158 183 155 144 113 77 188 102 44 196 133 169 6 190 64 46 54 88 52 161 58 38 38 70 2 57 1 117 69 196 66 107 23 53 145 55 41 133 94 105 16 116 89 70 79 96 81 43 38 55 112 197 120 151 157 129 31 123 183 195 103 6 63 183 91 136 95 89 107 130 2 3 82 120 155 112 87 43 144 2 ...
output:
369348632 14811982 952255159 778234279 361573467 933717273 817249505 140857816 820818860 533172591 933761564 554440134 845528741 9961307 803443871 798136962 958449481 113740709 573187144 774653297 223872699 469954506 929222897 594103743 34006010 378678518 534759092 877512379 67817951 940837128 59346...
result:
ok single line: '369348632 14811982 952255159 7...6 209174952 846138107 606226830'
Test #7:
score: 0
Accepted
time: 696ms
memory: 68780kb
input:
1 200 200 30 58 187 153 18 123 83 18 139 131 25 156 177 159 145 150 62 179 158 10 173 178 81 13 28 183 7 75 155 189 44 6 154 125 62 43 166 98 71 27 153 71 10 106 70 106 87 70 44 125 185 59 65 34 185 23 141 64 8 115 31 14 10 164 100 156 48 30 117 97 187 113 115 149 105 188 117 159 2 23 174 58 200 57 ...
output:
221837212 604439188 441024281 993310666 37581123 844685181 3682829 438955606 660599677 362476716 534289133 849765609 65950746 291678709 724608271 907340807 921499236 981163671 506610509 732789655 765435784 896185995 537321417 348341337 214812569 823165134 929869732 858991865 301115595 95048907 47508...
result:
ok single line: '221837212 604439188 441024281 ...9 527622105 209405952 656542233'
Test #8:
score: 0
Accepted
time: 123ms
memory: 16432kb
input:
1 107 144 4 54 70 43 35 78 48 33 84 84 34 30 48 76 82 91 43 98 98 41 73 9 78 35 79 1 102 107 97 19 81 1 25 8 31 20 14 27 8 98 14 67 66 6 5 51 87 63 20 28 37 36 49 67 80 66 65 4 30 31 103 84 35 107 84 6 100 78 107 96 14 59 75 43 70 50 5 49 84 56 94 41 7 1 76 75 76 8 38 21 91 31 11 44 28 2 49 103 25 1...
output:
762990993 604823981 192287409 780451571 433936079 750404770 3828429 383373163 852753672 355792314 584536962 979526951 133844459 286265226 70717829 437213858 439153503 154991813 65542818 164011298 33932573 277424798 663628821 799843980 88266345 205804207 116119644 592409516 292407131 57113242 4697598...
result:
ok single line: '762990993 604823981 192287409 ...8 370570955 301599038 676184282'
Test #9:
score: 0
Accepted
time: 118ms
memory: 16172kb
input:
1 112 112 36 50 2 109 75 61 53 22 69 94 28 7 74 74 88 48 2 41 20 111 26 29 33 20 99 21 52 20 25 25 56 95 108 97 19 112 86 39 97 47 110 41 68 27 87 34 97 111 93 77 59 84 22 85 110 62 5 7 9 3 72 5 71 37 105 3 5 103 112 83 37 14 40 103 58 4 41 9 6 31 81 40 69 29 68 71 18 39 28 22 77 77 59 108 36 1 7 54...
output:
223686934 382067316 430425551 225081177 6641738 298909699 697408485 339083370 88177839 696997813 222562263 833744008 522858294 185558909 161348624 38279006 492327166 151744582 249678276 50577569 752546114 468931178 804571527 793742478 528822458 179233262 827142953 782311905 177954357 5309556 7707131...
result:
ok single line: '223686934 382067316 430425551 ...3 947948969 696668385 160850057'
Test #10:
score: 0
Accepted
time: 475ms
memory: 52712kb
input:
1 200 100 100 43 100 44 100 45 100 46 100 47 100 48 100 49 100 50 100 51 100 52 101 43 101 44 101 45 101 46 101 47 101 48 101 49 101 50 101 51 101 52 102 43 102 44 102 45 102 46 102 47 102 48 102 49 102 50 102 51 102 52 103 43 103 44 103 45 103 46 103 47 103 48 103 49 103 50 103 51 103 52 104 43 104...
output:
454644159 488575000 323123529 547929203 224184375 880948703 318566536 325703217 589754845 826453476 1582262 -1 -1 -1 -1 -1 -1 -1 -1 293016905 230859856 -1 -1 -1 -1 -1 -1 -1 -1 220285410 171517385 -1 -1 -1 -1 -1 -1 -1 -1 913170818 390272160 -1 -1 -1 -1 -1 -1 -1 -1 333559748 266600322 -1 -1 -1 -1 -1 -...
result:
ok single line: '454644159 488575000 323123529 ...0 568328122 843968742 819428573'