QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#369995 | #8285. Shell Sort | SolitaryDream# | TL | 2243ms | 83588kb | C++17 | 2.0kb | 2024-03-28 20:42:46 | 2024-03-28 20:42:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
using pii=pair<int,int>;
const int N=31,P=1e9+7;
map<vector<int>,pii> f;
pii merge(const pii &a,const pii &b)
{
pii w=a;
if(b.first>w.first)
w=b;
else if(b.first==w.first)
w.second=(w.second+b.second)%P;
return w;
}
int pos[N][N],cnt[N];
int bef[N][N];
int n,m,d[N];
int calc(vector<int> v)
{
int cnt=0;
for(int i=1;i<=m;i++)
{
for(int j=0;j<d[i];j++)
for(int k=j;k<n;k+=d[i])
{
for(int t=k;t>=d[i]&&v[t]>v[t-d[i]];t-=d[i])
cnt+=v[t]==-1,swap(v[t],v[t-d[i]]);
}
}
return cnt;
}
pii dp(vector<int> s)
{
if(f.count(s))
return f[s];
pii &ret=f[s];
ret.first=-1e9;
int hav=0;
for(int i=0;i<d[1];i++)
if(s[i])
hav|=bef[1][s[i]-1];
bool flag=0;
for(int i=0;i<d[1];i++)
{
if(s[i]==cnt[i])
continue;
flag=1;
s[i]++;
pii w=dp(s);
s[i]--;
int x=pos[i][s[i]];
vector<int> v(n);
for(int x=0;x<d[1];x++)
for(int y=0;y<s[x];y++)
v[pos[x][y]]=-2;
v[pos[i][s[i]]]=-1;
w.first+=calc(v);
ret=merge(ret,w);
}
if(!flag)
ret.first=0,ret.second=1;
return ret;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>d[i];
while(d[1]>n)
{
m--;
for(int i=m;i>=1;i--)
d[i]=d[i+1];
}
for(int i=0;i<d[1];i++)
for(int j=i;j<n;j+=d[1])
pos[i][cnt[i]++]=j;
for(int i=1;i<=m;i++)
for(int j=0;j<n;j++)
{
bef[i][j]=(1<<j);
if(j>=d[i])
bef[i][j]|=bef[i][j-d[i]];
}
vector<int> b(d[1]);
auto [u,v]=dp(b);
cout<<u<<" "<<v<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
2 2 2 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
5 4 5 4 2 1
output:
6 4
result:
ok 2 number(s): "6 4"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3800kb
input:
8 4 6 3 2 1
output:
15 4
result:
ok 2 number(s): "15 4"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
8 6 8 7 5 4 2 1
output:
14 2
result:
ok 2 number(s): "14 2"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
8 3 8 7 1
output:
22 7
result:
ok 2 number(s): "22 7"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
8 1 1
output:
28 1
result:
ok 2 number(s): "28 1"
Test #7:
score: 0
Accepted
time: 6ms
memory: 4176kb
input:
16 2 6 1
output:
77 15
result:
ok 2 number(s): "77 15"
Test #8:
score: 0
Accepted
time: 40ms
memory: 5568kb
input:
16 8 10 9 8 7 6 5 4 1
output:
57 5
result:
ok 2 number(s): "57 5"
Test #9:
score: 0
Accepted
time: 45ms
memory: 5564kb
input:
16 10 10 9 8 7 6 5 4 3 2 1
output:
57 3
result:
ok 2 number(s): "57 3"
Test #10:
score: 0
Accepted
time: 41ms
memory: 5616kb
input:
16 7 10 9 8 6 5 4 1
output:
49 1
result:
ok 2 number(s): "49 1"
Test #11:
score: 0
Accepted
time: 11ms
memory: 4116kb
input:
16 4 7 6 2 1
output:
52 9
result:
ok 2 number(s): "52 9"
Test #12:
score: 0
Accepted
time: 10ms
memory: 4360kb
input:
22 3 5 3 1
output:
100 1
result:
ok 2 number(s): "100 1"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
22 1 1
output:
231 1
result:
ok 2 number(s): "231 1"
Test #14:
score: 0
Accepted
time: 406ms
memory: 21672kb
input:
22 4 10 8 3 1
output:
97 4
result:
ok 2 number(s): "97 4"
Test #15:
score: 0
Accepted
time: 441ms
memory: 21900kb
input:
22 5 10 7 6 3 1
output:
92 70
result:
ok 2 number(s): "92 70"
Test #16:
score: 0
Accepted
time: 447ms
memory: 21676kb
input:
22 6 10 9 8 7 3 1
output:
97 1
result:
ok 2 number(s): "97 1"
Test #17:
score: 0
Accepted
time: 514ms
memory: 21588kb
input:
22 10 10 9 8 7 6 5 4 3 2 1
output:
109 1
result:
ok 2 number(s): "109 1"
Test #18:
score: 0
Accepted
time: 16ms
memory: 4480kb
input:
14 2 10 1
output:
61 210
result:
ok 2 number(s): "61 210"
Test #19:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
18 2 2 1
output:
117 1
result:
ok 2 number(s): "117 1"
Test #20:
score: 0
Accepted
time: 2243ms
memory: 83588kb
input:
30 2 9 1
output:
264 84
result:
ok 2 number(s): "264 84"
Test #21:
score: 0
Accepted
time: 1803ms
memory: 67564kb
input:
29 2 9 1
output:
253 36
result:
ok 2 number(s): "253 36"
Test #22:
score: 0
Accepted
time: 279ms
memory: 16388kb
input:
25 2 8 1
output:
195 8
result:
ok 2 number(s): "195 8"
Test #23:
score: -100
Time Limit Exceeded
input:
30 4 10 9 5 1