bzoj 3198: [Sdoi2013]spring hash+容斥原理
来源:互联网 发布:sap hana sql 编辑:程序博客网 时间:2024/06/01 10:10
题意
给定n个元素,每个元素是一个六元组,求有多少对元素满足相同的位置恰好有k个。
n<=100000
分析
很容易想到容斥原理。
不难发现,容斥系数要乘上一个组合数
代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int N=100005;const int mo1=1000007;const int mo2=998244353;const int ba1=233;const int ba2=1221;int n,m,a[N][7],cnt,q[N],last[mo1];LL ans;bool vis[7];struct edge{int next,w2,s;}e[N];int read(){ int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int C(int x,int y){ int ans=1; for (int i=y+1;i<=x;i++) ans*=i; for (int i=1;i<=x-y;i++) ans/=i; return ans;}int ins(int w1,int w2){ for (int i=last[w1];i;i=e[i].next) if (e[i].w2==w2) { e[i].s++; return e[i].s-1; } e[++cnt].w2=w2;e[cnt].s=1;e[cnt].next=last[w1];last[w1]=cnt; return 0;}void calc(int y){ LL tot=0;int top=0;cnt=0; for (int i=1;i<=n;i++) { int w1=0,w2=0; for (int j=1;j<=6;j++) if (vis[j]) { w1=((LL)w1*ba1+(LL)a[i][j])%mo1; w2=((LL)w2*ba2+(LL)a[i][j])%mo2; } tot+=ins(w1,w2);q[++top]=w1; } while (top) last[q[top]]=0,top--; if ((y-m)%2==0) ans+=(LL)tot*C(y,m); else ans-=(LL)tot*C(y,m);}void dfs(int x,int y){ if (x>6) { if (y>=m) calc(y); return; } dfs(x+1,y); vis[x]=1; dfs(x+1,y+1); vis[x]=0;}int main(){ n=read();m=read(); for (int i=1;i<=n;i++) for (int j=1;j<=6;j++) a[i][j]=read(); dfs(1,0); printf("%lld",ans); return 0;}
阅读全文
0 0
- BZOJ 3198([Sdoi2013]spring-Hash判重+容斥原理)
- BZOJ 3198 Sdoi2013 spring Hash+容斥原理
- bzoj 3198: [Sdoi2013]spring hash&容斥原理
- bzoj 3198: [Sdoi2013]spring (hash+容斥原理)
- bzoj 3198: [Sdoi2013]spring hash+容斥原理
- [BZOJ3198][Sdoi2013]spring(hash+容斥原理+组合数学)
- bzoj 3129 [Sdoi2013]方程 数论 容斥
- bzoj 3198: [Sdoi2013]spring 题解
- bzoj 3129: [Sdoi2013]方程(容斥原理+组合数学+数论)
- bzoj3198 [Sdoi2013]spring 哈希表+容斥
- bzoj 3622 容斥原理
- BZOJ 2005 容斥原理
- BZOJ 2045 容斥原理
- bzoj 1853 容斥原理
- BZOJ 2393 容斥原理
- bzoj 2393 & bzoj 1853 容斥原理
- BZOJ 2839 集合计数 容斥原理
- BZOJ 3771 Triple FFT+容斥原理
- java基础语法总结
- python 3 一些常用的内置数据结构介绍
- Excle"自Access"导入Access数据没反应
- 集合框架(一)
- SSH组合工程之-struts2&hibernate&spring(工程下载)
- bzoj 3198: [Sdoi2013]spring hash+容斥原理
- inno setup在代码区域随意删除文件
- 服务端redis 安装以及配置
- python随机访问二进制文件
- haha
- Android 性能微型优化建议
- 左旋转字符串java实现
- 栈和队列基础面试题
- spring mvc入门