BZOJ3198:[SDOI2013]SPRING

Description

Input

Output

Sample Input

3 3
1 2 3 4 5 6
1 2 3 0 0 0
0 0 0 4 5 6

Sample Output

2

HINT

 

题解:

一脸容斥的样子。

枚举判断是否相同的泉水集合S,若|S|>=K,则inc(ANS,(-1)^(|S|-K)*C(|S|,K)*相同对数)

哈希表记录、判断(我之前竟然写了类似字符串哈希的的做法,哈希值相同直接判断相同,WA惨了)。

 

代码:

 1 var
 2   i,j,k,l,n,m:longint;
 3   f:array[0..200001,1..6]of int64;
 4   b:array[1..6]of longint;
 5   c:array[0..6,0..6]of int64;
 6   cc:array[0..133330]of longint;
 7   d:array[0..200001,-1..6]of longint;
 8   a:array[0..200001]of int64;
 9   ans:int64;
10 procedure find(x,z:longint;y:int64);
11 var i,j,l:longint;
12 begin
13   i:=cc[a[z]];
14   while i>0 do
15   begin
16     l:=0;
17     for j:=1 to x do if d[i,j]<>f[z,b[j]] then begin l:=1; break; end;
18     if l=0 then begin ans:=ans+y*d[i,0]; inc(d[i,0]); exit; end;
19     i:=d[i,-1];
20   end;
21   inc(m); for i:=1 to x do d[m,i]:=f[z,b[i]];
22   d[m,0]:=1; d[m,-1]:=cc[a[z]]; cc[a[z]]:=m;
23 end;
24 function ss2(x:longint;y:int64):int64;
25 var i,j,k:longint;
26 begin
27   ss2:=0; m:=0;
28   for i:=0 to 133330 do cc[i]:=0;
29   for i:=1 to n do
30   begin
31     a[i]:=1;
32     for j:=1 to x do a[i]:=(a[i]*13131+f[i,b[j]])mod 133331;
33     find(x,i,y);
34   end;
35 end;
36 procedure ss(x,y:longint);
37 var i:longint;
38 begin
39   if x>6 then
40   begin
41     if y>=k then
42     begin
43       if(y-k)mod 2=0 then ss2(y,c[y,k])
44       else ss2(y,-c[y,k]);
45     end;
46     exit;
47   end;
48   ss(x+1,y);
49   inc(y); b[y]:=x; ss(x+1,y);
50 end;
51 begin
52   c[0,0]:=1;
53   for i:=1 to 6 do
54   begin
55     c[i,0]:=1; c[i,i]:=1;
56     for j:=1 to i-1 do c[i,j]:=c[i-1,j-1]+c[i-1,j];
57   end;
58   readln(n,k);
59   for i:=1 to n do
60   for j:=1 to 6 do read(f[i,j]);
61   ss(1,0);
62   writeln(ans);
63 end.
View Code
posted @ 2017-01-07 17:51  GhoStreach  阅读(162)  评论(0编辑  收藏  举报