2022.11.10
额,就是一个随笔而已,仿照 \(\color{Red}{Cotsheep}\) 那样,随便写点,权当水吧。
神妙的构造,报废的大脑。
因为只用最后一位决定字典序,其他完全可以全用 a
填充。
显然答案具有单调性,那就直接上个二分。
可以把 check 的过程看成是 \(k\) 进制的运算,开个栈模拟算数进位即可。
code
// I forgot all the tragedies and all I saw were miracles.
/************************************
|* Author: A.I.skeleton
|* Problem: C - Lexicographic constraints
|* Contest: AtCoder - AtCoder Grand Contest 029
|* URL: https://atcoder.jp/contests/agc029/tasks/agc029_c
|* When: 2022-11-10 07:21:47
|*
|* Memory: 1024 MB
|* Time: 2000 ms
************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define lc p<<1
#define rc p<<1|1
#define lb(x) (x&-x)
#define ch(i) (i-'a')
#define F(i) (i).first
#define S(i) (i).second
#define X(i) (i>n?i-n:i)
#define qk fflush(stdout);
#define kw(x,k) (((x)>>(k))&1)
#define cl(x) ((x).clear());
#define si(i) ((int)i.size())
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define id(x,y) ((x-1)*m+y)
#define all(x) (x).begin(),(x).end()
#define me(t,x) memset(t,x,sizeof(t))
#define L(i,j,k) for(int (i)=(j);i<=(k);(i)++)
#define R(i,j,k) for(int (i)=(j);i>=(k);(i)--)
#define ll(i,j,k,l) for(int (i)=(j);i<=(k);(i)+=(l))
#define rr(i,j,k,l) for(int (i)=(j);i>=(k);(i)-=(l))
#define FST ios::sync_with_stdio(0);cin.tie(nullptr);
#define ddcout(a) cout<<fixed<<setprecision(10)<<a<<'\n';
// #define DE
namespace DG{
#ifdef DE
#define got cout<<"get here"<<'\n';
#define cut cout<<"-------------------------\n";
#define time cerr<<1e3*clock()/CLOCKS_PER_SEC<<" ms\n",0;
#define dgvi(v) cout<<#v": size= "<<si(v)<<"\nelement: ";for(auto p:v) cout<<p<<" ";cout<<'\n';
#define dgar(x,s) cout<<#x": len= "<<s+1<<"\nelement: ";L(i,0,s) cout<<x[i]<<" \n"[i==s];
#define dge(x,n) int _=0;L(i,0,n) _+=si(x[i]);cout<<"There are "<<_<<" edges in "<<#x<<'\n';\
L(u,0,n) for(int v:x[u]) cout<<u<<' '<<v<<'\n';
#define dg(...) W(#__VA_ARGS__,__VA_ARGS__)
template<typename ...Args>
void W(string X,Args&&...V){cout<<X<<" = ";string D=""; (...,(cout<<D<<V,D=", "));cout<<'\n';}
#else
#define got
#define time 0
#define cut
#define dgvi(v)
#define dgar(x,s)
#define dge(x,n)
#define dg(...)
#endif
}using namespace DG;namespace IO{
template<typename T>void rd(T &x){cin>>x;}template<typename T>void wr(T x){cout<<x;}
template<typename T,typename ...Args>void rd(T &x,Args &...args){rd(x),rd(args...);}
template<typename T,typename ...Args>void wr(T x,Args ...args){wr(x),wr(args...);}
}using namespace IO;
#define int long long
const int N=2e6+100,INF=1e18;
int n,a[N],l=2,r=N,m,ans=N;
stack<pi>st;
void ins(int v,int x){
while(F(st.top())>v) st.pop();
if(F(st.top())==v) S(st.top())++;
else st.push({v,1});
if(st.size()>1&&S(st.top())==x)
st.pop(),ins(v-1,x);
}bool check(int x){
while(!st.empty()) st.pop();
st.push({0,0});
L(i,2,n) if(a[i]<=a[i-1]) ins(a[i],x);
while(st.size()>1) st.pop();
return S(st.top())==0;
}signed main(){
FST;rd(n);L(i,1,n) rd(a[i]);ans=n;
bool f=1;L(i,2,n) f&=(a[i]>a[i-1]);
if(f) return wr(1,'\n'),time;
while(l<=r){
m=(l+r)>>1;
if(check(m)) ans=m,r=m-1;
else l=m+1;
}wr(ans,'\n');
return time;
}
\(n \le 50\),那什么爆枚都能干上去。
首先如何检查两棵树是否一致。
考虑枚举根的情况,在两棵树的根都已经确定的情况下,只要所有点的父节点都一致,说明两棵树一致,时间复杂度 \(O(n)\)。
那就直接枚举所有根的情况,然后分情况讨论:
- 如果根不被操作,就直接找出所有需要改变的节点,连边跑一个拓扑,如果有环说明不可行,否则更新答案。
- 如果根被操作,就再次枚举所有可能作为根的新父节点的节点,然后按上述一样操作更新答案即可。
单组时间复杂度:\(O(n^3)\)。
要命的是把 \(j\) 写成 \(i\),但是样例过了,调了半天。
code
// I forgot all the tragedies and all I saw were miracles.
/************************************
|* Author: A.I.skeleton
|* Problem: F - Grafting
|* Contest: AtCoder - AtCoder Grand Contest 027
|* URL: https://atcoder.jp/contests/agc027/tasks/agc027_f?lang=en
|* When: 2022-11-10 08:48:43
|*
|* Memory: 1024 MB
|* Time: 5000 ms
************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define lc p<<1
#define rc p<<1|1
#define lb(x) (x&-x)
#define ch(i) (i-'a')
#define F(i) (i).first
#define S(i) (i).second
#define X(i) (i>n?i-n:i)
#define kw(x,k) (((x)>>(k))&1)
#define cl(x) ((x).clear());
#define si(i) ((int)i.size())
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define id(x,y) ((x-1)*m+y)
#define all(x) (x).begin(),(x).end()
#define me(t,x) memset(t,x,sizeof(t))
#define L(i,j,k) for(int (i)=(j);i<=(k);(i)++)
#define R(i,j,k) for(int (i)=(j);i>=(k);(i)--)
#define ll(i,j,k,l) for(int (i)=(j);i<=(k);(i)+=(l))
#define rr(i,j,k,l) for(int (i)=(j);i>=(k);(i)-=(l))
#define FST ios::sync_with_stdio(0);cin.tie(nullptr);
#define ddcout(a) cout<<fixed<<setprecision(10)<<a<<'\n';
// #define DE
#pragma GCC optimize(2)
namespace DG{
#ifdef DE
#define got cout<<"get here"<<'\n';
#define cut cout<<"-------------------------\n";
#define time cerr<<1e3*clock()/CLOCKS_PER_SEC<<" ms\n",0;
#define dgvi(v) cout<<#v": size= "<<si(v)<<"\nelement: ";for(auto p:v) cout<<p<<" ";cout<<'\n';
#define dgar(x,s) cout<<#x": len= "<<s+1<<"\nelement: ";L(i,0,s) cout<<x[i]<<" \n"[i==s];
#define dge(x,n) int _=0;L(i,0,n) _+=si(x[i]);cout<<"There are "<<_<<" edges in "<<#x<<'\n';\
L(u,0,n) for(int v:x[u]) cout<<u<<' '<<v<<'\n';
#define dg(...) W(#__VA_ARGS__,__VA_ARGS__)
template<typename ...Args>
void W(string X,Args&&...V){cout<<X<<" = ";string D=""; (...,(cout<<D<<V,D=", "));cout<<'\n';}
#else
#define got
#define time 0
#define cut
#define dgvi(v)
#define dgar(x,s)
#define dge(x,n)
#define dg(...)
#endif
}using namespace DG;namespace IO{
template<typename T>void rd(T &x){cin>>x;}template<typename T>void wr(T x){cout<<x;}
template<typename T,typename ...Args>void rd(T &x,Args &...args){rd(x),rd(args...);}
template<typename T,typename ...Args>void wr(T x,Args ...args){wr(x),wr(args...);}
}using namespace IO;
#define int long long
const int N=55,INF=1e18;
bool vis[N];int n,u,v,s;
struct graph{
vi g[N];int fa[N],r[N];
void qk(){L(i,0,50) cl(g[i]);me(r,0);}
void I(int u,int v){g[u].pb(v);r[v]++;}
void dfs(int u){for(int v:g[u]) if(v^fa[u]) fa[v]=u,dfs(v);}
int tp(){
queue<int>q;int rt=0;
L(i,1,n) if(vis[i]&&!r[i]) ++rt,q.push(i);
while(!q.empty()){
int u=q.front();q.pop();
for(int v:g[u]) if(!(--r[v]))
rt++,q.push(v);
}return rt;
}
}A,B,G;
int sol(){
int rt=0;L(i,1,n) rt+=(vis[i]=(A.fa[i]!=B.fa[i]));
if(rt>=s) return n+1;G.qk();
L(i,1,n){
if(!vis[i]){
if(vis[A.fa[i]]) return n+1;
else continue;
}//u should change first before fa_i.
if(vis[A.fa[i]]) G.I(i,A.fa[i]);
if(vis[B.fa[i]]) G.I(B.fa[i],i);
}return G.tp()==rt?rt:n+1;
//Check if there is a circle
}void work(){
rd(n);A.qk();B.qk();s=n+1;
L(i,2,n) rd(u,v),A.I(u,v),A.I(v,u);
L(i,2,n) rd(u,v),B.I(u,v),B.I(v,u);
L(i,1,n){
A.fa[i]=B.fa[i]=0;A.dfs(i);B.dfs(i);s=min(s,sol());
//do not change the root,then just check all point's father.
if(A.r[i]==1) L(j,1,n) if(i^j)//if root can be changed first as a leaf
A.fa[j]=i,A.dfs(j),B.dfs(i),A.fa[i]=B.fa[i]=0,s=min(s,sol()+1);
}wr((s>n?-1:s),'\n');
}signed main(){
FST;int t;rd(t);
L(i,1,t) work();
return time;
}
上午之后不想写题,就疯狂改装我缺省源里的 modint
模块。
加上了任意模,各种运算符重载。
反正就是改到了看不出是从 \(\color{Black}{j}\color{Red}{iangly}\) 那贺来的程度了。
下午和晚上看 ICPC WF 的直播。
晚上瞎走了一题。
反正就先考虑所有边之间独立,那就只考虑单个行,求出后快速幂。
设 \(f_i\) 表示以 \(i\) 为长度的行的摆放方式并且忽略最少放置两个卒的限制
那么显然有 \(f_i = \sum_{j=1}^{i-2} f_j = f_{i-1}+f_{i-2}\),不难发现这就是斐波那契数列。
减去第二条性质中的情况,那么每一行的方案数就是 \(f_{n+3}-n-2\)。
\(n,p\) 都非常大,矩阵快速幂外加快速乘即可。
code
// I forgot all the tragedies and all I saw were miracles.
/************************************
|* Author: A.I.skeleton
|* Problem: P5059 中国象棋
|* Contest: Luogu
|* URL: https://www.luogu.com.cn/problem/P5059
|* When: 2022-11-10 19:53:58
|*
|* Memory: 125 MB
|* Time: 1000 ms
************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define lc p<<1
#define rc p<<1|1
#define lb(x) (x&-x)
#define pb push_back
#define ch(i) (i-'a')
#define vi vector<int>
#define F(i) (i).first
#define S(i) (i).second
#define X(i) (i>n?i-n:i)
#define pi pair<int,int>
#define id(x,y) ((x-1)*m+y)
#define cl(x) ((x).clear());
#define si(i) ((int)i.size())
#define kw(x,k) (((x)>>(k))&1)
#define all(x) (x).begin(),(x).end()
#define me(t,x) memset(t,x,sizeof(t))
#define L(i,j,k) for(int (i)=(j);i<=(k);(i)++)
#define R(i,j,k) for(int (i)=(j);i>=(k);(i)--)
#define ll(i,j,k,l) for(int (i)=(j);i<=(k);(i)+=(l))
#define rr(i,j,k,l) for(int (i)=(j);i>=(k);(i)-=(l))
#define dout(a,x) cout<<fixed<<setprecision(x)<<a<<'\n';
#define FST ios::sync_with_stdio(false);cin.tie(nullptr);
#define DE
#pragma GCC optimize(2)
namespace DG{
#ifdef DE
#define got cout<<"get here"<<'\n';
#define cut cout<<"-------------------------\n";
#define time cerr<<1e3*clock()/CLOCKS_PER_SEC<<" ms\n",0;
#define dgvi(v) cout<<#v": size= "<<si(v)<<"\nelement: ";for(auto p:v) cout<<p<<" ";cout<<'\n';
#define dgar(x,s) cout<<#x": len= "<<s+1<<"\nelement: ";L(i,0,s) cout<<x[i]<<" \n"[i==s];
#define dge(x,n) int _=0;L(i,0,n) _+=si(x[i]);cout<<"There are "<<_<<" edges in "<<#x<<'\n';\
L(u,0,n) for(int v:x[u]) cout<<u<<' '<<v<<'\n';
#define dg(...) W(#__VA_ARGS__,__VA_ARGS__)
template<typename ...Args>
void W(string X,Args&&...V){cout<<X<<" = ";string D=""; (...,(cout<<D<<V,D=", "));cout<<'\n';}
#else
#define got
#define time 0
#define cut
#define dgvi(v)
#define dgar(x,s)
#define dge(x,n)
#define dg(...)
#endif
}using namespace DG;namespace IO{
template<typename T>void rd(T &x){cin>>x;}template<typename T>void wr(T x){cout<<x;}
template<typename T,typename ...Args>void rd(T &x,Args &...args){rd(x),rd(args...);}
template<typename T,typename ...Args>void wr(T x,Args ...args){wr(x),wr(args...);}
}using namespace IO;
#define int long long
const int N=2e6+100,INF=1e18;
int n,mod;
ll qmul(ll a,ll b,ll p) {
ll rt=a*b-((ll)((ld)a/p*b))*p;
return rt>=p?rt-p:rt<0?rt+p:rt;
}ll power(ll a,ll b,ll p){
ll s=1;while(b){
if(b&1) s=qmul(s,a,p);
a=qmul(a,a,p);b>>=1;
}return s;
}struct mt{
int a[2][2];mt(){me(a,0);}
friend mt operator*(mt a,mt b){
mt c;L(i,0,1) L(j,0,1) L(k,0,1)
c.a[i][j]+=qmul(a.a[i][k],b.a[k][j],mod);return c;
}
}b,s;
void init(){
b.a[0][0]=b.a[0][1]=b.a[1][0]=1;
s.a[0][0]=s.a[0][1]=1;
}void power(int k){for(;k;k>>=1,b=b*b) if(k&1) s=s*b;}
signed main(){
FST;rd(n,mod);
if(n==1) return wr("0\n"),time;
init();power(n+1);ll ans=(s.a[0][0]-n-2+mod)%mod;
dg(ans);ans=power(ans,n+1,mod);wr(ans,'\n');
return time;
}
晚上回家之后啥也没干,光顾着摆烂了。
\(\color{White}{\text{bilibili and genshin}}\)