QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#640890#8529. Balance of Permutationucup-team134AC ✓6521ms531704kbC++178.5kb2024-10-14 16:43:292024-10-14 16:43:29

Judging History

你现在查看的是最新测评结果

  • [2024-10-14 16:43:29]
  • 评测
  • 测评结果:AC
  • 用时:6521ms
  • 内存:531704kb
  • [2024-10-14 16:43:29]
  • 提交

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 bot[2][M][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(){
	bot[0][M-1][0]=1; // basecase
	for(int e=0;e<=G;e++){
		int my=e&1,other=my^1;
		memset(bot[other],0,sizeof bot[other]);
		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(bot[my][msk][uzeto]==0)continue;
				//cout << aa << " " << e << " " << uzeto << ": " << (int)bot[my][msk][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)bot[my][msk][uzeto]);
					if(myu>G*G)printf("%i %i\n",myu,G*G);
					assert(myu<=G*G);
					total[e][myu]+=bot[my][msk][uzeto];
					assert(cnt[msk][myu]==0);
					cnt[msk][myu]+=bot[my][msk][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);
						bot[my][msk^(1<<i)][uzeto+ad]+=bot[my][msk][uzeto];
					}
				}
				// Take some smaller 1
				int ad=sledeci+1;
				assert(ad+uzeto<=G*G);
				bot[other][msk][uzeto+ad]+=bot[my][msk][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];
int K;
vector<int> mybot;

vector<li> fact(G+1);

bool pastLim=false;
int emOffset;
vector<int> ans;
int LIMIT;

void dfs(int msk,int em,int treba){
	doneit[msk][em][treba]=1;
	int x=__builtin_popcount(msk);
	//cout << aa << " " << em << " " << treba << endl;
	if(K-x+em+emOffset==K){ // took everything, base case :)
		ordtodo[em].pb({msk,treba});
		return;
	}
	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 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++){
					value+=cnt[msk][k]*puta[em][k];
				}
			}
		}
	}
	return value;
}
void calcans(li poredu,li totalcnt){
	if(sz(ans)==n){
		assert(poredu==1);
		for(auto p:ans){
			printf("%i ",p+1);
		}
		printf("\n");
		return;
	}
	if(sz(ans)==LIMIT){
		for(auto p:havetop)havebot.insert(p);
		havetop.clear();
		LIMIT=N+1;
		pastLim=1;
		b+=dodatniTreba;
		dodatniTreba=0;
	}
	
	// Take something from bot
	// Recalc from answer
	int treba=b;
	emOffset=0;
	for(int i=0;i<sz(ans);i++){
		if(ans[i]<LIMIT){
			treba-=abs(i-ans[i]);
		}
		else{
			emOffset++;
			treba-=LIMIT-i;
		}
	}
	mybot.clear();
	for(auto d:havebot){
		mybot.pb(d-sz(ans));
	}
	if(treba<0)assert(0);
	K=sz(mybot);
	
	// Clear DFS
	memset(doneit,0,sizeof doneit);
	int MM=1<<K;
	for(int i=0;i<=G2;i++)
		ordtodo[i].clear();
	
	// DFS for case of taking bigger
	int takeEl=LIMIT-sz(ans);
	int novitreba=treba-takeEl;
	li starting=0;
	li totalTop=0;
	if(!pastLim&&novitreba>=0){ // only if there are any options
		//printf("Calculating!\n");
		for(int i=0;i<=G;i++)for(int j=0;j<=G*G;j++)puta[i][j]=0;
		memset(bot[1],0,sizeof bot[1]);
		bot[1][MM-1][novitreba]=1; // basecase when skipping first for bigger
		dfs(MM-1,1,novitreba);
		for(int em=1;em<=G;em++){
			int my=em&1,other=my^1;
			memset(bot[other],0,sizeof bot[other]);
			for(int ii=sz(ordtodo[em])-1;ii>=0;ii--){
				int msk=ordtodo[em][ii].f,t=ordtodo[em][ii].s;
				int x=__builtin_popcount(msk);
				li myv=bot[my][msk][t];
				if(myv==0)continue;
				//printf("%i %i %i!\n",em,msk,t);
				
				if(K-x+em+emOffset==K){ // took everything, base case :)
					for(int i=0;i<K;i++){
						if(msk&(1<<i)){
							t-=K-emOffset-1-mybot[i];
						}
					}
					t+=dodatniTreba;
					if(t<0||t>G*G)continue;
					//printf("Treba: %i\n",t);
					//printf("Opcija: %i\n",(int)total[em+emOffset][t]);
					
					puta[em+emOffset][t]+=fact[em-1]*fact[em+emOffset]*myv;
					starting+=total[em+emOffset][t]*fact[em-1]*fact[em+emOffset]*myv;
					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<=t){
							bot[my][msk^(1<<i)][t-ad]+=myv;
						}
					}
				}
				int ad=K-emOffset-vecUzeo;
				if(ad<=t){
					bot[other][msk][t-ad]+=myv;
				}
			}
		}
		//printf("Opcija: %i\n",(int)starting);
		for(auto p:havetop){
			li val=removeVal(p-LIMIT);
			li my=starting-val;
			totalTop+=my;
		}
		//printf("Totalno top: %i\n",(int)totalTop);
	}
	
	if(totalcnt==-1||poredu<=totalcnt-totalTop){
		// calculate full DP
		dfs(MM-1,0,treba);
		for(int em=G2;em>=0;em--){
			int my=em&1,other=my^1;
			for(auto p:ordtodo[em]){
				int msk=p.f,tr=p.s;
				int t=tr;
				int x=__builtin_popcount(msk);
				bot[my][msk][tr]=0;
					
				if(K-x+em+emOffset==K){
					if(!pastLim){
						for(int i=0;i<K;i++){
							if(msk&(1<<i)){
								tr-=K-emOffset-1-mybot[i];
							}
						}
						tr+=dodatniTreba;
						if(tr<0||tr>G*G)continue;
						
						bot[my][msk][t]=total[em+emOffset][tr]*fact[em]*fact[em+emOffset];
						continue;
					}
					else{
						assert(msk==0&&em==0);
						bot[my][msk][tr]=tr==0;
						continue;
					}
				}
				int vecUzeo=K-x+em;
				li mojval=0;
				for(int i=0;i<K;i++){
					if(msk&(1<<i)){
						int taking=mybot[i];
						int ad=abs(taking-vecUzeo);
						if(ad<=tr){
							mojval+=bot[my][msk^(1<<i)][tr-ad];
						}
					}
				}
				if(!pastLim){
					int ad=K-emOffset-vecUzeo;
					if(ad<=tr){
						mojval+=bot[other][msk][tr-ad];
					}
				}
				bot[my][msk][tr]=mojval;
			}
		}
		totalcnt=bot[0][MM-1][treba];
	}
	//printf("Total: %i\n",(int)totalcnt);
	if(!pastLim&&poredu>totalcnt-totalTop){ // val is in the top
		poredu-=totalcnt-totalTop;
		vector<int> opt;
		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("Opcija2 %i: %i\n",opt[i],(int)my);
			if(my>=poredu){
				ans.pb(p);
				remove(p-LIMIT);
				havetop.erase(p);
				calcans(poredu,my);
				return;
			}
			poredu-=my;
		}
		assert(0);
	}
	
	//value is in the bottom
	
	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<=treba){
			my=bot[0][msk^(1<<i)][treba-ad];
		}
		//printf("Opcija %i: %i\n",opt[i],(int)my);
		if(my>=poredu){
			ans.pb(opt[i]);
			havebot.erase(opt[i]);
			calcans(poredu,my);
			return;
		}
		poredu-=my;
	}
	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';
	}
	//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,-1);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 56ms
memory: 122360kb

input:

6 6 6

output:

1 2 6 3 4 5 

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 5632ms
memory: 524604kb

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: 4ms
memory: 122136kb

input:

1 0 1

output:

1 

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 12ms
memory: 123244kb

input:

2 0 1

output:

1 2 

result:

ok 2 number(s): "1 2"

Test #5:

score: 0
Accepted
time: 8ms
memory: 122564kb

input:

2 2 1

output:

2 1 

result:

ok 2 number(s): "2 1"

Test #6:

score: 0
Accepted
time: 43ms
memory: 123088kb

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: 52ms
memory: 122296kb

input:

7 20 100

output:

3 6 7 4 1 5 2 

result:

ok 7 numbers

Test #8:

score: 0
Accepted
time: 56ms
memory: 122324kb

input:

7 2 6

output:

2 1 3 4 5 6 7 

result:

ok 7 numbers

Test #9:

score: 0
Accepted
time: 60ms
memory: 122992kb

input:

7 24 1

output:

4 5 6 7 1 2 3 

result:

ok 7 numbers

Test #10:

score: 0
Accepted
time: 56ms
memory: 122596kb

input:

7 22 360

output:

7 6 4 3 5 2 1 

result:

ok 7 numbers

Test #11:

score: 0
Accepted
time: 64ms
memory: 122540kb

input:

7 20 358

output:

5 7 2 4 6 3 1 

result:

ok 7 numbers

Test #12:

score: 0
Accepted
time: 80ms
memory: 127576kb

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: 96ms
memory: 125000kb

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: 2546ms
memory: 471408kb

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: 2609ms
memory: 469416kb

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: 2752ms
memory: 472920kb

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: 2963ms
memory: 476020kb

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: 3377ms
memory: 481956kb

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: 4215ms
memory: 493076kb

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: 6345ms
memory: 529792kb

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: 0
Accepted
time: 6228ms
memory: 526228kb

input:

30 450 1140008168482799670544355

output:

26 16 17 18 19 20 21 22 23 24 25 27 28 29 30 1 2 3 5 9 4 8 14 10 6 11 12 15 7 13 

result:

ok 30 numbers

Test #22:

score: 0
Accepted
time: 5894ms
memory: 524188kb

input:

30 150 480087379811286955791425915

output:

7 4 8 5 16 3 1 12 13 11 9 10 15 25 18 17 20 30 28 2 6 14 23 21 24 26 27 22 19 29 

result:

ok 30 numbers

Test #23:

score: 0
Accepted
time: 5974ms
memory: 524288kb

input:

30 150 480087379811286955791439470

output:

7 4 8 5 16 3 1 12 13 11 9 10 15 25 18 17 20 30 28 2 19 6 22 24 21 23 26 14 29 27 

result:

ok 30 numbers

Test #24:

score: 0
Accepted
time: 6238ms
memory: 528552kb

input:

30 440 41509275104334759322587324

output:

22 23 20 24 18 30 19 26 21 28 4 29 17 25 27 16 3 1 2 5 8 13 10 15 7 12 9 14 11 6 

result:

ok 30 numbers

Test #25:

score: 0
Accepted
time: 6221ms
memory: 527308kb

input:

30 450 1140008168482800727111311

output:

26 16 17 18 19 20 21 22 23 24 25 27 28 29 30 1 2 5 7 14 4 15 8 11 3 13 10 9 6 12 

result:

ok 30 numbers

Test #26:

score: 0
Accepted
time: 6379ms
memory: 527976kb

input:

30 400 52289890275214604423031772929

output:

26 27 29 21 28 16 18 11 2 25 24 23 6 30 20 13 17 10 15 4 9 12 8 22 19 1 5 7 3 14 

result:

ok 30 numbers

Test #27:

score: 0
Accepted
time: 790ms
memory: 466828kb

input:

30 0 1

output:

1 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 

result:

ok 30 numbers

Test #28:

score: 0
Accepted
time: 6210ms
memory: 529320kb

input:

30 450 1

output:

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

result:

ok 30 numbers

Test #29:

score: 0
Accepted
time: 6326ms
memory: 524244kb

input:

30 450 1710012252724199424000000

output:

30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 

result:

ok 30 numbers

Test #30:

score: 0
Accepted
time: 6290ms
memory: 527544kb

input:

30 450 1692383260428073656742269

output:

30 27 26 28 18 29 21 19 25 17 20 16 24 22 23 7 13 4 6 3 5 12 1 15 14 9 11 8 2 10 

result:

ok 30 numbers

Test #31:

score: 0
Accepted
time: 6521ms
memory: 525368kb

input:

30 302 5918364042599361729860937331200

output:

30 29 28 27 26 25 14 8 9 10 11 12 13 7 15 16 17 18 19 20 21 22 23 24 6 5 4 3 2 1 

result:

ok 30 numbers

Test #32:

score: 0
Accepted
time: 6209ms
memory: 531704kb

input:

30 254 2256781660157136563723839089600

output:

25 2 3 12 7 16 19 8 22 6 11 17 27 26 10 24 15 21 20 18 28 9 30 23 14 13 5 29 4 1 

result:

ok 30 numbers

Test #33:

score: 0
Accepted
time: 6159ms
memory: 525172kb

input:

30 448 3131906441000512625049600

output:

23 20 28 18 26 30 19 29 27 22 17 24 21 25 2 13 16 15 14 12 11 10 9 8 7 6 5 4 3 1 

result:

ok 30 numbers

Test #34:

score: 0
Accepted
time: 789ms
memory: 466916kb

input:

30 2 20

output:

1 2 3 4 5 6 7 8 9 11 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 

result:

ok 30 numbers

Test #35:

score: 0
Accepted
time: 808ms
memory: 466960kb

input:

30 2 29

output:

2 1 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 

result:

ok 30 numbers

Extra Test:

score: 0
Extra Test Passed