这题的数据好弱…直接用map水过去了
但是这里map的复杂度还不够优秀,需要使用unordered_map
#include <bits/stdc++.h>
using namespace std;
const int maxn=2009;
int a[maxn],b[maxn],pre[maxn];
unordered_map<int,int>m;
unordered_map<int,int>mb;
unordered_map<int,int>mpre;
int n,k,ans;
int main()
{
cin >> n >> k;
for(int i=1;i<=n;i++) cin >> a[i],pre[i]=pre[i-1]+a[i];
for(int i=1;i<=k;i++) cin >> b[i],mb[b[i]]=1;
sort(b+1,b+1+k);
for(int i=1;i<=n;i++)//枚举b[1]放在哪个位置
{
int chu = b[1]-pre[i],temp=0;
if( mpre[chu] ) continue;
mpre[ chu ]=1;
for(int j=1;j<=n;j++)
{
chu+=a[j];
if( m[chu]==0&&mb[chu] ) temp++,m[chu]=1;
if(n-j+temp<k) break;//剪枝,假设后面全都可行
}
if(temp==k) ans++;
m.clear();
}
cout<<ans;
}