Codeforces Round 875 (Div. 1) 题解

这篇具有很好参考价值的文章主要介绍了Codeforces Round 875 (Div. 1) 题解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

A Copil Copac Draws Trees

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 

#define MAXN (212345)
ll n,cal=0,b[MAXN];
vi v[MAXN];
map<pair<int,int> ,int> h;
int id[MAXN];
void dfs(int x,int fa) {
	for (auto u:v[x])
		if (u!=fa) {
			id[u]=h[mp(u,x)];
			if(id[u]>id[x]) b[u]=b[x];
			else b[u]=b[x]+1;
			
			dfs(u,x);
			
		}
}
int main()
{
//	freopen("a.in","r",stdin);
//	freopen(".out",w",stdout);
	int T=read();
	while(T--) {
		h.clear();
		n=read();
		For(i,n-1) {
			int x=read(),y=read();
			v[x].pb(y); v[y].pb(x);
			h[mp(x,y)]=h[mp(y,x)]=i;
		}
		For(i,n) b[i]=1e9;b[1]=0;id[1]=1e9;
		cal=0;
		dfs(1,-1);
		For(i,n) gmax(cal,b[i])
		For(i,n) v[i].resize(0);
		cout<<cal<<endl;
	}
	
	
	return 0;
}


B The BOSS Can Count Pairs

You are given two arrays a and b both of length n. Your task is to count the number of pairs of integers ( i , j ) (i,j) (i,j) such that 1 ≤ i < j ≤ n 1≤i<j≤n 1i<jn
and a i ⋅ a j = b i + b j a_i⋅a_j=b_i+b_j aiaj=bi+bj
1 ≤ a i , b i ≤ n 1≤a_i,b_i≤n 1ai,bin

考虑 m i n ( a i , a j ) ≤ 2 n , min(a_i,a_j)\le \sqrt{ 2n}, min(ai,aj)2n ,
.
暴力计算 a i = a j a_i=a_j ai=aj的情况
之后枚举 a i a_i ai较大的那个 i i i,暴力处理 a i a_i ai较少的。

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (200000+10)
int a[MAXN],b[MAXN];
int fr[633][MAXN]={};
ll work() {
	int n=read();
	For(i,n) a[i]=read();
	For(i,n) b[i]=read();
	int m=sqrt(2*n);
	For(i,n) if(a[i]<=m) fr[a[i]][b[i]]++;
	ll ans=0;
	For(i,n) {
		if(1<=a[i] && a[i]<=m && 1<=a[i]*a[i]-b[i] && a[i]*a[i]-b[i]<=n) {
			ans+=fr[a[i]][a[i]*a[i]-b[i]];
		}
	}
	for(int i=2;i<=m;i+=2){
		ans-=fr[i][i/2*i];
	}
	ans/=2;
	
	For(i,n) { 
		For(j,m) if(j<a[i] ){
			ll p=a[i]*j-b[i];
			if(1<=p&&p<=n) {
				ans+=fr[j][p];
			}
		}
	}
	
	For(i,n) if(a[i]<=m) fr[a[i]][b[i]]--;
	return ans;
	
}
int main()
{
//	freopen("B.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		cout<<work()<<endl;	
	}
	return 0;
}

C Hyperregular Bracket Strings

一个括号序列,满足题目给定的k个子区间为括号序列,求方案数。

有2个区间相交等于处理端点分割的3个区间。
有2个区间保护等于里面一个区间,外面删去里面一个区间。

对于最后分割好的区间,两个单位区间在一起,当且仅当它们被 k k k个子区间哪些包含的集合相同。文章来源地址https://www.toymoban.com/news/detail-472752.html

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (998244353)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (612345)
ll inj[MAXN],jie[MAXN],inv[MAXN];
inline int C(int a,int b) {
	return (ll)jie[a]*inj[b]%F*inj[a-b]%F;
}
ll p=F;
inline int pow2(int a,ll b)  //a^b mod p 
{  
    if (b==0) return 1%p;  
    int c=pow2(a,b/2);  
    c=(ll)c*c%p;  
    if (b&1) c=(ll)c*a%p;  
    return c;  
}  
void pre(int n) {
	jie[0]=1;For(i,n) jie[i]=mul(jie[i-1],i);
	inj[0]=inj[1]=1;Fork(i,2,n) inv[i]=inj[i]=(F-(F/i))*inj[F%i]%F;
	inv[1]=1;
	For(i,n) inj[i]=mul(inj[i],inj[i-1]);  
} 
pair<int,int> pa[MAXN*2];
ll Catalan(int n) {
	if(n%2==0) return (ll)C(n,n/2)*inv[n/2+1]%F;
	return 0;
}
ll l[MAXN],r[MAXN],col[MAXN]={};
mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<ll> rnd(0,LLONG_MAX);
ll get(){return rnd(gen);}
int work() {
	int n=read(),k=read();
	For(i,k) {
		l[i]=read(),r[i]=read();
		col[i]=get();
	}
	if(n&1) return 0;
	For(i,k) if((r[i]-l[i])%2!=1) return 0;

	map<ll,ll> dif,freq;
	dif[1]^=col[0],dif[n+1]^=col[0];
	For(i,k) {
		dif[l[i]]^=col[i],dif[r[i]+1]^=col[i];
	}
	ll h=dif.begin()->se;
	for(auto it=next(dif.begin());it!=dif.end();it++) {
		freq[h]+=it->fi - prev(it)->fi;
		h^=it->se;
	}
	
	ll ans=1;
	for(auto it:freq) {
		ans=mul(ans,Catalan(it.se));
	}
	return ans;
}
int main()
{
//	freopen("C.in","r",stdin);
//	freopen(".out","w",stdout);
	pre(600000);
	int T=read();
	cout<<Catalan(10);
	while(T--) {
		cout<<work()<<endl;
	}
	
	
	return 0;
}

到了这里,关于Codeforces Round 875 (Div. 1) 题解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Codeforces Round 877 (Div. 2) 题解

    写了两题了,先总结一下吧。。。看了三题,都是思维题构造题,搞心态真的搞心态。。。感觉自己屁都不会,完全没有动力。。。有的有思路但是写不出来,但是思路不够成熟,练的题太少,其实这场到B题就卡住了,C题也是,题意都能读懂,但是思考的方向不对,很焦虑

    2024年02月10日
    浏览(25)
  • Codeforces Round 858 (Div. 2)题解

    目录 A. Walking Master(贪心) 题面翻译 思路: 代码实现  B. Mex Master(贪心) 题面翻译: 思路: 代码实现 C. Sequence Master(数学) 题面翻译: 思路: 代码实现 YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the

    2023年04月08日
    浏览(68)
  • Codeforces Round 889 (Div. 2) 题解

    晚上睡不着就来总结一下叭~(OoO) 终榜!!! 先不放图了。。怕被dalaoHack...呜呜呜~         7.29半夜比赛,本来是不想打的,感觉最近做的题太多了,什么牛客杭电以及CF上的题等等等,挺杂的,也来不及整理,本想梳理一下,而且也感觉今天状态不太好,但见他们都要

    2024年02月15日
    浏览(31)
  • Educational Codeforces Round 145 Div. 2 题解

    目录 A. Garland(签到) 题面翻译 思路: 代码 B. Points on Plane(数学) 题面翻译 思路: 代码 C. Sum on Subarray(构造) 题面翻译: 思路: 代码 D. Binary String Sorting 题面翻译 思路: 代码 You have a garland consisting of 4 colored light bulbs, the color of the i-th light bulb is si. Initially, all the l

    2023年04月09日
    浏览(27)
  • Educational Codeforces Round 147 div2题解

    目录 A. Matching(签到) 思路: 代码:  B. Sort the Subarray(签到) 思路: 代码: C. Tear It Apart(枚举) 思路: 代码: D. Black Cells(模拟) 思路:  代码一: 代码二:(模仿自\\\"AG\\\"佬) An integer template is a string consisting of digits and/or question marks. A positive (strictly greater than 0) in

    2023年04月21日
    浏览(26)
  • Educational Codeforces Round 134 (Div.2) D 题解

    D. Maximum AND 给定两组序列 (a) (b) ,长度为 (n) ,现有一新序列 (c) ,长度也为 (n) 。 其中, (c_i = a_i oplus b_i) 。 定义 (f(a,b) = c_1c_2……c_n) 。 现在你可以随意编排 (b) 序列的顺序,求 (f(a,b)) 的最大值。 以下位运算均是二进制。 由于按位与的运算结果是越来越小的

    2024年02月06日
    浏览(31)
  • Educational Codeforces Round 148 (Rated for Div. 2) 题解

    总结:5.21下午VP一场,死在了A题,给我wa崩溃了,浪费了差不多一个小时,BC还挺顺畅,虽然C题是在结束后不久交上去的。。。。 思路:其实思路很简单, “ The string s a palindrome ”, 题目已经说了所给的为回文字符串,所以直接判断一半 有几种字符 即可(开始的时候计算整

    2024年02月06日
    浏览(29)
  • Codeforces Round 867 (Div. 3)

    从所有a[i]+i-1=t的选择种取个max即可 实际上就是取同符号乘积的最大值 找规律,发现结果与边长n的关系是:res = n * (n + 3) - (n - 2) ①当n为奇数时,除了1其他均无解 ②当n为偶数时,我们可以构造一个形如n,1,n - 2,3,...的数列 首先我们可以发现n必定出现在起始位置。如果n不在起

    2024年02月02日
    浏览(33)
  • Codeforces Round 868 Div 2

    要求构造一个仅包含 (1) 和 (-1) 的长度为 (n) 的数组 (a) ,使得存在 (k) 个下标对 ((i, j), i j) 满足 (a_i times a_j = 1) 。 当有 (x) 个 (1) , (y) 个 (-1) 时,其满足条件的下标对数量为 (frac{x (x - 1)}{2} + frac{y (y - 1)}{2}) 。 由于 (n) 只有 (100) ,直接枚举 (x) 即可。

    2024年02月01日
    浏览(30)
  • Codeforces Round 871 (Div. 4)

    给定n个长度为10的字符串,问其与codeforces字符串的对应下标字母不同的个数。 对于每个字符串从前往后依次和“codeforces”对应字符比较然后统计不同字母数即可 给定长度为n的数组,问连续相邻为0的最大长度 维护一个全局最大长度res,和当前连续序列的长度cnt。从前往后扫

    2024年02月03日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包