CodeForces

来源:互联网 发布:xp系统分区软件 编辑:程序博客网 时间:2024/06/03 19:39
Jury Marks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 were n (n ≤ k) values b1, 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).

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


给出K个评委打的分数,和N个打分后当时的总分,推测出有几种初始分。先算出每个评委打分后加分的总和,去重,然后将一个当前分代入所有加分计算初始分,再求得该初始分下可能的当前分,最好检验已知当前分是否是推测当前分的子集。

由于要求的是初始分可能有几种,所以只需要cur[0]+sum[i]做一个循环即可,这个cur[0]一定会出现在某个加分后面,对应的初始分也是一定的,排列组合的方式并不重要。一开始把每个数都带了一遍,其实没必要,给自己增加累赘……

去重函数:unique(a,a+n),返回值是尾地址,因此通过减去a获得长度

二分查找:binary_search(a,a+n,x),返回值是bool

这次又当场造轮子……一查才知道有这两个现成的函数,对STL的理解还是有待进步

#include <iostream>#include <algorithm>using namespace std;int point[2001];int mark[2001];int sum[2001];int psb[2001];int beg;int cur[2001];int main(){        int k,n,i,j,t,cot,flag,su;        cin>>k>>n;        for(i=0;i<k;i++){            cin>>point[i];        }        for(i=0;i<n;i++){            cin>>cur[i];        }        su=0;        for(i=0;i<k;i++){            su+=point[i];            sum[i]=su;        }        sort(sum,sum+k);        int s=unique(sum,sum+k)-sum;        cot=0;        for(j=0;j<s;j++){            beg=cur[0]-sum[j];            for(t=0;t<k;t++){                mark[t]=beg+sum[t];            }            flag=1;            for(i=0;i<n;i++){                if(!binary_search(mark,mark+t,cur[i])){                    flag=0;                    break;                }            }            if(flag){                cot++;            }        }        cout<<cot<<endl;        return 0;}

原创粉丝点击