CodeForces - 831C Jury Marks binary_search

Description

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

Example

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.

題意:

某人具有一個原始分數,經n個評委對分數進行加減,已知n個評委依次給出的分數和m個暫時總分(經某個評委給分後的暫時總分,亂序),問能求出多少種原始分數。

題解:

設原始分數爲x,先求出一個x,再看看這個x是否符合其他的條件。詳解如下:

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include<algorithm>
using namespace std;
int a[2005], b[2005];
int main()
{
    //freopen("data.in", "r", stdin);
    int n, m;
    while (~scanf("%d%d", &n, &m)) {
        scanf("%d", &a[0]);
        int temp;
        for (int i = 1; i < n; i++) {
            scanf("%d", &temp);
            a[i] = a[i - 1] + temp;//a[i]記錄評委給分的前綴和,便於求x
        }
        for (int i = 0; i < m; i++) { scanf("%d", &b[i]); }

        sort(a, a + n);//對a進行排序和去重
        int *end = unique(a, a + n);//去重函數需要在sort之後調用,返回最後一個元素的指針
        int ans = 0;
        for (int *i = a; i < end; i++) {
            bool flag = true;
            int x = b[0] - *i;//求一個原始分數x
            for (int j = 1; j < m; j++) {//判斷x能否滿足其他條件
                if (!binary_search(a, end, b[j] - x)) {
                    flag = false;
                    break;
                }
            }
            if (flag)   ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
發佈了44 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章