Jury Marks <思维题>

来源:互联网 发布:如何评价太祖知乎 编辑:程序博客网 时间:2024/06/14 19:30

                                                               Jury Marks                    

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 thek judges rated the participant there weren (n ≤ k) valuesb1, b2, ..., bn (it is guaranteed that all values bj are 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.

Input

The first line contains two integers k andn (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.

Output

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).

Example
Input
4 1-5 5 0 2010
Output
3
Input
2 2-2000 -20003998000 4000000
Output
1
Note

The answer for the first example is 3 because initially the participant could have - 10,10 or 15 points.

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


题目应该是可以算的上是一个水题的,但是刚开始拿到这道题目的时候不知道咋下手,问了一下大佬,听了一下思路,其实是蛮简单的。
具体思路:先求出所有的前缀和,然后我们找出所有可能的初始数,判断每一个初始数是否能够满足主人公记得的所有分数,如果满足,ans++;
那么怎么找初始数呢,拿第一个案例举例,主人公只记得10这个数,那么这个数可能是初始数加上-5得到,也可能是加上-5+5得到,也可能是加上-5+5+0+20得到
因为-5+5和-5+5+0是一样的,所以我们只需考虑一种即可,那么这样的话,初始数可能为15,10,-10,接下来就判断,这三个初始数是否满足主人公所记住的所有分数,答案显然是都满足的,因为这个案例特殊,只记住了一个,那我们从这个一个分数推出来的初始数一定满足条件,所以答案就是3,第二个案例类似来推,只要初始数满足主人公所记住的两个分数,ans++;

#include<cstdio>#include<cctype>#include<iostream>#include<stack>#include<map>#include<cstring>#include<string>#include<sstream>#include<queue>#include<set>using namespace std ;int main()  {    int k,n;    while(~scanf("%d %d",&k,&n)){             int a[2005],b[2005];            set<int>num;        for(int i=0;i<k;i++){            scanf("%d",&a[i]);            if(i)a[i]+=a[i-1];            num.insert(a[i]);//因为法官是按顺序给分的,所以前缀和有重复的我们就可以去掉,简便方法就是丢进set集合里        }        for(int i=0;i<n;i++)            scanf("%d",&b[i]);            int cou=0;        set<int>::iterator i=num.begin();    for(;i!=num.end();i++){            int ans=0;        int x=b[0]-*i;//设置任意一个值推出来的初始数        for(int j=0;j<n;j++)            if(num.count(b[j]-x)){//如果在集合里能找到分数-初始数,就说明这个初始数x能够推出当前b[j]            ans++;            }            if(ans==n){//如果当前的初始数x能够推出所有的b[j],结果cou++;                cou++;               // printf("%d\n",x);            }    }    printf("%d\n",cou);    }    return 0 ;}