QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#639590#8529. Balance of Permutationucup-team134RE 3ms12404kbC++177.6kb2024-10-13 20:39:162024-10-13 20:39:16

Judging History

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

  • [2024-10-13 20:39:16]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:12404kb
  • [2024-10-13 20:39:16]
  • 提交

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);
gp_hash_table<int,li> bot[661][G2+1];
bool doneit[661][G2+1][M2];
int K;
vector<int> mybot;
void reset(){
	K=sz(mybot);
	for(int i=0;i<661;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];
				}
			}
			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);
}
li processBot(){
	reset();
	for(int i=0;i<=G;i++)for(int j=0;j<=G*G;j++)puta[i][j]=0;
	queue<pair<pair<int,int>,int>> q;
	q.push({{(1<<K)-1,0},lastTr});
	bot[lastTr][0][(1<<K)-1]=1;
	doneit[lastTr][0][(1<<K)-1]=1;
	memset(doneit,0,sizeof doneit);
	li anss=0;
	while(sz(q)){
		int msk=q.front().f.s,em=q.front().f.s,treba=q.front().s;
		q.pop();
		li my=bot[treba][em][msk];
		int x=__builtin_popcount(msk);
		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];
					}
				}
				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]*my;
				anss+=total[em+emOffset][treba]*fact[em]*fact[em+emOffset]*my;
				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[treba-ad][em][msk^(1<<i)]+=my;
					if(!doneit[treba-ad][em][msk^(1<<i)]){
						q.push({{msk^(1<<i),em},treba-ad});
						doneit[treba-ad][em][msk^(1<<i)]=1;
					}
				}
			}
		}
		if(sz(havetop)){
			int ad=K-emOffset-vecUzeo;
			if(ad<=treba){
				bot[treba-ad][em+1][msk]+=my;
				if(!doneit[treba-ad][em+1][msk]){
					q.push({{msk,em+1},treba-ad});
					doneit[treba-ad][em+1][msk]=1;
				}
			}
		}
	}
	return anss;
}
void calcans(li treba){
	if(sz(ans)==n){
		assert(treba==1);
		for(auto p:ans){
			printf("%i ",p+1);
		}
		printf("\n");
		return;
	}
	/*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;
	}
	// Take something from bot
	li totalCnt=calctr();
	//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
		processBot();
		// Take something from top
		opt.clear();
		for(auto p:havetop)opt.pb(p);
		for(auto p:opt){
			havetop.erase(p);
			ans.pb(p);
			remove(p-LIMIT);
	
			li my=processBot();
			//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);
	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);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 12404kb

input:

6 6 6

output:

1 2 6 3 4 5 

result:

ok 6 numbers

Test #2:

score: -100
Runtime Error

input:

30 300 3030303030303030303030

output:


result: