码迷,mamicode.com
首页 > 其他好文 > 详细

【2016多校】T3 subset(分块,DP)

时间:2016-11-13 22:38:43      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:pre   reset   out   code   class   set   var   str   string   

 1 var dp:array[0..300,0..300]of longint;
 2     s1,s2,n,i,x,j:longint;
 3     ch:string;
 4 
 5 procedure dfs1(var s1,s2:longint;k,s:longint);
 6 begin
 7  if k>7 then
 8  begin
 9   inc(dp[s1,s]);
10   exit;
11  end;
12  if s2 and (1<<k)>0 then dfs1(s1,s2,k+1,s+(1<<k))
13   else
14   begin
15    dfs1(s1,s2,k+1,s);
16    dfs1(s1,s2,k+1,s+(1<<k));
17   end;
18 end;
19 
20 procedure dfs2(var s1,s2:longint;k,s:longint);
21 begin
22  if k>7 then
23  begin
24   dec(dp[s1,s]);
25   exit;
26  end;
27  if s2 and (1<<k)>0 then dfs2(s1,s2,k+1,s+(1<<k))
28   else
29   begin
30    dfs2(s1,s2,k+1,s);
31    dfs2(s1,s2,k+1,s+(1<<k));
32   end;
33 end;
34 
35 procedure add(x:longint);
36 var i:longint;
37 begin
38  s1:=0; s2:=0;
39  for i:=15 downto 8 do
40   if x and (1<<i)>0 then s1:=s1+1<<(i-8);
41   for i:=7 downto 0 do
42    if x and (1<<i)>0 then s2:=s2+1<<i;
43  dfs1(s1,s2,0,0);
44 end;
45 
46 procedure del(x:longint);
47 var i:longint;
48 begin
49  s1:=0; s2:=0;
50  for i:=15 downto 8 do
51   if x and (1<<i)>0 then s1:=s1+1<<(i-8);
52   for i:=7 downto 0 do
53    if x and (1<<i)>0 then s2:=s2+1<<i;
54  dfs2(s1,s2,0,0);
55 end;
56 
57 function cnt(x:longint):longint;
58 var ret,i,s:longint;
59 begin
60  ret:=0;
61  s1:=0; s2:=0;
62  for i:=15 downto 8 do
63   if x and (1<<i)>0 then s1:=s1+1<<(i-8);
64  for i:=7 downto 0 do
65   if x and (1<<i)>0 then s2:=s2+1<<i;
66  s:=s1;
67  while s>0 do
68  begin
69   ret:=ret+dp[s,s2];
70   s:=s1 and (s-1);
71  end;
72  ret:=ret+dp[0,s2];
73  exit(ret);
74 end;
75 
76 begin
77  assign(input,subset.in); reset(input);
78  assign(output,subset.out); rewrite(output);
79  readln(n);
80  for i:=1 to n do
81  begin
82   readln(ch);
83   x:=0;
84   for j:=5 to length(ch) do x:=x*10+ord(ch[j])-ord(0);
85   if ch[1]=a then add(x);
86   if ch[1]=d then del(x);
87   if ch[1]=c then writeln(cnt(x));
88  end;
89  close(input);
90  close(output);
91 end.

 

【2016多校】T3 subset(分块,DP)

标签:pre   reset   out   code   class   set   var   str   string   

原文地址:http://www.cnblogs.com/myx12345/p/6059957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!