bzoj 3198 [Sdoi2013]spring(容斥原理+Hash)
Description
.jpg)
Input
.jpg)
Output

Sample Input
1 2 3 4 5 6
1 2 3 0 0 0
0 0 0 4 5 6
Sample Output
HINT

【思路】
容斥原理+Hash
恰有k个元素相同的对数=至少k+1个相同*C(k+1,k) - 至少k+2个相同*C(k+2,k) + ……
枚举状态i,如果是101表示至少1和3两个相同,把n个年份关于i构造一个hash,然后放入hash中统计。这里只是关于位是1的构造hash,其他位都忽略了,所以得到的是至少有多少个相同的数目。
学了个hash表的写法,新姿势get :)
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef unsigned long long ull;
typedef long long ll;
const int N = ;
const int B = ; namespace Hash_set
{
struct node {
node* nxt;
ull H; int v;
node(){}
node(node* _,ull __) :
nxt(_),H(__),v() {}
}*head[N],mempool[N],*C=mempool;
int vis[N],kase;
void init() {
kase++; C=mempool;
}
int& insert(ull st) {
int pos=st%;
if(vis[pos]!=kase) {
vis[pos]=kase; head[pos]=0x0;
}
for(node* p=head[pos];p;p=p->nxt)
if(p->H==st) return p->v;
head[pos]=new (C++) node(head[pos],st);
return head[pos]->v;
}
} int n,K,a[N][],C[][]; ll calc(int st)
{
using namespace Hash_set;
ll ans=;
init();
for(int i=;i<n;i++) {
ull hash=;
for(int j=;j<;j++)
if( st&(<<j) )
hash=(hash*B+a[i][j]);
int& val=insert(hash);
ans+=(val++);
}
return ans;
} void get_C()
{
for(int i=;i<=;i++) {
C[i][]=C[i][i]=;
for(int j=;j<i;j++)
C[i][j]=C[i-][j-]+C[i-][j];
}
} void read(int& x) {
char c=getchar(); int f=; x=;
while(!isdigit(c)){if(c=='-')f=-; c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
} int main()
{
get_C();
read(n),read(K);
for(int i=;i<n;i++)
for(int j=;j<;j++) read(a[i][j]);
ll ans=;
for(int i=;i<;i++) {
int cnt=;
for(int j=;j<;j++)
if(i&(<<j)) cnt++;
if(cnt>=K) ans+=((cnt-K)&?-:)*calc(i)*C[cnt][K];
}
printf("%lld",ans);
return ;
}
bzoj 3198 [Sdoi2013]spring(容斥原理+Hash)的更多相关文章
- BZOJ 3198: [Sdoi2013]spring [容斥原理 哈希表]
3198: [Sdoi2013]spring 题意:n个物品6个属性,求有多少不同的年份i,j满足有k个属性对应相等 一开始读错题了,注意是对应相等 第i个属性只能和第i个属性对应 容斥一下 \[ 恰 ...
- [BZOJ 3198] [Sdoi2013] spring 【容斥 + Hash】
题目链接:BZOJ - 3198 题目分析 题目要求求出有多少对泉有恰好 k 个值相等. 我们用容斥来做. 枚举 2^6 种状态,某一位是 1 表示这一位相同,那么假设 1 的个数为 x . 答案就是 ...
- BZOJ 3198 SDOI2013 spring
为什么SDOI省选一年考两次容斥原理? 我们很容易发现>=k个相等时很好计算的 但是我们要求恰好k个,那么我们容斥即可 至于计算>=k个相等,首先我们枚举相等位置,对每个串对应位置做一遍h ...
- 3198: [Sdoi2013]spring【容斥原理+hash】
容斥是ans= 至少k位置相等对数C(k,k)-至少k+1位置相等对数C(k+1,k)+至少k+2位置相等对数*C(k+2,k) -- 然后对数的话2^6枚举状态然后用hash表统计即可 至于为什么要 ...
- bzoj 3197 [Sdoi2013]assassin(Hash+DP+KM)
Description Input Output Sample Input 4 1 2 2 3 3 4 0 0 1 1 1 0 0 0 Sample Output 1 HINT [思路] Hash,D ...
- sdoi2013 spring(hash+容斥)
大体思路是先求出来\(f[i]\)代表有至少\(i\)个位置相同的点对数. 然后就已经没什么好害怕的了(跟BZOJ3622一样) 然后这个\(f[i\)]怎么求呢? 最无脑的方法就是枚举位置,然后\( ...
- bzoj3198[Sdoi2013]spring 容斥+hash
3198: [Sdoi2013]spring Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1143 Solved: 366[Submit][Sta ...
- [Sdoi2013] [bzoj 3198] spring (hash+容斥原理)
题目描述 给出nnn个666维坐标,求有多少对点对满足恰好mmm个位置相等 1<=n<=1051<=n<=10^51<=n<=105 0<=k<=60& ...
- BZOJ3198 [Sdoi2013]spring 哈希 容斥原理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3198 题意概括 有n(1<=n<=100000)组数据,每组数据6个数. 现在问有几对 ...
随机推荐
- C#中字符串驻留技术
转自:http://www.cnblogs.com/Charles2008/archive/2009/04/12/1434115.html MSDN概念:公共语言运行库通过维护一个表来存放字符串,该表 ...
- [BEC][hujiang] Lesson03 Unit1:Working life ---Grammar & Listening & Vocabulary
3 Working life p8 Grammar Gerund and infinitive(动名词和不定式) 一般而言: 1 动词后面接动名词还是不定式没有特定规则,主要取决于语言习 ...
- 漫话C++0x(五)—- thread, mutex, condition_variable
熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不 ...
- c++: 获取delete[]中的数组大小
看一个小例子: 1 #include <iostream> 2 3 using namespace std; 4 5 class A { 6 public: 7 A() { ...
- Linux---More命令 初级实现
Linux: more已实现:more filename , quit不需要回车未实现:command | more 重定向 ,显示百分比 Waiting... /* Linux: more 已实现: ...
- java:I/O 往原文件追加内容
原来txt内容: "我要添加内容" import java.io.*; class Test { public static void main(String[] args) { ...
- 安装Hadoop系列 — eclipse plugin插件编译安装配置
[一].环境参数 eclipse-java-kepler-SR2-linux-gtk-x86_64.tar.gz //现在改为eclipse-jee-kepler-SR2-linux-gtk-x86_ ...
- 在vs2010中mfc,C++的一些小经验
1 如果你最近才从vc6.0到vs2010,在vs2010中mfc可能遇见一个小问题,如果你添加或改天了窗口中的控件,运行程序缺没有发现其中的变化,这时候需要在debug选项中rebuild all一 ...
- Spring个人总结
编写Spring第一个程序 Spring是一种开源框架,通过使用它可以大大降低企业应用程序的复杂性.Spring是一种非常完善的框架,几乎涉及WEB开发中的每一层,但是在开发中通常使用Spring开发 ...
- 1022. Genealogical Tree(topo)
1022 简单拓扑 不能直接dfs 可能有不联通的 #include <iostream> #include<cstdio> #include<cstring> # ...