Codeforces Round 871 (Div. 4) 题解

发布时间 2023-05-07 01:02:50作者: Binary_1110011

好久没打比赛了啊 qwq

A~C

skip

D

把能被 \(3\) 整除的数丢到一个队列里去 \(\text{BFS}\),每次取队头把它分成两个,如果能被 \(3\) 整除继续入队。最后看给定的数有没有入过队。

E

直接 \(\text{BFS}\),把每个块总和算出来求最大。

F

可以发现,图的点数 \(n=1+x+x\times y\)。又易知,第二次多出来的点的度均 \(=1\)。设度为 \(1\) 的点数为 \(cnt\),即 \(x\times y=cnt\)。所以 \(x=n-cnt-1\)\(y=cnt/x\)

G

因为每塔的总层数不多(\(<2000\)),所以考虑枚举层,然后 \(O(1)\) 求单层内总和。

因为一层内倒的是连续的,所以尝试推一个柿子:

\(\ \ \ \ a^2+(a+1)^2+(a+2)^2+...+(a+m-1)^2\)

\(=m\times a^2+\frac{(2m-2)\times m}2\times a+\frac{(m-1)\times m\times (2m-1)}6\)

然后考虑如何求每层的 \(a\)\(m\)

记当前层倒下的右端点为 \(R\),对应 \(m=R-a+1\)。从下往上做,有初始 \(a=n,R=n\)

\(n\) 所在层数可以预处理每层点数然后暴力找。对于第 \(i\) 层算完后,\(a,R\) 的变化有如下规律:

if(a==i*(i-1)/2+1) a-=i-1;
else a-=i;
if(R==i*(i+1)/2) R-=i;
else R-=i-1;

至于为什么,自己找几个数算算就好了。

血的教训:柿子不要抄错,不然可能对着正确的地方差半天错(

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int T,n,f[10005];
void solve(){
	cin>>n;
	int id;
	for(int i=1;i<=2000;i++){
		if(f[i]>=n){
			id=i;
			break;
		}
	}
	int a=n,R=n,ans=0,cnt=0,sum=0;
	for(int i=id;i>=1;i--){
		int m=R-a+1;
		ans+=m*a*a+((2ll*m-2)*m/2)*a+(m-1)*m*(2ll*m-1)/6;
		cnt++;
		if(a==i*(i-1)/2+1) a-=i-1;
		else a-=i;
		if(R==i*(i+1)/2) R-=i;
		else R-=i-1;
	}
	cout<<ans<<endl; 
}
signed main(){
	IOS;TIE;
	for(int i=1;f[i-1]<=1000000;i++){
		f[i]=f[i-1]+i;
	}
	cin>>T;
	while(T--) solve();
	return 0;
}
//m*a^2+((m*2-2)*m/2)a+(m-1)(m)(2m-1)/6 

H

考虑 \(\text{DP}\)

\(f_{i,j}\) 表示前 \(i\) 个数,选取若干个与和 \(=j\) 的方案数。

有转移:

\[f_{i,j}=f_{i,j}+f_{i-1,j} \]

\[f_{i,j\&a_i}=f_{i,j\&a_i}+f_{i-1,j} \]

最后答案为 对于所有二进制下 \(1\) 的个数 \(=k\)\(i\)\(\sum f_{n,i}\)

血的教训:div4不要去想什么乱七八糟的容斥

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
#define mod 1000000007
using namespace std;
int T,n,k,a[200005],f[200005][65],ans;
void solve(){
	cin>>n>>k;
	ans=0;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++){
		for(int j=0;j<64;j++) f[i][j]=0;
		f[i][a[i]]++;
		for(int j=0;j<64;j++){
			f[i][j&a[i]]+=f[i-1][j],f[i][j&a[i]]%=mod;
			f[i][j]+=f[i-1][j],f[i][j]%=mod;
		}
	}
	for(int s=0;s<(1<<6);s++){
		if(cnt1(s)==k) ans+=f[n][s],ans%=mod;
	}
	cout<<ans<<endl;
}
signed main(){
	IOS;TIE;
	cin>>T;
	while(T--) solve();
	return 0;
}