Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b1, b2, ..., bn (it is guaranteed that all values bjare distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
InputThe first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order.
The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
4 1 -5 5 0 20 10
3
2 2 -2000 -2000 3998000 4000000
1
完全不会做..看网上的题解看了好久才明白..真是怕了这种题了..
首先需要注意的是评委给的分数是有顺序的,从左到右的,这一点我们需要注意。
所以我们利用sum求前缀和即可。
之后我们对于某个初始值,要加上sum值,满足所有的过程分数。
我们图方便,从固定的某一个过程分开始计算。
对于第一个过程分,我们是第x个评委后得到的。所以原始分就是b【0】-sum【x】。
之后我们要保证其他已知过程分都在这个原始分经过所有sum之后的分数中。
#include<iostream>
#include<algorithm>
using namespace std;
int sum[2100];
int a[2100],b[2100];
int main()
{
int n,m;
sum[0]=0;
int shuchu=0;
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum[i+1]=sum[i]+a[i];
}
for(int i=0;i<m;i++)
{
cin>>b[i];
}
sort(sum+1,sum+1+n);
n=unique(sum+1,sum+1+n)-(sum+1);
int ans=n;
for(int i=1;i<=n;i++)//对于所有的评委分
{
int chushifen=b[0]-sum[i];
for(int j=1;j<m;j++)//对于其他过程分
{
int xuyaochazhi=b[j]-chushifen;
if(!binary_search(sum+1,sum+1+n,xuyaochazhi))//s没有找到,说明这个初始分不合格
{
ans--;
break;
}
}
}
cout<<ans<<endl;
}