Codeforces Round #502-D- The Wu(暴力)

D. The Wu

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Childan is making up a legendary story and trying to sell his forgery — a necklace with a strong sense of "Wu" to the Kasouras. But Mr. Kasoura is challenging the truth of Childan's story. So he is going to ask a few questions about Childan's so-called "personal treasure" necklace.

This "personal treasure" is a multiset SS of mm "01-strings".

A "01-string" is a string that contains nn characters "0" and "1". For example, if n=4n=4, strings "0110", "0000", and "1110" are "01-strings", but "00110" (there are 55 characters, not 44) and "zero" (unallowed characters) are not.

Note that the multiset SS can contain equal elements.

Frequently, Mr. Kasoura will provide a "01-string" tt and ask Childan how many strings ss are in the multiset SS such that the "Wu" value of the pair (s,t)(s,t) is not greater than kk.

Mrs. Kasoura and Mr. Kasoura think that if si=tisi=ti (1≤i≤n1≤i≤n) then the "Wu" value of the character pair equals to wiwi, otherwise 00. The "Wu" value of the "01-string" pair is the sum of the "Wu" values of every character pair. Note that the length of every "01-string" is equal to nn.

For example, if w=[4,5,3,6]w=[4,5,3,6], "Wu" of ("1001", "1100") is 77 because these strings have equal characters only on the first and third positions, so w1+w3=4+3=7w1+w3=4+3=7.

You need to help Childan to answer Mr. Kasoura's queries. That is to find the number of strings in the multiset SS such that the "Wu" value of the pair is not greater than kk.

Input

The first line contains three integers nn, mm, and qq (1≤n≤121≤n≤12, 1≤q,m≤5⋅1051≤q,m≤5⋅105) — the length of the "01-strings", the size of the multiset SS, and the number of queries.

The second line contains nn integers w1,w2,…,wnw1,w2,…,wn (0≤wi≤1000≤wi≤100) — the value of the ii-th caracter.

Each of the next mm lines contains the "01-string" ss of length nn — the string in the multiset SS.

Each of the next qq lines contains the "01-string" tt of length nn and integer kk (0≤k≤1000≤k≤100) — the query.

Output

For each query, print the answer for this query.

Examples

input

Copy

2 4 5
40 20
01
01
10
11
00 20
00 40
11 20
11 40
11 60

output

Copy

2
4
2
3
4

input

Copy

1 2 4
100
0
1
0 0
0 100
1 0
1 100

output

Copy

1
2
1
2

Note

In the first example, we can get:

"Wu" of ("01", "00") is 4040.

"Wu" of ("10", "00") is 2020.

"Wu" of ("11", "00") is 00.

"Wu" of ("01", "11") is 2020.

"Wu" of ("10", "11") is 4040.

"Wu" of ("11", "11") is 6060.

In the first query, pairs ("11", "00") and ("10", "00") satisfy the condition since their "Wu" is not greater than 2020.

In the second query, all strings satisfy the condition.

In the third query, pairs ("01", "11") and ("01", "11") satisfy the condition. Note that since there are two "01" strings in the multiset, the answer is 22, not 11.

In the fourth query, since kk was increased, pair ("10", "11") satisfies the condition too.

In the fifth query, since kk was increased, pair ("11", "11") satisfies the condition too.

题意:给你m个由01构成的字符串,每个的长度为n,然后给你一个长为n的w数组,然后有q次查询。

每次查询给你一个字符串t和一个数字k,询问你pair(ai,t)<=k的字符串的个数。

其中pair(a,b)的计算方式是如果a字符串的第i位和b字符串的第i位相同的话,则pair(a,b)+w[i]。

题解:因为每个字符串的长度最长是12,我们可以暴力所有字符串的匹配时的pair值,也只有4096*4096

之后我们发现k只有100这么大!!! 因此我们可以开一个数组用来存某个字符串和相应pair值出现的次数。

这样我们每次查询是就可以O(100)查询了。。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
int n,m,q,w[105],qq[20];
int a,b[4099],c[500005];
int val[5000][105];
char s1[500005][15],s2[15],s3[15];
void init()
{
	qq[0]=1;
	for(int i=1;i<=13;i++)
		qq[i]=qq[i-1]*2;
}
int main(void)
{
	init();
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",&w[i]);
	for(int i=1;i<=m;i++)
	{
		scanf("%s",s1[i]+1);
		for(int j=n;j>0;j--)
			if(s1[i][j]=='1')
				c[i]+=qq[n-j];
		b[c[i]]++;
	}
	for(int i=0;i<(1<<n);i++)
		for(int j=0;j<(1<<n);j++)
		{
			int k;a=0;
			for(k=0;k<n;k++)
				if((i&(1<<k))==(j&(1<<k)))
					a+=w[n-k];
			if(a<=100)
				val[i][a]+=b[j];
		}
	while(q--)
	{
		int k;
		ll ans=0,t=0;
		scanf("%s%d",s2+1,&k);
		for(int i=n;i>0;i--)
			if(s2[i]=='1')
				t+=qq[n-i];
		for(int i=0;i<=k;i++)
			ans+=val[t][i];
		printf("%lld\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/haut_ykc/article/details/81544915