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 kjudges 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 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.
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 20
10
Output
3
Input
2 2
-2000 -2000
3998000 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(1<=n<=k)个过程中的积分,问他的初始积分有多少可能。
【思路】首先根据每次的加分做一个前缀和运算,来获得到这个点为止所加的分,枚举初始积分可能的值,并判断在这个前提下所有n个过程中的积分是否都出现过。这里用set的size()和count()来实现会很方便,也可以用map来实现;
两种代码都差不多相同;
map代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
map <int, int> s;
int n, k;
int a[maxn], sum[maxn], gg[maxn];
int main()
{
int ans = 0;
scanf("%d %d",&k ,&n);
for(int i = 1;i <= k;i++)
scanf("%d", &a[i]); //评委的值
for(int i = 1;i <= k;i++) //前缀和
{
sum[i] += a[i];
sum[i + 1] = sum[i];
s[sum[i]] = 1; //将前缀和标记
}
for(int i = 1;i <= n;i++) scanf("%d",&gg[i]);
map <int, int>::iterator it; //map遍历
for(it = s.begin();it != s.end();it++)
{
int cs = gg[1] - it->first; //初始值 = 当前分数 - 评委给的分;其实gg[x]都行,只是为了避免出现n == 1的情况是出错才选择g[1]的;
int f = 0;
for(int j = 1;j <= n;j++)
if(s[gg[j] - cs] == 0) //评委给的分数 = 当前值 - 初始值;
{
f = 1; //有一个值没有出现过 pass掉
break;
}
if(f == 0) ans++; //否则表示当前初始值满足题意;
}
printf("%d\n",ans);
}
set的代码同理 :因为set里的元素一定是唯一的,判断一下所加的分数是否出现在set种就行;
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
set <int> s, t, d;
int n, k;
int a[maxn], sum[maxn], gg[maxn];
int main()
{
int ans = 0;
scanf("%d %d",&k ,&n);
for(int i = 1;i <= k;i++)
scanf("%d", &a[i]);//评委的值
for(int i = 1;i <= k;i++) //前缀和,到当前点裁判给了多少分;
{
sum[i] += a[i];
sum[i + 1] = sum[i];
s.insert(sum[i]); //将分数加到set中,set中不会有相同是数,也就是裁判给的分数和会出现哪几种
}
for(int i = 1;i <= n;i++) scanf("%d",&gg[i]);
set <int>::iterator it; //遍历set,从小到大
for(it = s.begin();it != s.end();it++)
{
int aw = *it; //set的当前元素
int cs = gg[1] - aw; //初始值 = 当前分数 - 评委给的分;
int f = 0;
for(int j = 1;j <= n;j++)
if(!s.count(gg[j] - cs)) //评委给的分数 = 当前值 - 初始值;
{
f = 1;//有一个值没有出现过 pass掉
break;
}
if(f == 0) ans++; //否则表示当前初始值满足题意;
}
printf("%d\n",ans);
}