CF Round #424( Div.2) C. Jury Marks 【思维+STL】

来源:互联网 发布:淘宝怎么做全屏海报 编辑:程序博客网 时间:2024/06/05 03:26


C. Jury Marks


time limit per test 2 seconds

memory limit per test     256 megabytes


Polycarp watched TV-show wherek jury members one by one rated aparticipant 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 thei-th jury member gave ai points.

Polycarp does not remember how many points the participant hadbefore this k marks were given, but he remembers that among the scoresannounced after each of thek judges rated the participant there were n (n ≤ k) values b1, b2, ..., bn (it is guaranteed that allvalues bj are distinct). It ispossible 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 scorethe participant could have before the judges rated the participant.

Input

The first line contains two integersk and n (1 ≤ n ≤ k ≤ 2 000) —the number of jury members and the number of scores Polycarp remembers.

The second line containsk integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order.

The third line containsndistinct integers b1, b2, ..., bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that thesevalues are not necessarily given in chronological order.

Output

Print the number of options for the score the participant could have before thejudges rated the participant. If Polycarp messes something up and there is nooptions, print "0"(without quotes).

Examples

Input

4 1
-5 5 0 20
10

Output

3

Input

2 2
-2000 -2000
3998000 4000000

Output

1

Note

The answer for the first example is3 because initially the participantcould have  - 10,10 or 15 points.

In the second example there is only one correct initial score equaling to4 002 000.

 

【题意】现在有一个人,有一个初始积分,接下来有k个人为他加分或减分,然后告诉你n(1<=n<=k)个积分减分过程中的积分,问根据这些信息,他的初始积分有多少可能

【思路】首先我们对加减分做一个前缀和运算,便于直接从某一个状态算出初始积分,因为每个过程分可能出现在任意位置,所以我们应该把它放在任意位置算出每种可能的初始积分,然后把这些可能值放入vector容器中,对于每个过程分重复这个操作,最后借助map容器判断vector容器中有几个初始积分出现了至少n次(每种限制条件都满足),然后输出即可。

但是交了一发WA了,后来找到了一组数据,就知道自己错哪里了:

3 2

5 -5 5

3 7

错误输出:2

正确输出:0

原因在于前缀和可能有相同的值,造成了一个过程值对应某一个初始值多次,影响结果,对此我们只要对前缀和做一个去重处理就AC了。


#include <cstdio>#include <map>#include <iostream>#include <queue>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn= 2005;const int mod = 20090717;const int INF = 0x3f3f3f3f;const double eps = 1e-6;int sum[maxn];vector<int>vec;map<int,int>mp;int main(){    int n,k;    int x;    sum[0]=0;    scanf("%d%d",&n,&k);    for(int i=1;i<=n;i++)    {        scanf("%d",&x);        sum[i]=sum[i-1]+x;    }    sort(sum+1,sum+1+n);    int len=unique(sum+1,sum+1+n)-(sum+1);    //printf("%d\n",len);    for(int i=1;i<=k;i++)    {        scanf("%d",&x);        for(int j=1;j<=len;j++)        {            int temp=x-sum[j];            vec.push_back(temp);        }    }    sort(vec.begin(),vec.end());    int num=0;    for(int i=0;i<vec.size();i++)    {        mp[vec[i]]++;    }    for(int i=0;i<vec.size();i++)    {        if(mp[vec[i]]>=k)        {            num++;            mp[vec[i]]=0;        }    }    printf("%d\n",num);}


阅读全文
1 0
原创粉丝点击