QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#639686#8529. Balance of Permutationucup-team134TL 5100ms983028kbC++179.0kb2024-10-13 21:32:562024-10-13 21:32:57

Judging History

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

  • [2024-10-13 21:32:57]
  • 评测
  • 测评结果:TL
  • 用时:5100ms
  • 内存:983028kb
  • [2024-10-13 21:32:56]
  • 提交

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;
gp_hash_table<int,li> bot[D+1][G2+1];
bool doneit[D+1][G2+1][M2];
li bot2[M2][2][D+1];
int K;
vector<int> mybot;
void reset(){
	K=sz(mybot);
	for(int i=0;i<=D;i++)for(int j=0;j<=K;j++)bot[i][j].clear();
}
vector<li> fact(G+1);
bool pastLim=false;
int emOffset;
li calc(int msk,int em,int treba){
	int x=__builtin_popcount(msk);
	bitset<M2> aa=msk;
	//cout << aa << " " << em << " " << treba << endl;
	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)return 0;
			//printf("Treba: %i\n",treba);
			//printf("Opcija: %i\n",(int)total[em+emOffset][treba]);
			puta[em+emOffset][treba]+=fact[em]*fact[em+emOffset];
			return total[em+emOffset][treba]*fact[em]*fact[em+emOffset];
		}
		else{
			assert(msk==0&&em==0);
			return treba==0;
		}
	}
	if(bot[treba][em].find(msk)!=bot[treba][em].end())return bot[treba][em][msk];
	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){
				val+=calc(msk^(1<<i),em,treba-ad);
			}
		}
	}
	if(sz(havetop)){
		int ad=K-emOffset-vecUzeo;
		if(ad<=treba){
			val+=calc(msk,em+1,treba-ad);
		}
	}
	bot[treba][em][msk]=val;
	return val;
}
li getbot(int treba){
	if(treba<0)return 0;
	reset();
	return calc((1<<K)-1,0,treba);
}
vector<int> ans;
int LIMIT;
int lastTr;
li calctr(){
	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));
	}
	return getbot(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++)bot2[i][0][j]=0;
	li anss=0;
	
	bot2[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++)bot2[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=bot2[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){
							bot2[msk^(1<<i)][my][treba-ad]+=myv;
						}
					}
				}
				if(sz(havetop)){
					int ad=K-emOffset-vecUzeo;
					if(ad<=treba){
						bot2[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=calctr();
	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=calc(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: 0ms
memory: 9712kb

input:

6 6 6

output:

1 2 6 3 4 5 

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 4954ms
memory: 983028kb

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: 2ms
memory: 11440kb

input:

1 0 1

output:

1 

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 2ms
memory: 11524kb

input:

2 0 1

output:

1 2 

result:

ok 2 number(s): "1 2"

Test #5:

score: 0
Accepted
time: 2ms
memory: 9460kb

input:

2 2 1

output:

2 1 

result:

ok 2 number(s): "2 1"

Test #6:

score: 0
Accepted
time: 2ms
memory: 9464kb

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: 0ms
memory: 9456kb

input:

7 20 100

output:

3 6 7 4 1 5 2 

result:

ok 7 numbers

Test #8:

score: 0
Accepted
time: 1ms
memory: 9400kb

input:

7 2 6

output:

2 1 3 4 5 6 7 

result:

ok 7 numbers

Test #9:

score: 0
Accepted
time: 2ms
memory: 9528kb

input:

7 24 1

output:

4 5 6 7 1 2 3 

result:

ok 7 numbers

Test #10:

score: 0
Accepted
time: 2ms
memory: 9464kb

input:

7 22 360

output:

7 6 4 3 5 2 1 

result:

ok 7 numbers

Test #11:

score: 0
Accepted
time: 0ms
memory: 9456kb

input:

7 20 358

output:

5 7 2 4 6 3 1 

result:

ok 7 numbers

Test #12:

score: 0
Accepted
time: 4ms
memory: 10204kb

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: 0ms
memory: 9940kb

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: 1060ms
memory: 404564kb

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: 1122ms
memory: 385208kb

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: 1279ms
memory: 413696kb

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: 1700ms
memory: 431824kb

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: 1779ms
memory: 456748kb

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: 5100ms
memory: 699644kb

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: -100
Time Limit Exceeded

input:

30 400 46479902466857426153849991132

output:


result: