当前位置:   article > 正文

Codeforces Round 875 (Div. 1) 题解_a. copil copac draws trees

a. copil copac draws trees

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;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

C Hyperregular Bracket Strings

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

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

对于最后分割好的区间,两个单位区间在一起,当且仅当它们被 k k k个子区间哪些包含的集合相同。

#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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号