QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#639753 | #8529. Balance of Permutation | ucup-team134 | TL | 7954ms | 763384kb | C++17 | 9.8kb | 2024-10-13 22:11:41 | 2024-10-13 22:11:42 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ld long double
#define li __int128
using namespace __gnu_pbds;
using namespace std;
mt19937 rng(time(NULL));
const int N=30,G=15,M=1<<G;
li top[M][2][G*G+1];
int n,b;
li total[G+1][G*G+1];
li puta[G+1][G*G+1];
li cnt[M][G*G+1];
bool done[M];
void calctop(){
top[M-1][0][0]=1; // basecase
for(int e=0;e<=G;e++){
int my=e&1,other=my^1;
for(int i=0;i<M;i++)for(int j=0;j<=G*G;j++)top[i][other][j]=0;
for(int msk=M-1;msk>=0;msk--){
int x=__builtin_popcount(msk);
int nema=G-x + e;
int sledeci=G-nema-1;
for(int uzeto=0;uzeto<=G*G;uzeto++){
if(top[msk][my][uzeto]==0)continue;
bitset<G> aa=msk;
//cout << aa << " " << e << " " << uzeto << ": " << (int)top[msk][my][uzeto] << endl;
if(nema==G){
int myu=uzeto;
for(int i=0;i<G;i++){
if(msk&(1<<i)){
myu+=i;
}
}
//cout << aa << " ";
//printf("%i %i += %i\n",e,myu,(int)top[msk][my][uzeto]);
if(myu>G*G)printf("%i %i\n",myu,G*G);
assert(myu<=G*G);
total[e][myu]+=top[msk][my][uzeto];
assert(cnt[msk][myu]==0);
cnt[msk][myu]+=top[msk][my][uzeto];
continue;
}
// Take it
for(int i=0;i<G;i++){
int ad=abs(i-sledeci);
if(msk&(1<<i)){
assert(ad+uzeto<=G*G);
top[msk^(1<<i)][my][uzeto+ad]+=top[msk][my][uzeto];
}
}
// Take some smaller 1
int ad=sledeci+1;
assert(ad+uzeto<=G*G);
top[msk][other][uzeto+ad]+=top[msk][my][uzeto];
}
}
}
}
vector<int> tk;
void remove(int i){
tk.clear();
for(int msk=0;msk<M;msk++){
if(!(msk&(1<<i))){
if(!done[msk]){
done[msk]=1;
tk.pb(msk);
int em=__builtin_popcount(msk);
for(int k=0;k<=G*G;k++){
//printf("%i %i -= %i\n",em,k,(int)cnt[msk][k]);
total[em][k]-=cnt[msk][k];
}
}
}
}
}
void undo(){
for(auto msk:tk){
done[msk]=0;
int em=__builtin_popcount(msk);
for(int k=0;k<=G*G;k++){
total[em][k]+=cnt[msk][k];
}
}
}
set<int> havebot,havetop;
const int G2=N-G,M2=(1<<G2),D=G2*G2;
int dodatniTreba;
bool doneit[M2][G2+1][D+1];
vector<pair<int,int>> ordtodo[G2+1];
li bot[M2][2][D+1];
int K;
vector<int> mybot;
void reset(){
K=sz(mybot);
}
vector<li> fact(G+1);
bool pastLim=false;
int emOffset;
int lastTr;
vector<int> ans;
int LIMIT;
void dfs(int msk,int em,int treba){
doneit[msk][em][treba]=1;
int x=__builtin_popcount(msk);
bitset<M2> aa=msk;
//cout << aa << " " << em << " " << treba << endl;
if(K-x+em+emOffset==K){ // took everything, base case :)
ordtodo[em].pb({msk,treba});
return;
}
li val=0;
int vecUzeo=K-x+em;
for(int i=0;i<K;i++){
if(msk&(1<<i)){
int taking=mybot[i];
int ad=abs(taking-vecUzeo);
if(ad<=treba){
if(!doneit[msk^(1<<i)][em][treba-ad]){
dfs(msk^(1<<i),em,treba-ad);
}
}
}
}
if(sz(havetop)){
int ad=K-emOffset-vecUzeo;
if(ad<=treba){
if(!doneit[msk][em+1][treba-ad]){
dfs(msk,em+1,treba-ad);
}
}
}
ordtodo[em].pb({msk,treba});
}
li calc(){
lastTr=b;
emOffset=0;
for(int i=0;i<sz(ans);i++){
if(ans[i]<LIMIT){
// small
lastTr-=abs(i-ans[i]);
}
else{
emOffset++;
lastTr-=LIMIT-i;
}
}
mybot.clear();
for(auto d:havebot){
mybot.pb(d-sz(ans));
}
if(lastTr<0)return 0;
reset();
memset(doneit,0,sizeof doneit);
int MM=1<<K;
for(int i=0;i<=G2;i++)
ordtodo[i].clear();
dfs(MM-1,0,lastTr);
for(int em=G2;em>=0;em--){
int my=em&1,other=my^1;
for(auto p:ordtodo[em]){
int msk=p.f,treba=p.s;
int t=treba;
int x=__builtin_popcount(msk);
bot[msk][my][treba]=0;
if(K-x+em+emOffset==K){ // took everything, base case :)
if(!pastLim){
for(int i=0;i<K;i++){
if(msk&(1<<i)){
treba-=K-emOffset-1-mybot[i];
}
}
treba+=dodatniTreba;
if(treba<0||treba>G*G)continue;
//printf("Treba: %i\n",treba);
//printf("Opcija: %i\n",(int)total[em+emOffset][treba]);
bot[msk][my][t]=total[em+emOffset][treba]*fact[em]*fact[em+emOffset];
continue;
}
else{
assert(msk==0&&em==0);
bot[msk][my][treba]=treba==0;
continue;
}
}
int vecUzeo=K-x+em;
for(int i=0;i<K;i++){
if(msk&(1<<i)){
int taking=mybot[i];
int ad=abs(taking-vecUzeo);
if(ad<=treba){
bot[msk][my][treba]+=bot[msk^(1<<i)][my][treba-ad];
}
}
}
if(sz(havetop)){
int ad=K-emOffset-vecUzeo;
if(ad<=treba){
bot[msk][my][treba]+=bot[msk][other][treba-ad];
}
}
}
}
return bot[MM-1][0][lastTr];
}
queue<pair<pair<int,int>,int>> q;
li processBot(){
lastTr=b;
emOffset=0;
for(int i=0;i<sz(ans);i++){
if(ans[i]<LIMIT){
// small
lastTr-=abs(i-ans[i]);
}
else{
emOffset++;
lastTr-=LIMIT-i;
}
}
mybot.clear();
for(auto d:havebot){
mybot.pb(d-sz(ans));
}
if(lastTr<0)return 0;
reset();
int MM=1<<K;
for(int i=0;i<=G;i++)for(int j=0;j<=G*G;j++)puta[i][j]=0;
for(int i=0;i<MM;i++)for(int j=0;j<=D;j++)bot[i][0][j]=0;
li anss=0;
bot[MM-1][0][lastTr]=1; // basecase
for(int em=0;em<=G;em++){
int my=em&1,other=my^1;
for(int i=0;i<MM;i++)for(int j=0;j<=D;j++)bot[i][other][j]=0;
for(int msk=MM-1;msk>=0;msk--){
int x=__builtin_popcount(msk);
for(int t=0;t<=D;t++){
int treba=t;
li myv=bot[msk][my][treba];
if(myv==0)continue;
if(K-x+em+emOffset==K){ // took everything, base case :)
if(!pastLim){
for(int i=0;i<K;i++){
if(msk&(1<<i)){
treba-=K-emOffset-1-mybot[i];
}
}
treba+=dodatniTreba;
if(treba<0||treba>G*G)continue;
//printf("Treba: %i\n",treba);
//printf("Opcija: %i\n",(int)total[em+emOffset][treba]);
puta[em+emOffset][treba]+=fact[em]*fact[em+emOffset]*myv;
anss+=total[em+emOffset][treba]*fact[em]*fact[em+emOffset]*myv;
continue;
}
else{
assert(0);
}
}
int vecUzeo=K-x+em;
for(int i=0;i<K;i++){
if(msk&(1<<i)){
int taking=mybot[i];
int ad=abs(taking-vecUzeo);
if(ad<=treba){
bot[msk^(1<<i)][my][treba-ad]+=myv;
}
}
}
if(sz(havetop)){
int ad=K-emOffset-vecUzeo;
if(ad<=treba){
bot[msk][other][treba-ad]+=myv;
}
}
}
}
}
return anss;
}
li removeVal(int i){
li value=0;
for(int msk=0;msk<M;msk++){
if(!(msk&(1<<i))){
if(!done[msk]){
int em=__builtin_popcount(msk);
for(int k=0;k<=G*G;k++){
//printf("%i %i -= %i\n",em,k,(int)cnt[msk][k]);
value+=cnt[msk][k]*puta[em][k];
//total[em][k]-=cnt[msk][k];
}
}
}
}
return value;
}
ll starttim;
ll tima,timb;
void calcans(li treba){
if(sz(ans)==n){
assert(treba==1);
for(auto p:ans){
printf("%i ",p+1);
}
printf("\n");
return;
}
//printf("%i: %lld\n",sz(ans),clock()-starttim);
/*printf("%i!\n",sz(ans));
for(auto p:ans)printf("%i ",p);
printf("\n");
for(auto p:havebot)printf("%i ",p);
printf("\n");
for(auto p:havetop)printf("%i ",p);
printf("\n");*/
if(sz(ans)==LIMIT){
// Convert all the top to bot case
// TODO
for(auto p:havetop)havebot.insert(p);
havetop.clear();
LIMIT=N+1;
pastLim=1;
b+=dodatniTreba;
dodatniTreba=0;
}
// Take something from bot
ll startt=clock();
li totalCnt=calc();
tima+=clock()-startt;
//printf("Total: %i\n",(int)totalCnt);
vector<int> opt;
for(auto p:havebot)opt.pb(p);
int msk=(1<<K)-1;
for(int i=0;i<sz(mybot);i++){
int taking=mybot[i];
int ad=abs(taking);
li my=0;
if(ad<=lastTr){
my=bot[msk^(1<<i)][0][lastTr-ad];
}
//printf("Pokusavam %i: %i\n",opt[i],(int)my);
if(my>=treba){
ans.pb(opt[i]);
havebot.erase(opt[i]);
calcans(treba);
return;
}
treba-=my;
}
/*for(auto p:opt){
havebot.erase(p);
ans.pb(p);
li my=calctr();
printf("Pokusavam %i: %i\n",p,(int)my);
if(my>=treba){
calcans(treba);
return;
}
treba-=my;
ans.pop_back();
havebot.insert(p);
}*/
if(sz(havetop)){
// calculate
startt=clock();
ans.pb(LIMIT);
li starting=processBot();
ans.pop_back();
timb+=clock()-startt;
// Take something from top
opt.clear();
for(auto p:havetop)opt.pb(p);
for(int i=0;i<sz(opt);i++){
int p=opt[i];
li val=removeVal(p-LIMIT);
li my=starting-val;
//printf("Pokusavam 2 %i: %i\n",p,(int)my);
if(my>=treba){
ans.pb(p);
remove(p-LIMIT);
havetop.erase(p);
calcans(treba);
return;
}
treba-=my;
}
/*for(auto p:opt){
havetop.erase(p);
ans.pb(p);
remove(p-LIMIT);
li my=calctr();
//printf("Pokusavam 2 %i: %i\n",p,(int)my);
if(my>=treba){
calcans(treba);
return;
}
treba-=my;
ans.pop_back();
havetop.insert(p);
undo();
}*/
}
assert(0);
}
int main()
{
fact[0]=1;
for(int i=1;i<=G;i++)fact[i]=i*fact[i-1];
scanf("%i %i",&n,&b);
if(b>G2*G2){
dodatniTreba=b-G2*G2;
b=G2*G2;
}
int mx=0;
for(int i=0;i<n;i++){
mx+=abs(i-(n-1-i));
}
//printf("%i!\n",mx);
string s;
cin >> s;
li k=0;
for(auto p:s){
k*=10;
k+=p-'0';
}
starttim=clock();
//n=b=k=6;
if(n>G){
calctop();
//printf("Done calctop!\n");
for(int i=0;i<n-G;i++){
havebot.insert(i);
}
for(int i=n-G;i<n;i++)havetop.insert(i);
LIMIT=n-G;
}
else{
for(int i=0;i<n;i++)havebot.insert(i);
LIMIT=n;pastLim=1;
}
calcans(k);
//printf("%lld %lld\n",tima,timb);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 46ms
memory: 122984kb
input:
6 6 6
output:
1 2 6 3 4 5
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 2248ms
memory: 755636kb
input:
30 300 3030303030303030303030
output:
1 2 3 4 9 23 20 28 24 16 21 17 27 29 8 26 25 30 19 18 22 12 7 13 6 10 5 15 14 11
result:
ok 30 numbers
Test #3:
score: 0
Accepted
time: 12ms
memory: 124724kb
input:
1 0 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 7ms
memory: 124788kb
input:
2 0 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #5:
score: 0
Accepted
time: 8ms
memory: 122972kb
input:
2 2 1
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: 0
Accepted
time: 44ms
memory: 122780kb
input:
5 8 3
output:
1 5 4 2 3
result:
ok 5 number(s): "1 5 4 2 3"
Test #7:
score: 0
Accepted
time: 53ms
memory: 123472kb
input:
7 20 100
output:
3 6 7 4 1 5 2
result:
ok 7 numbers
Test #8:
score: 0
Accepted
time: 52ms
memory: 122824kb
input:
7 2 6
output:
2 1 3 4 5 6 7
result:
ok 7 numbers
Test #9:
score: 0
Accepted
time: 50ms
memory: 123200kb
input:
7 24 1
output:
4 5 6 7 1 2 3
result:
ok 7 numbers
Test #10:
score: 0
Accepted
time: 58ms
memory: 127132kb
input:
7 22 360
output:
7 6 4 3 5 2 1
result:
ok 7 numbers
Test #11:
score: 0
Accepted
time: 52ms
memory: 123268kb
input:
7 20 358
output:
5 7 2 4 6 3 1
result:
ok 7 numbers
Test #12:
score: 0
Accepted
time: 78ms
memory: 133424kb
input:
10 48 10001
output:
7 5 8 9 6 10 3 4 1 2
result:
ok 10 numbers
Test #13:
score: 0
Accepted
time: 83ms
memory: 133260kb
input:
10 42 10101
output:
3 9 6 8 10 5 7 2 1 4
result:
ok 10 numbers
Test #14:
score: 0
Accepted
time: 990ms
memory: 702828kb
input:
25 300 1
output:
7 14 15 16 17 18 19 20 21 22 23 24 25 1 2 3 4 5 6 8 9 10 11 12 13
result:
ok 25 numbers
Test #15:
score: 0
Accepted
time: 1185ms
memory: 700244kb
input:
25 300 283788388040048639877
output:
25 24 23 22 21 20 19 18 17 16 11 12 13 14 15 10 9 8 7 5 6 4 2 1 3
result:
ok 25 numbers
Test #16:
score: 0
Accepted
time: 1277ms
memory: 702952kb
input:
26 302 105773752969551707419545
output:
19 22 25 13 17 18 23 20 10 26 16 6 5 11 14 12 24 4 3 21 1 15 7 8 2 9
result:
ok 26 numbers
Test #17:
score: 0
Accepted
time: 1503ms
memory: 705404kb
input:
27 308 8781128321749037280676555
output:
16 18 17 21 25 6 20 24 22 15 27 5 7 8 2 9 26 13 1 3 14 10 23 19 4 11 12
result:
ok 27 numbers
Test #18:
score: 0
Accepted
time: 1448ms
memory: 710052kb
input:
28 304 806517199954337651602356955
output:
12 17 5 16 23 26 25 15 20 2 19 7 22 24 6 13 11 10 28 8 1 21 18 14 27 3 4 9
result:
ok 28 numbers
Test #19:
score: 0
Accepted
time: 3013ms
memory: 724156kb
input:
29 322 40281026669581503094652149519
output:
16 21 10 25 17 29 9 28 2 8 26 27 22 4 3 5 18 14 19 1 23 20 15 11 13 7 6 12 24
result:
ok 29 numbers
Test #20:
score: 0
Accepted
time: 7954ms
memory: 763384kb
input:
30 400 46479902466857426153849991132
output:
25 19 30 29 9 20 26 21 14 27 28 10 22 11 24 2 7 4 18 17 5 13 12 6 8 1 15 23 16 3
result:
ok 30 numbers
Test #21:
score: -100
Time Limit Exceeded
input:
30 450 1140008168482799670544355