跳到正文

个人记录

来做梦 250817 总结

_recollector_约 15 分钟
原文
文章信息与版本

文章信息

发布
2025年8月17日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 15 分钟

版本

  1. v1当前

T1

我在干什么,这种 rz 题都没做出来。/ll

赛时直接考虑枚举 n\sqrt n 次找,然后绝对被卡,所以加一个 O(n2)O(n^2) 暴力,50 pts。

所以考虑优化,我们发现对于一段连续的个 A 他对后面连续的 BC 段都能生效,例如AAABCBCBC。故可以统计 A 的数量,O(n)O(n) 即可。

C++
#include<bits/stdc++.h>
using namespace std;
namespace zorr{
	#ifdef getchar_unlocked
	#define gc getchar_unlocked
	#define pc putchar_unlocked
	#else
	#define gc getchar
	#define pc putchar
	#endif
    #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
    #define meow ios_base::sync_with_stdio(false);cin.tie(0)
	#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
	#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
	const int inf=0x3f3f3f3f;
	const long long infll=0x3f3f3f3f3f3f3f3fll;
	const __int128 infi128=(__int128(1)<<126);
	mt19937 __rnd(time(0));
	template<typename T>T rd()
	{
		T x=0;bool w=0;
		char ch=gc();
		for(;!isdigit(ch);ch=gc())w|=(ch=='-');
		for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
		return x;
	}
	template<typename T>void wt(T x)
	{
		if(x<0)pc('-'),x=-x;
		if(x>9)wt(x/10);
		pc(x%10+48);
	}
	int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
}
#define int long long
using namespace zorr;
const int N=2e5+5;
void solve()
{
    string s;
    cin>>s;
    int n=s.size(),cnt=0,ans=0;s="#"+s;
    rep(i,1,n,1)
    {
        if(s[i]=='A')++cnt;
        else if(s[i]=='B')
        {
            if(i<n&&s[i+1]=='C')ans+=cnt,++i;
            else cnt=0;
        }
        else cnt=0;
    }
    cout<<ans;
}
signed main()
{
    // meow;
	int T=1;
	//cin>>T;
	//T=read();
	rep(i,1,T,1)solve();
}

T2

有点小难 /ll

所以我们从边的角度不好考虑,从每个核心点出发考虑,最终答案最多可以使用 nn 个核心。

其实不用考虑匹配,对于一个连通块,一定是可以满足 xx 个点是可以给 xx 条边使用的。

所以只需要判断每个连通块的点的数量和边的数量即可。

接下来直接按照最小生成树的贪心选取即可。

O(nα(n)+mlog⁡n)O(n\alpha(n)+m\log n)

C++
#include<bits/stdc++.h>
using namespace std;
namespace zorr{
	#ifdef getchar_unlocked
	#define gc getchar_unlocked
	#define pc putchar_unlocked
	#else
	#define gc getchar
	#define pc putchar
	#endif
    #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
    #define meow ios_base::sync_with_stdio(false);cin.tie(0)
	#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
	#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
	const int inf=0x3f3f3f3f;
	const long long infll=0x3f3f3f3f3f3f3f3fll;
	const __int128 infi128=(__int128(1)<<126);
	mt19937 __rnd(time(0));
	template<typename T>T rd()
	{
		T x=0;bool w=0;
		char ch=gc();
		for(;!isdigit(ch);ch=gc())w|=(ch=='-');
		for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
		return x;
	}
	template<typename T>void wt(T x)
	{
		if(x<0)pc('-'),x=-x;
		if(x>9)wt(x/10);
		pc(x%10+48);
	}
	int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
}
#define int long long
using namespace zorr;
const int N=2e5+5;
int fa[N],sz[N],sze[N];
struct node{int u,v,w;}e[N];
int fnd(int x){return x==fa[x]?x:fa[x]=fnd(fa[x]);}
void solve()
{
    int n=rd<int>(),m=rd<int>();
    rep(i,1,n,1)fa[i]=i,sz[i]=1,sze[i]=0;
    rep(i,1,m,1)e[i]={rd<int>(),rd<int>(),rd<int>()};
    sort(e+1,e+m+1,[](node n1,node n2){return n1.w>n2.w;});
    int ans=0;
    rep(i,1,m,1)
    {
        int u=fnd(e[i].u),v=fnd(e[i].v);
        if(u==v)
        {
            if(sze[u]<sz[u])
            {
                ++sze[u];
                ans+=e[i].w;
            }
        }
        else
        {
            if(sze[u]<sz[u]||sze[v]<sz[v])
            {
                fa[u]=v;
                sz[v]+=sz[u];
                sze[v]+=sze[u];
                ans+=e[i].w;
                ++sze[v];
            }
        }
    }
    wt<int>(ans);pc(10);
}
signed main()
{
    // meow;
	int T=1;
	//cin>>T;
    T=rd<int>();
	rep(i,1,T,1)solve();
}

T3

赛时感觉会统计答案,换根 dp,没时间了打了个暴力,没想到猜对了,这种 rz 题都没有做出来。/ll

我们考虑设 fuf_u 为 uu 子树内的答案,cuc_u 为 uu 中因子 22 和因子 55 的较小值,很显然的有 fu=szu×cuf_u=sz_u \times c_u。

我们考虑 gug_u 为 uu 子树外的答案,有 gv=(szu−szv)×cu+gug_v=(sz_u-sz_v)\times c_u+g_u,g1=f1g_1=f_1。

显然的 O(n)O(n)。

C++
#include<bits/stdc++.h>
using namespace std;
namespace zorr{
	#ifdef getchar_unlocked
	#define gc getchar_unlocked
	#define pc putchar_unlocked
	#else
	#define gc getchar
	#define pc putchar
	#endif
    #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
    #define meow ios_base::sync_with_stdio(false);cin.tie(0)
	#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
	#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
	const int inf=0x3f3f3f3f;
	const long long infll=0x3f3f3f3f3f3f3f3fll;
	const __int128 infi128=(__int128(1)<<126);
	mt19937 __rnd(time(0));
	template<typename T>T rd()
	{
		T x=0;bool w=0;
		char ch=gc();
		for(;!isdigit(ch);ch=gc())w|=(ch=='-');
		for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
		return x;
	}
	template<typename T>void wt(T x)
	{
		if(x<0)pc('-'),x=-x;
		if(x>9)wt(x/10);
		pc(x%10+48);
	}
	int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
}
#define int long long
using namespace zorr;
const int N=2e5+5;
int sz[N],dp[2][N],cnt[2][N];
vector<int>e[N];
void dfs1(int u,int fa)
{
    sz[u]=1;
    for(auto &v:e[u])
    {
        if(v==fa)continue;
        dfs1(v,u);
        sz[u]+=sz[v];
    }
    // cout<<u<<' '<<sz[u]<<' '<<cnt[u]<<'\n';
}
void dfs2(int u,int fa)
{
    for(auto &v:e[u])
    {
        if(v==fa)continue;
        dp[0][v]=(sz[u]-sz[v])*cnt[0][u]+dp[0][u];
        dp[1][v]=(sz[u]-sz[v])*cnt[1][u]+dp[1][u];
        dfs2(v,u);
    }
}
void solve()
{
    int n,m;
    cin>>n>>m;
    rep(i,1,n,1)
    {
        int x=i,cnt1=0,cnt2=0;
        while(x%2==0)++cnt1,x/=2;
        while(x%5==0)++cnt2,x/=5;
        cnt[0][i]=cnt1;
        cnt[1][i]=cnt2;
    }
    rep(i,1,n-1,1)
    {
        int u,v;
        cin>>u>>v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs1(1,0);
    dp[0][1]=cnt[0][1]*sz[1];
    dp[1][1]=cnt[1][1]*sz[1];
    dfs2(1,0);
    rep(i,1,m,1)
    {
        int x;
        cin>>x;
        cout<<min(dp[0][x]+sz[x]*cnt[0][x],dp[1][x]+sz[x]*cnt[1][x])<<'\n';
    }
}
signed main()
{
    meow;
	int T=1;
	//cin>>T;
	//T=read();
	rep(i,1,T,1)solve();
}

T4

太难了/dk

我们考虑先放权值 0 的点,再放 1,2,⋯\cdots。

同时点权不相邻

考虑状压,fsf_s 表示数字放置状态 ss 时的方案数,不难得到 集合能到的的终点合集需要满足以下条件才可以转移:wjand⁡j=0w_j \operatorname{and} j=0 且 wjand⁡(i⊕j)=(i⊕j)w_j \operatorname{and} (i\oplus j)=(i\oplus j)

O(3n)O(3^n)。

C++

#include<bits/stdc++.h>
using namespace std;
// #define int long long
namespace florr{
	#ifdef getchar_unlocked
	#define gc getchar_unlocked
	#define pc putchar_unlocked
	#else
	#define gc getchar
	#define pc putchar
	#endif
    #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
    #define meow ios_base::sync_with_stdio(false);cin.tie(0)
	#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
	#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
	#define loop(i,st,ed,nxt) for(int i=st;i!=ed;i=nxt)
	const int inf=0x3f3f3f3f;
	const long long infll=0x3f3f3f3f3f3f3f3fll;
	const __int128 infi128=(__int128(1)<<126);
	mt19937 __rnd(time(0));
	template<typename T=int>T rd()
	{
		T x=0;bool w=0;
		char ch=gc();
		for(;!isdigit(ch);ch=gc())w|=(ch=='-');
		for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
		return x;
	}
	template<typename T=int>void wt(T x)
	{
		if(x<0)pc('-'),x=-x;
		if(x>9)wt(x/10);
		pc(x%10+48);
	}
	int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
}
using namespace florr;
const int N=20;
int f[N],dp[1<<N],w[1<<N];
void solve()
{
	int n=rd(),m=rd();
    rep(i,1,m,1)
    {
        int u=rd()-1,v=rd()-1;
        f[u]|=(1<<v);
        f[v]|=(1<<u);
    }
    rep(s,0,(1<<n)-1,1)rep(i,0,n-1,1)
        if(s&(1<<i))
            w[s]|=f[i];
    dp[0]=1;
    rep(s,1,(1<<n)-1,1)loop(t,s,0,s&(t-1))
    {
        if(!(w[t]&t)&&(w[t]&(s^t))==(s^t))
            dp[s]+=dp[s^t];
    }
    cout<<dp[(1<<n)-1];
}
signed main()
{
    // meow;
	signed T=1;
	//cin>>T;
	//T=rd<signed>();
	rep(i,1,T,1)solve();
}
原文