# Codeforces Round 871 (Div. 4) 题解

skip

## G

$$\ \ \ \ 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$$

$$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

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

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

posted @ 2023-05-07 00:57  Binary_Lee  阅读(192)  评论(0编辑  收藏  举报
Title