Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) D. The Wu(思维)
D. The Wu
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
解析:
那个表达式其实就是对异或之后的值取反
得到的一个字符串,位置为1则有对应的值
所以预处理出所有的结果串
然后算出各种串的数量
再预处理出每种串和其他种串得到的结果
做一个前缀和即可
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
const int N=1e4+10;
int i,j,k,l,s,n,m,q,a[N],B[N],A[N],G[N][105];
char c[N];
int main()
{
#ifdef local
freopen("D://rush.txt","r",stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>m>>q;
for (i=n;i;i--)
cin>>a[i];
for (i=0;i<(1<<n);i++)
{
int x=i,j=1,s=0;
while (x)
{
if (x&1) s+=a[j];
x>>=1;
j++;
}
A[i]=min(101,s);
}
for (i=1;i<=m;i++) {
cin>>c;
s=0;
for (j=0;j<n;j++) s=s*2+c[j]-'0';
B[s]++;
}
for (i=0;i<(1<<n);i++)
{
for (j=0;j<(1<<n);j++)
{
if (!B[j]) continue;
s=((1<<n)-1)^i^j;
G[i][A[s]]+=B[j];
}
for (j=1;j<=100;j++) G[i][j]+=G[i][j-1];
}
for (i=1;i<=q;i++)
{
cin>>c>>k;
s=0;
for (j=0;j<n;j++) s=s*2+c[j]-'0';
cout<<G[s][k]<<endl;
}
return 0;
}