QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#639279 | #8529. Balance of Permutation | ucup-team134 | TL | 3908ms | 614484kb | C++17 | 5.8kb | 2024-10-13 18:41:35 | 2024-10-13 18:41:35 |
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 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];
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]);
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;
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
vector<int> opt;
for(auto p:havebot)opt.pb(p);
for(auto p:opt){
havebot.erase(p);
ans.pb(p);
int tr=b;
emOffset=0;
for(int i=0;i<sz(ans);i++){
if(ans[i]<LIMIT){
// small
tr-=abs(i-ans[i]);
}
else{
emOffset++;
tr-=LIMIT-i;
}
}
mybot.clear();
for(auto d:havebot){
mybot.pb(d-sz(ans));
}
li my=getbot(tr);
//printf("Pokusavam %i: %i\n",p,(int)my);
if(my>=treba){
calcans(treba);
return;
}
treba-=my;
ans.pop_back();
havebot.insert(p);
}
// 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);
int tr=b;
emOffset=0;
for(int i=0;i<sz(ans);i++){
if(ans[i]<LIMIT){
// small
tr-=abs(i-ans[i]);
}
else{
emOffset++;
tr-=LIMIT-i;
}
}
//printf("Limit = %i\n",LIMIT);
mybot.clear();
for(auto d:havebot){
mybot.pb(d-sz(ans));
}
li my=getbot(tr);
//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: 0ms
memory: 11128kb
input:
6 6 6
output:
1 2 6 3 4 5
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 3086ms
memory: 614484kb
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: 0ms
memory: 13160kb
input:
1 0 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 11168kb
input:
2 0 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #5:
score: 0
Accepted
time: 3ms
memory: 11156kb
input:
2 2 1
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 11160kb
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: 4ms
memory: 11184kb
input:
7 20 100
output:
3 6 7 4 1 5 2
result:
ok 7 numbers
Test #8:
score: 0
Accepted
time: 0ms
memory: 11168kb
input:
7 2 6
output:
2 1 3 4 5 6 7
result:
ok 7 numbers
Test #9:
score: 0
Accepted
time: 5ms
memory: 12628kb
input:
7 24 1
output:
4 5 6 7 1 2 3
result:
ok 7 numbers
Test #10:
score: 0
Accepted
time: 0ms
memory: 12916kb
input:
7 22 360
output:
7 6 4 3 5 2 1
result:
ok 7 numbers
Test #11:
score: 0
Accepted
time: 4ms
memory: 11208kb
input:
7 20 358
output:
5 7 2 4 6 3 1
result:
ok 7 numbers
Test #12:
score: 0
Accepted
time: 10ms
memory: 11360kb
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: 9ms
memory: 12568kb
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: 1515ms
memory: 380092kb
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: 1527ms
memory: 364380kb
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: 1983ms
memory: 376900kb
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: 3071ms
memory: 391384kb
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: 3908ms
memory: 403784kb
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: -100
Time Limit Exceeded
input:
29 322 40281026669581503094652149519