QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#370019 | #8285. Shell Sort | SolitaryDream | AC ✓ | 4149ms | 184016kb | C++17 | 2.2kb | 2024-03-28 21:09:14 | 2024-03-28 21:09:15 |
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],ps[N][N];
int n,m,d[N];
int calc(int S,int p)
{
int cnt=0;
for(int i=1;i<=m;i++)
{
int nS=0,np=-1;
for(int j=0;j<d[i];j++)
{
int all=ps[i][j]&S;
int all0=ps[i][j]&~S;
int c0=__builtin_popcount(all0);
if(j==p%d[i])
{
cnt+=__builtin_popcount(all&((1<<p)-1));
cnt+=__builtin_popcount(all0>>(p+1));
}
if(all0)
nS|=ps[i][j]^bef[i][j+c0*d[i]-d[i]];
else
nS|=ps[i][j];
if(j==p%d[i])
np=c0*d[i]+j-d[i];
}
S=nS,p=np;
}
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][pos[i][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]];
w.first+=calc(hav,x);
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);
ps[i][j%d[i]]|=(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/2<<" "<<v<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3572kb
input:
2 2 2 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3576kb
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: 3864kb
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: 3560kb
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: 3656kb
input:
8 3 8 7 1
output:
22 7
result:
ok 2 number(s): "22 7"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
8 1 1
output:
28 1
result:
ok 2 number(s): "28 1"
Test #7:
score: 0
Accepted
time: 4ms
memory: 4132kb
input:
16 2 6 1
output:
77 15
result:
ok 2 number(s): "77 15"
Test #8:
score: 0
Accepted
time: 41ms
memory: 5576kb
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: 43ms
memory: 5580kb
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: 40ms
memory: 5640kb
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: 8ms
memory: 4152kb
input:
16 4 7 6 2 1
output:
52 9
result:
ok 2 number(s): "52 9"
Test #12:
score: 0
Accepted
time: 6ms
memory: 4144kb
input:
22 3 5 3 1
output:
100 1
result:
ok 2 number(s): "100 1"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
22 1 1
output:
231 1
result:
ok 2 number(s): "231 1"
Test #14:
score: 0
Accepted
time: 346ms
memory: 21832kb
input:
22 4 10 8 3 1
output:
97 4
result:
ok 2 number(s): "97 4"
Test #15:
score: 0
Accepted
time: 336ms
memory: 21664kb
input:
22 5 10 7 6 3 1
output:
92 70
result:
ok 2 number(s): "92 70"
Test #16:
score: 0
Accepted
time: 371ms
memory: 21616kb
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: 427ms
memory: 21612kb
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: 12ms
memory: 4416kb
input:
14 2 10 1
output:
61 210
result:
ok 2 number(s): "61 210"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
18 2 2 1
output:
117 1
result:
ok 2 number(s): "117 1"
Test #20:
score: 0
Accepted
time: 1344ms
memory: 83624kb
input:
30 2 9 1
output:
264 84
result:
ok 2 number(s): "264 84"
Test #21:
score: 0
Accepted
time: 1031ms
memory: 67572kb
input:
29 2 9 1
output:
253 36
result:
ok 2 number(s): "253 36"
Test #22:
score: 0
Accepted
time: 168ms
memory: 16592kb
input:
25 2 8 1
output:
195 8
result:
ok 2 number(s): "195 8"
Test #23:
score: 0
Accepted
time: 3568ms
memory: 184016kb
input:
30 4 10 9 5 1
output:
188 40
result:
ok 2 number(s): "188 40"
Test #24:
score: 0
Accepted
time: 4149ms
memory: 183864kb
input:
30 9 10 9 8 7 6 5 4 3 1
output:
184 6
result:
ok 2 number(s): "184 6"
Test #25:
score: 0
Accepted
time: 3979ms
memory: 183844kb
input:
30 8 10 9 8 7 4 3 2 1
output:
154 1
result:
ok 2 number(s): "154 1"
Test #26:
score: 0
Accepted
time: 3992ms
memory: 183748kb
input:
30 8 10 8 7 6 5 4 3 1
output:
155 1
result:
ok 2 number(s): "155 1"
Test #27:
score: 0
Accepted
time: 3762ms
memory: 184016kb
input:
30 6 10 8 6 4 3 1
output:
150 4
result:
ok 2 number(s): "150 4"
Test #28:
score: 0
Accepted
time: 4111ms
memory: 183820kb
input:
30 10 10 9 8 7 6 5 4 3 2 1
output:
184 6
result:
ok 2 number(s): "184 6"
Test #29:
score: 0
Accepted
time: 2873ms
memory: 138812kb
input:
29 6 10 9 7 5 3 1
output:
129 200
result:
ok 2 number(s): "129 200"
Extra Test:
score: 0
Extra Test Passed