P3298 [SDOI2013]泉
[SDOI2013]泉
题目描述
作为光荣的济南泉历史研究小组中的一员,铭铭收集了历史上
现在铭铭希望知道有多少对不同的年份:
对于
思路点拨
题目种的 恰好 这个条件并不是很好解决,我们考虑使用二项式反演将其转换成 至少 或者 不超过 的问题。我们设
根据定义,我们有:
我们简单反演得到:
现在我们只需要考虑
时空复杂度分析
我们枚举
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-f;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int MAXN=1e5+10;
int n,k;
struct node{
int s[7];
bool friend operator<(const node &A,const node &B){
if(A.s[1]==B.s[1]){
if(A.s[2]==B.s[2]){
if(A.s[3]==B.s[3]){
if(A.s[4]==B.s[4]){
if(A.s[5]==B.s[5])
return A.s[6]<B.s[6];
return A.s[5]<B.s[5];
}
return A.s[4]<B.s[4];
}
return A.s[3]<B.s[3];
}
return A.s[2]<B.s[2];
}
return A.s[1]<B.s[1];
}
bool friend operator==(const node &A,const node &B){
for(int i=1;i<=6;i++)
if(A.s[i]!=B.s[i]) return 0;
return 1;
}
}a[MAXN],b[MAXN];
const int N=7;
int f[N],g[N];
int run(int x){
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++){
int tot=0;
for(int j=0;j<6;j++)
if(x&(1<<j)){
++tot;
b[i].s[tot]=a[i].s[j+1];
}
}
sort(b+1,b+n+1);
int cnt=0,res=0;
for(int i=1;i<=n;i++){
if(i==1) res++;
else if(b[i]==b[i-1])
res++;
else{
cnt+=res*(res-1);
res=1;
}
}
cnt+=res*(res-1);
return cnt/2;
}
namespace Math{
int sum[N];
void prepare(){
sum[0]=1;
for(int i=1;i<=6;i++)
sum[i]=sum[i-1]*i;
}
int qpow(int a,int b){
int ans=1,base=a;
while(b){
if(b&1) ans=(ans*base);
base=base*base;
b>>=1;
}
return ans;
}
int C(int n,int m){
return sum[n]/(sum[m]*sum[n-m]);
}
}
using namespace Math;
signed main(){
n=read(),k=read();
for(int i=1;i<=n;i++){
for(int j=1;j<=6;j++)
a[i].s[j]=read();
}
for(int i=0;i<(1<<6);i++){
int popcount=0;
for(int j=0;j<6;j++)
if(i&(1<<j)) popcount++;
g[popcount]+=run(i);
}
prepare();
int ans=0;
for(int i=k;i<=6;i++)
ans=ans+qpow(-1,i-k)*C(i,k)*g[i];
cout<<ans;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现