n维前缀和
DP法
每一维考虑,从低维向高维转移。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define endl '\n'
const int MAXN = 1e5;
const int MD = 7; //每维小于7
const double eps = 1e-6;
int n, t;
map<int, int> sum;
void clean()
{
sum.clear();
}
void solve()
{
clean();
cin >> t >> n;
int MA = 1; // state范围
for (int i = 1; i <= n; ++i)
MA *= MD;
// input t points of n dimension
for (int i = 1; i <= t; ++i)
{
int state = 0;
for (int j = 1; j <= n; ++j)
{
int x;
cin >> x;
state = state * MD + x;
}
sum[state] = 1;
}
int now = 1;
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < MA; ++j)
{
if (j % (now * MD) / now)
sum[j] += sum[j - now];
}
now *= MD;
}
int q;
cin >> q;
while (q--)
{
int sta = 0;
for (int i = 1; i <= n; ++i)
{
int x;
cin >> x;
sta = sta * MD + x;
}
cout << sum[sta] << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(NULL);
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
容斥原理
利用容斥原理方法
标签:MD,const,前缀,int,sum,state,sta From: https://www.cnblogs.com/0CarryT0/p/16874035.html