# Solution to the problem of [2020 provincial election simulation]

Portal

### T1:

The examination room wrote a
The method of false complexity of recursive merging
I'm a fool
90 points
Put recursion and merge to see which is put in front and then use heap for maintenance

```#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
return (ib==ob)?EOF:*ib++;
}
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=200005;
int v[N],a1[N],a2[N],n;
#define lc (u<<1)
#define rc ((u<<1)|1)
#define mid ((l+r)>>1)
struct Seg{
pii tr[N<<2];
inline pii merge(cs pii &a,cs pii &b){return a.fi<b.fi?a:b;}
void build(int u,int l,int r,int *a){
if(l==r){tr[u].se=l;tr[u].fi=a[l];return;}
build(lc,l,mid,a),build(rc,mid+1,r,a);
tr[u]=merge(tr[lc],tr[rc]);
}
pii query(int u,int l,int r,int st,int des){
if(st<=l&&r<=des)return tr[u];
if(des<=mid)return query(lc,l,mid,st,des);
if(mid<st)return query(rc,mid+1,r,st,des);
return merge(query(lc,l,mid,st,des),query(rc,mid+1,r,st,des));
}
}Seg[2];
#undef lc
#undef rc
#undef mid
struct node{
int l,r,vl,vr;
node(int a=0,int b=0,int c=0,int d=0):l(a),r(b),vl(c),vr(d){}
friend inline bool operator <(cs node &a,cs node &b){
return v[a.vl]>v[b.vl];
}
};
priority_queue<node>q;
inline node get(int l,int r){
pii p=Seg[l&1].query(1,1,n,l,r),q=Seg[(l&1)^1].query(1,1,n,p.se+1,r);
return node(l,r,p.se,q.se);
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
//	freopen("my.out","w",stdout);
#endif
for(int i=1;i<=n;i++){
if(i&1)a1[i]=v[i],a2[i]=1e9;
else a2[i]=v[i],a1[i]=1e9;
}
Seg[1].build(1,1,n,a1),Seg[0].build(1,1,n,a2);
q.push(get(1,n));
while(q.size()){
node now=q.top();q.pop();
cout<<v[now.vl]<<" "<<v[now.vr]<<" ";
if(now.l<now.vl)q.push(get(now.l,now.vl-1));
if(now.vl<now.vr-1)q.push(get(now.vl+1,now.vr-1));
if(now.vr<now.r)q.push(get(now.vr+1,now.r));
}
return 0;
}
```

### T2:

Popular group digit dpdpdp

I wrote a log2nlog^2nlog2n method
But it seems that the logloglog is easier to write?
I'm mentally retarded

```#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
return (ib==ob)?EOF:*ib++;
}
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
ll pw[20];
inline int getin(char c){
if(isdigit(c))return c-'0';
else return 10+c-'A';
}
inline void write(int x){
if(x<10)cout<<x;
else putchar('A'+x-10);
}
namespace solve1{
int dig[20],cnt;
inline void calc1(ll pos,int res){
cnt=0;
while(pos)dig[++cnt]=pos%16,pos/=16;
reverse(dig+1,dig+cnt+1);
write(dig[res]);puts("");
}
inline void solve(ll n){
for(int len=1;;len++){
if(n/len<=pw[len]-pw[len-1]){
return calc1(pw[len-1]+(n-1)/len,(n-1)%len+1);
}
n-=(pw[len]-pw[len-1])*len;
}
}
}
namespace solve2{
ll f[20][2][2],s[20];
int whi;
int dig[20],cnt;
ll dfs(int pos,int lim,int zero){
if(!pos)return 0;
if(f[pos][lim][zero])return f[pos][lim][zero];
ll ret=0;
for(int L=(lim)?dig[pos]:15,i=0;i<=L;i++){
ret+=dfs(pos-1,lim&(i==dig[pos]),zero&(i==0));
if(whi==0&&i==0&&!zero){
if(lim&&i==dig[pos])
ret+=s[pos-1]+1;
else ret+=pw[pos-1];
}
if(whi!=0&&i==whi){
if(lim&&i==dig[pos])
ret+=s[pos-1]+1;
else ret+=pw[pos-1];
}
//	cout<<pos<<" "<<i<<" "<<ret<<'\n';
}
return f[pos][lim][zero]=ret;
}
inline void calc2(ll n,int res){
cnt=0;ll pos=n;int anc=0;
while(pos)dig[++cnt]=pos%16,pos/=16;
reverse(dig+1,dig+cnt+1);
for(int i=1;i<=res;i++)if(dig[i]==whi)anc++;
//	cout<<anc<<" "<<n-1<<'\n';
cnt=0,pos=n-1;
while(pos)dig[++cnt]=pos%16,pos/=16;
for(int i=1;i<=cnt;i++)s[i]=s[i-1]+dig[i]*pw[i-1];
cout<<anc+dfs(cnt,1,1)<<'\n';
}
inline void solve(ll n){
char ch=gc();
while(isspace(ch))ch=gc();
whi=getin(ch);
memset(f,0,sizeof(f));
for(int len=1;;len++){
if(n/len<=pw[len]-pw[len-1]){
return calc2(pw[len-1]+(n-1)/len,(n-1)%len+1);
}
n-=(pw[len]-pw[len-1])*len;
}
}
}
inline void solve(){
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
//	freopen("my.out","w",stdout);
#endif
pw[0]=1;
for(int i=1;pw[i-1]<=1e18;i++)pw[i]=pw[i-1]*16;
while(T--)solve();
}
```

### T3:

I'm a vegetable
Not at all

Let f[i][j]f[i][j]f[i][j] indicate that there are three lines of jjj columns and each line has a number of black schemes
Consider a column by column enumeration. If there are no rows in the previous column, insert them into the original row iii
f[i][j]∗(1+i+(i2))−>f[i][j+1]f[i][j]*(1+i+{i\choose 2})->f[i][j+1]f[i][j]∗(1+i+(2i​))−>f[i][j+1]
f[i][j]∗(i+k+2k+2)−>f[i+k][j+1]f[i][j]*{i+k+2\choose k+2}->f[i+k][j+1]f[i][j]∗(k+2i+k+2​)−>f[i+k][j+1]
The second factor is due to the fact that the original row iii is in the jjj column
So we can consider adding k+2k+2k+2 lines, two of which represent the
Set another 222 positions to handle the situation that there is no original line iii as the beginning / end

And then it's all right

```#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
return (ib==ob)?EOF:*ib++;
}
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=998244353,G=3;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
cs int N=32005;
int rev[N];
inline void init_rev(int lim){
for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
inline void ntt(int *f,int lim,int kd){
for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
for(int a0,a1,mid=1;mid<lim;mid<<=1){
int wn=ksm(G,(mod-1)/(mid<<1));
for(int i=0;i<lim;i+=mid<<1)
for(int j=0,w=1;j<mid;j++,Mul(w,wn))
}
if(kd==-1){
reverse(f+1,f+lim);
for(int i=0,iv=Inv(lim);i<lim;i++)Mul(f[i],iv);
}
}
int fac[N],ifac[N];
inline void init_inv(cs int len=N-5){
fac[0]=ifac[0]=1;
for(int i=1;i<=len;i++)fac[i]=mul(fac[i-1],i);
ifac[len]=Inv(fac[len]);
for(int i=len-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
inline int C(int n,int m){return n<m?0:mul(fac[n],mul(ifac[m],ifac[n-m]));}
int lim,n,m,f[N],a[N],g[N];
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
lim=1;init_inv();
while(lim<=n*2)lim<<=1;
init_rev(lim);
for(int i=1;i<=n;i++)g[i]=ifac[i+2];
ntt(g,lim,1);
a[0]=1;
for(int i=1;i<=m;i++){
for(int j=0;j<=n;j++)f[j]=mul(a[j],ifac[j]);
ntt(f,lim,1);
for(int j=0;j<lim;j++)Mul(f[j],g[j]);
ntt(f,lim,-1);