Problem:
Square Route
スクウェア・ルート
English text is not available in this practice contest.
このたび新しい豪邸を建てることを決めた大富豪の品田氏は，どの街に新邸を建てようかと悩んでいる．実は，品田氏は正方形がとても好きという変わった人物であり，そのため少しでも正方形の多い街に住みたいと思っている．
品田氏は，碁盤目状に道路の整備された街の一覧を手に入れて，それぞれの街について，道路により形作られる正方形の個数を数えることにした．ところが，道と道の間隔は一定とは限らないため，手作業で正方形を数えるのは大変である．そこであなたには，碁盤目状の道路の情報が与えられたときに，正方形の個数を数えるプログラムを書いて欲しい．
Input
入力は複数のデータセットから構成されており，各データセットは以下のような構成になっている．
N
M
h
1
h
2
...
h
N
w
1
w
2
...
w
M
1 行目には 2 つの正の整数
N
,
M
(1 ≦
N
,
M
≦ 1500) が与えられる．続く
N
行
h
1
,
h
2
, ...,
h
N
(1 ≦
h
i
≦ 1000）は，道路と道路の南北方向の間隔を表す．ここで
h
i
は北から
i
番目の道路と北から
i
+ 1 番目の道路の間隔である．同様に，続く
M
行
w
1
, ...,
w
M
(1 ≦
w
i
≦ 1000）は，道路と道路の東西方向の間隔を表す．ここで
w
i
は西から
i
番目の道路と西から
i
+ 1 番目の道路の間隔である．道路自体の幅は十分細いため考慮する必要はない．
図 D-1: 最初のデータセット
N
=
M
= 0 は入力の終端を示しており，データセットには含めない．
Output
各データセットに対して，正方形の個数を 1 行に出力しなさい．たとえば，Sample Input の最初のデータセットにおいては，以下のとおり 6 個の正方形を含むので，このデータセットに対する出力は 6 となる．
図 D-2: 最初のデータセットに含まれる正方形
Sample Input
3 3
1
1
4
2
3
1
1 2
10
10
10
0 0
Output for the Sample Input
6
2

Submitted solutions:

code_snippet: #include<bits/stdc++.h>

using namespace std;

int main(){
    int n,m;
    while(cin>>n>>m,n){
        vector<int>h(n),w(m);
        for(int i=0;i<n;++i){
            cin>>h[i];
        }
        for(int i=0;i<m;++i){
            cin>>w[i];
        }
        vector<int>comb1(1500001,0);
        long long ans=0;
        for(int i=0;i<n;++i){
            for(int j=i;j<n;++j){
                int buf=0;
                for(int k=i;k<=j;++k){
                    buf+=h[k];
                }
                comb1[buf]++;
            }
        }
        for(int i=0;i<m;++i){
            for(int j=i;j<m;++j){
                int buf=0;
                for(int k=i;k<=j;++k){
                    buf+=w[k];
                }
                ans+=comb1[buf];
            }
        }
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 2290
memory_kb: 9252
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

// y 1-100000 c 1000000000 y < 100

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    const int MAX_SUM = 1500000; // 1500 * 1000
    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0 && m == 0) break;

        // vector を使って出現回数をカウントする
        vector<int> n_freq(MAX_SUM + 1, 0);
        vector<int> m_freq(MAX_SUM + 1, 0);

        vector<int> n_prefix(n + 1, 0);
        vector<int> m_prefix(m + 1, 0);

        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;
            n_prefix[i + 1] = n_prefix[i] + x;
        }
        for (int i = 0; i < m; i++) {
            int x;
            cin >> x;
            m_prefix[i + 1] = m_prefix[i] + x;
        }

        // 高さ方向の連続区間の和をカウント
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                int sum = n_prefix[j] - n_prefix[i];
                n_freq[sum]++;
            }
        }

        // 幅方向の連続区間の和をカウント
        for (int i = 0; i < m; i++) {
            for (int j = i + 1; j <= m; j++) {
                int sum = m_prefix[j] - m_prefix[i];
                m_freq[sum]++;
            }
        }

        // 正方形の個数を計算
        long long result = 0;
        for (int s = 1; s <= MAX_SUM; s++) {
            if (n_freq[s] && m_freq[s]) {
                result += (long long)n_freq[s] * m_freq[s];
            }
        }

        cout << result << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 15196
final_rank: 3

code_snippet: #include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std;
int square(int n, int m) {
    int h,w;
    vector<int> H(n);
    vector<int> W(m);
    for(int i=0; i<n;i++){
        cin>>H.at(i);
    }
    for(int i=0; i<m; i++){
        cin>>W.at(i);
    }
    for(int i=0; i<n; i++){
        h=H.at(i);
        for(int j=i+1; j<n; j++){
            h+=H.at(j);
            H.push_back(h);
        }
    }
    for(int i=0; i<m; i++){
        w=W.at(i);
        for(int j=i+1; j<m; j++){
            w+=W.at(j);
            W.push_back(w);
        }
    }
    sort(H.begin(), H.end());
    sort(W.begin(), W.end());
    int sum=0;
    h=H.size();
    w=W.size();
    int a=0;
    int b=0;
    while(a<h && b<w){
        if(H.at(a)<W.at(b)){
            a++;
        }
        else if(H.at(a)>W.at(b)){
            b++;
        }
        else{
            int s=1;
            int t=1;
            while(a<h-1 && H.at(a)==H.at(a+1)){
                s++;
                a++;
            }
            while(b<w-1 && W.at(b)==W.at(b+1)){
                t++;
                b++;
            }
            sum+=(s*t);
            a++;
            b++;
        }
    }
    return sum;
}

int main() {
    int n,m;
    while(cin>>n>>m && n!=0){
        cout<<square(n,m)<<endl;
    }
}
accuracy: 1
time_ms: 540
memory_kb: 18600
final_rank: 7

code_snippet: #include <iostream>
#include <iomanip>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <cassert>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(a) a.begin(), a.end()
#define arr(a) a.rbegin(), a.rend()
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
using ll = long long;

//Konishii

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(true) {
        int n, m; cin >> n >> m;
        if(n == 0 && m == 0) break;
        vector<int> h(n), w(m);
        rep(i, n) cin >> h[i];
        rep(i, m) cin >> w[i];

        int ma = 1500 * 1000;
        vector<int> cnth(ma + 10, 0), cntw(ma + 10, 0);
        vector<int> pfh(n + 1, 0), pfw(m + 1, 0);
        rep(i, n) pfh[i+1] += pfh[i] + h[i];
        rep(i, m) pfw[i+1] += pfw[i] + w[i];

        rep(i, n) {
            for(int j = i; j < n; j++) {
                int H = pfh[j+1] - pfh[i];
                cnth[H]++;
            }
        }
        rep(i, m) {
            for(int j = i; j < m; j++) {
                int W = pfw[j+1] - pfw[i];
                cntw[W]++;
            }
        }
        ll ans = 0;

        rep(i, ma + 1) {
            ans += ll(cnth[i]) * cntw[i];
        }
        cout << ans << "\n";
    }
}
accuracy: 1
time_ms: 100
memory_kb: 21172
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

void init() {}

ll n, m;
vector<ll> v, w;
void input() {
    cin >> n >> m;
    v.resize(n);
    w.resize(m);
    for(auto &x : v)
        cin >> x;
    for(auto &x : w)
        cin >> x;
}
void solve() {
    map<ll, a2> mp;
    for(int i = 0; i < n; i++) {
        ll now = 0;
        for(int j = i; j < n; j++) {
            now += v[j];
            mp[now][0]++;
        }
    }
    for(int i = 0; i < m; i++) {
        ll now = 0;
        for(int j = i; j < m; j++) {
            now += w[j];
            mp[now][1]++;
        }
    }

    ll ans = 0;
    for(auto &x : mp) {
        ans += x.second[0] * x.second[1];
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    init();
    while(1) {
        input();
        if(n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 1850
memory_kb: 27068
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define loop(n) for (int tjh = 0; tjh < (int)(n); tjh++)
constexpr int maxd=1500000;
constexpr int maxN=1500;
int N,M,L,te1;
array<int,maxd+1>h0,w0;
array<int,maxN>h,w;
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cout<<fixed<<setprecision(16);
  for(;cin>>N>>M,N;){
    L=min(N,M);
    memset(h0.data(),0,(L*1000+1)*4);
    memset(w0.data(),0,(L*1000+1)*4);
    rep(i,N)cin>>h[i];
    rep(i,M)cin>>w[i];
    rep(i,N){
      te1=0;
      for(int j=i;j<N;j++){
	te1+=h[j];
	h0[te1]++;
      }
    }
    rep(i,M){
      te1=0;
      for(int j=i;j<M;j++){
	te1+=w[j];
	w0[te1]++;
      }
    }
    te1=0;
    rep(i,L*1000+1)te1+=h0[i]*w0[i];
    cout<<te1<<'\n';
  }
}
accuracy: 1
time_ms: 20
memory_kb: 15180
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  while (true) {
    ll N, M;
    cin >> N >> M;
    if (N==0 && M==0)break;

    vector<ll> h(N), w(M);
    for (int i = 0; i < N; i++)
      cin >> h[i];
    for (int i = 0; i < M; i++)
      cin >> w[i];

    vector<ll> prefh(N + 1, 0), prefw(M + 1, 0);
    for (int i = 0; i < N; i++) {
      prefh[i + 1] = prefh[i] + h[i];
    }
    for (int i = 0; i < M; i++) {
      prefw[i + 1] = prefw[i] + w[i];
    }

    vector<ll> eh, ew;
    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        eh.push_back(prefh[j] - prefh[i]);
      }
    }
    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        ew.push_back(prefw[j] - prefw[i]);
      }
    }
    sort(eh.begin(), eh.end());
    sort(ew.begin(), ew.end());

    ll ans = 0;
    for (int i = 0; i < eh.size(); i++) {
      auto itl = lower_bound(ew.begin(), ew.end(), eh[i]);
      auto itr = upper_bound(ew.begin(), ew.end(), eh[i]);
      ans += itr - itl;
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 770
memory_kb: 44124
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  while(true) {
    ll H, W;
    cin >> H >> W;

    if(!H) { break; }

    vector<ll> h(H), w(W), sh{0}, sw{0};
    for(auto &i : h) {
      cin >> i;
      sh.emplace_back(sh.back() + i);
    }
    for(auto &i : w) {
      cin >> i;
      sw.emplace_back(sw.back() + i);
    }

    map<ll, ll> mh, mw;
    for(ll r = 0; r <= H; r++) {
      for(ll l = 0; l < r; l++) { mh[sh[r] - sh[l]]++; }
    }
    for(ll r = 0; r <= W; r++) {
      for(ll l = 0; l < r; l++) { mw[sw[r] - sw[l]]++; }
    }

    ll ans = 0;
    for(auto &i : mh) { ans += i.second * mw[i.first]; }

    cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 1640
memory_kb: 45184
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) return 0;
    vector<ll> A(N+1), B(M+1);
    A[0] = B[0] = 0;
    rep(i,0,N) {
        ll X;
        cin >> X;
        A[i+1] = A[i]+X;
    }
    rep(i,0,M) {
        ll X;
        cin >> X;
        B[i+1] = B[i]+X;
    }
    map<ll,ll> mp;
    rep(i,0,N+1) {
        rep(j,i+1,N+1) {
            mp[A[j]-A[i]]++;
        }
    }
    ll ANS = 0;
    rep(i,0,M+1) {
        rep(j,i+1,M+1) {
            ANS += mp[B[j]-B[i]];
        }
    }
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 1460
memory_kb: 27232
final_rank: 13

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip> //setprecision
#include <map> // map
#include <unordered_map> //unordered_map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
#include <sstream>
#include <initializer_list>
#include <random>
#include <unordered_set>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <functional>
#include <climits>
#include <utility>
#include <cassert>
//#include <atcoder/all> // aclibraryを使う際は g++ a.cpp -O2 -std=c++17 -I . でコンパイルする
//using namespace atcoder;
using namespace std;
const long long INF = 4000000000000000001; // 4*10^18
const int inf = 2001001001;
const long long MOD = 998244353;
const double pi = 3.141592653589;
const vector<int> dh = {1,0,-1,0} , dw = {0,1,0,-1};
const vector<int> dh8 = {-1,-1,-1,0,0,1,1,1} , dw8 = {-1,0,1,-1,1,-1,0,1};
#define endl "\n";
long long modpow(long long a, long long n, long long mod) {
    a %= mod;
    long long ret = 1;
    while (n > 0) {
        if (n & 1) ret = ret * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ret % mod;
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}
long long gcd(long long a, long long b) {
    if (b == 0) return a; else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}
long long get_mod(long long res){
    if(res < 0) res += MOD;
    return res % MOD;
}
double DegToRad(double deg){
    return deg*(pi/180.0);
}
double RadToDeg(double rad){
    return rad/(pi/180.0);
}
int uniform_rand(int mini , int maxi){
    random_device seed;
    mt19937_64 engine(seed());
    uniform_real_distribution<> ran(mini , maxi+1);
    int ret = ran(engine);
    if(ret >= maxi+1) ret = maxi;
    return ret;
}


int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    // FILE* in = freopen("mojain/in1.txt" , "r" , stdin); //2つ以上のケースを試したい場合はファイル名をループ回してin2.txt等で指定していく
    // FILE* out = freopen("mojaout/out1.txt", "w", stdout);
    // ifstream ifs("in.txt"); // 巨大ケースは in.txt に書いてifsで標準入力

    while(true){
        int N,M;cin >> N >> M;
        if(N == 0){
            break;
        }

        vector<int> H(N) , W(M);
        vector<int> ruiH(N+1) , ruiW(M+1);
        for(int i = 0;i<N;i++){
            cin >> H.at(i);
            ruiH.at(i+1) = ruiH.at(i) + H.at(i);
        }
        
        for(int i = 0;i<M;i++){
            cin >> W.at(i);
            ruiW.at(i+1) = ruiW.at(i) + W.at(i);
        }

        map<int,int> WM;
        for(int i = 0;i<M+1;i++){
            for(int j = i+1;j<M+1;j++){
                WM[ruiW.at(j)-ruiW.at(i)]++;
            }
        }

        int ans = 0;
        for(int i = 0;i<N+1;i++){
            for(int j = i+1;j<N+1;j++){
                ans += WM[ruiH.at(j)-ruiH.at(i)];
            }
        }

        cout << ans << endl;








        
        

        

    }
    

    

}
accuracy: 1
time_ms: 1380
memory_kb: 21188
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,k,n) for (int i = k; i < (n); ++i)
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
typedef pair<ll, ll> P;
const long long INF = 1e17;

int main(){
    while(true){
        int n,m;
        cin >> n >> m;
        if(n==0){
            break;
        }
        vector<int> h(n);
        for(int i=0;i<n;i++){
            cin >> h[i];
        }

        vector<int> w(m);
        for(int i=0;i<m;i++){
            cin >> w[i];
        }

        //sum_h[i-1]:h[0]からh[i]の和
        vector<int> sum_h(n+10);
        //sum_w[i-1]:w[0]からw[i]の和
        vector<int> sum_w(m+10);

        for(int i=1;i<=n;i++){
            sum_h[i] = sum_h[i-1] + h[i-1];
        }

        for(int i=1;i<=m;i++){
            sum_w[i] = sum_w[i-1] + w[i-1];
        }

        //高さ別度数分布
        vector<ll> h_cnt(2000000);
        //幅別度数分布
        vector<ll> w_cnt(2000000);

        for(int l=0;l<n;l++){
            for(int r=l+1;r<=n;r++){
                h_cnt[sum_h[r]-sum_h[l]]++;
            }
        }

        for(int l=0;l<m;l++){
            for(int r=l+1;r<=m;r++){
                w_cnt[sum_w[r]-sum_w[l]]++;
            }
        }

        ll ans = 0LL;

        for(int i=0;i<2000000;i++){
            ans += h_cnt[i]*w_cnt[i];
        }

        cout << ans << endl;


    }
    return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 34140
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define eb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<long long>;
using vvl = vector<vector<long long>>; using vs = vector<string>;
using pl = pair<long long, long long>;
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
template<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;
template <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}
template <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}
const ll INF = 1LL << 60;
const ld PI = acos(-1);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);
 
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif
void solve();
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
  int num_tc = 1;
  //cin >> num_tc;
  rep(tc,num_tc){
    //cout << "Case #" << tc+1 << ": " ;// << endl;
    solve();
  }
}
//見切り発車で実装しては行けない．頭の中でコードが完全に出来上がるまで書き始めるな．
void solve(){
  while(true){
    int N,M;cin>>N>>M;
    if(N==0&&M==0)return;
    map<ll,ll> mp;
    vl h(N);rep(i,N)cin>>h[i];
    vl w(M);rep(i,M)cin>>w[i];
    vl S(N+1),T(M+1);
    rep(i,N)S[i+1]+=S[i]+h[i];
    rep(i,M)T[i+1]+=T[i]+w[i];
    ll ans = 0;
    rep(l,N)for(int r=l+1;r<=N;r++)mp[S[r]-S[l]]++;
    rep(l,M)for(int r=l+1;r<=M;r++)ans+=mp[T[r]-T[l]];
    cout<<ans<<endl;
  }
}
accuracy: 1
time_ms: 1470
memory_kb: 27284
final_rank: 15

code_snippet: // g++-13 1.cpp -std=c++17 -O2 -I .
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/tag_and_trait.hpp>
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
 
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vst = vector<string>;
using vvst = vector<vst>;
 
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end())

random_device seed;
mt19937_64 randint(seed());

ll grr(ll mi, ll ma) { // [mi, ma)
    return mi + randint() % (ma - mi);
}

void solve(int n,int m){
  vi h(n+1,0);
  vi w(m+1,0);
  rep(i,0,n){
    int x;cin>>x;
    h[i+1]=h[i]+x;
  }
  rep(i,0,m){
    int x;cin>>x;
    w[i+1]=w[i]+x;
  }

  map<int,ll> mp;
  rep(i,0,n+1){
    rep(j,i+1,n+1){
      mp[h[j]-h[i]]++;
    }
  }

  ll ans=0;
  rep(i,0,m+1){
    rep(j,i+1,m+1){
      ans+=mp[w[j]-w[i]];
    }
  }

  cout<<ans<<endl;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  while(true){
    int n,m;cin>>n>>m;
    if(n==0&&m==0)return 0;
    solve(n,m);
  }
}
accuracy: 1
time_ms: 1500
memory_kb: 27196
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    while (true) {
        ll N, M, ans = 0;
        cin >> N >> M;
        if (N == 0 && M == 0) {
            break;
        }
        vector<ll> h(N + 1, 0), w(M + 1, 0);
        vector<ll> hz(N + 1, 0), wz(M + 1, 0);

        for (ll i = 1; i <= N; i++) cin >> h[i];
        for (ll i = 1; i <= M; i++) cin >> w[i];

        for (ll i = 1; i <= N; i++) {
            hz[i] = hz[i - 1] + h[i];
        }

        for (ll i = 1; i <= M; i++) {
            wz[i] = wz[i - 1] + w[i];
        }

        unordered_map<ll, ll> sum_count_h;
        for (ll i = 1; i <= N; i++) {
            for (ll j = 0; j < i; j++) {
                sum_count_h[hz[i] - hz[j]]++;
            }
        }

        for (ll i = 1; i <= M; i++) {
            for (ll j = 0; j < i; j++) {
                ans += sum_count_h[wz[i] - wz[j]];
            }
        }

        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 18788
final_rank: 6

code_snippet: #include<bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
//using mint=modint1000000007;
//using mint=modint998244353;

const ll INF=1ll<<60;
ll mod10=1e9+7;
ll mod99=998244353;
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<n;++i)
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

bool solve(){
    ll N,M;cin>>N>>M;
    if(N==0&&M==0) return 0;
    vll A(N),B(M);
    rep(i,N) cin>>A[i];
    rep(i,M) cin>>B[i];

    vll rw(1),rw2(1);
    rep(i,N) rw.push_back(rw[i]+A[i]);
    rep(i,M) rw2.push_back(rw2[i]+B[i]);

    map<ll,ll> mp;
    rep(i,M) rep2(j,i+1,M+1){
        mp[rw2[j]-rw2[i]]++;
    }

    ll ans=0;
    rep(i,N) rep2(j,i+1,N+1){
        ans+=mp[rw[j]-rw[i]];
    }
    cout<<ans<<endl;

    return 1;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(solve());
}
accuracy: 1
time_ms: 1460
memory_kb: 27292
final_rank: 14

code_snippet: #include <bits/stdc++.h>

int solve() {
    int n, m;
    std::cin >> n >> m;
    if (n == 0) return 1;
    std::vector<long long> a(n + 1), b(m + 1);
    for (int i = 0; i < n; i++) {
        long long v;
        std::cin >> v;
        a[i + 1] = a[i] + v;
    }
    for (int i = 0; i < m; i++) {
        long long v;
        std::cin >> v;
        b[i + 1] = b[i] + v;
    }
    std::map<long long, long long> mp;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j < i; j++) {
            mp[a[i] - a[j]]++;
        }
    }
    long long ans = 0;
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j < i; j++) {
            ans += mp[b[i] - b[j]];
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

int main() {
    // int n;
    // std::cin >> n;
    // std::cin.ignore();
    // while (n--) solve();
    while (!solve());
}
accuracy: 1
time_ms: 1520
memory_kb: 27168
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
    while (1)
    {

        int n, m;
        cin >> n >> m;
        if (n == 0 && m == 0)
            break;
        vector<ll> h(n + 1);
        vector<ll> w(m + 1);
        for (int i = 1; i <= n; i++)
            cin >> h[i];
        for (int i = 1; i <= m; i++)
            cin >> w[i];

        for (int i = 1; i <= n; i++)
            h[i] += h[i - 1];
        for (int i = 1; i <= m; i++)
            w[i] += w[i - 1];

        map<ll, ll> x;
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                x[h[j] - h[i]]++;
            }
        }
        ll ans = 0;

        for (int i = 0; i < m; i++)
        {
            for (int j = i + 1; j <= m; j++)
            {
                ans += x[w[j] - w[i]];
            }
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 1460
memory_kb: 27148
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define rep(i,j,n) for (int i = j; i < (int)(n); i++)
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define all(x) (x.begin(), x.end())
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b){
  bool compare = (a < b);
  if(compare) a = b;
  return compare;
}
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b){
  bool compare = (a > b);
  if(compare) a = b;
  return compare;
}
string substrback(string s,size_t p,size_t l){
	//s=文字列　p=後ろから数えて何文字目　l=pから何文字目まで
	const size_t strl = s.length();
	return s.substr(strl - p,l);
}
//pair型のfirstで比較
bool comparePairs(const pair<ll,ll> &a, const pair<ll,ll> &b){
	if(a.first != b.first)
	return a.first<b.first;
	return a.second < b.second;
}
//pair型のsecondで比較
bool comparePairs2(const pair<ll,ll> &a, const pair<ll,ll> &b){
	if(a.second != b.second)
	return a.second<b.second;
	return a.first < b.first;
}
//pair型の１.second  と2.firstをつなげていく
bool customsort(const pair<ll,ll>  &a,const pair<ll,ll> &b){
	if(a.second == b.first){
		return true;
	}
	return false;
}
bool customsort2(const pair<ll, ll> &a, const pair<ll, ll> &b) {
    // 特定の値がfirstに出た場合、そのsecond値を先頭に持ってくる
    if (a.first == -1) {
        return true;
    } else if (b.first == -1) {
        return false;
    }
    return a.first < b.first;
}

//nの階乗
ull facto(ll n){
	if (n==0 || n ==1){
		return 1;
	} else {
		return n *facto(n-1);
	}
}
//nのΣ
ll sum(ll n) {
	ll sum =0;
	sum = n*(n+1)/2;
	return sum;
}
struct V {
	int a, b;
};
bool acom(const V& a, const V& b) {
	return a.a < b.a;
}
bool bcom(const V& a, const V& b) {
	return a.b < b.b;
}
//やりましょう!!
bool solve() {
	int n, m;
	cin>>n>>m;
	if(n==0&&m==0) return false;
	vector<int> h(n+1);
	vector<int> w(m+1);
	int current = 0;
	rep(i,0,n) {
		int x;
		cin>>x;
		h[i]=current;
		current+=x;
	}
	h[n]=current;
	current = 0;
	rep(i,0,m) {
		int x;
		cin>>x;
		w[i]=current;
		current+=x;
	}
	w[m]=current;
	vector<int> ruih, ruiw;
	for (int width = 1; width <= n; width++) {
		for (int start = 0; start <= n-width; start++) {
			ruih.push_back(h[start+width] - h[start]);
		}
	}
	for (int width = 1; width <= m; width++) {
		for (int start = 0; start <= m-width; start++) {
			ruiw.push_back(w[start+width] - w[start]);
		}
	}
	sort(ruiw.begin(), ruiw.end());
	ll ans = 0;
	rep(i,0,ruih.size()) {
		auto start = lower_bound(ruiw.begin(), ruiw.end(), ruih[i]);
		if(start==ruiw.end()) continue; 
		auto end = upper_bound(ruiw.begin(), ruiw.end(), ruih[i]);
		ans+=(end-start);
	}
	cout<<ans<<endl;
	return true;
}
int main() {
	while(solve());
}
accuracy: 1
time_ms: 450
memory_kb: 22872
final_rank: 8

code_snippet: #pragma region Macros
#include <bits/stdc++.h>

using namespace std;

using lint = long long;

using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define log(x) cout << x << endl
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;
#define logy(bool)         \
  if (bool) {              \
    cout << "Yes" << endl; \
  } else {                 \
    cout << "No" << endl;  \
  }

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion

int main() {
  cin.tie(0)->sync_with_stdio(0);
  while (1) {
    int n, m, ans = 0;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    vector<int> h(n), w(m), hs(n + 1, 0), ws(m + 1, 0), t(1500001, 0), y(1500001, 0);
    rep(i, n) {
      cin >> h[i];
    }
    rep(i, m) {
      cin >> w[i];
    }
    rep(i, n) {
      hs[i + 1] = hs[i] + h[i];
    }
    rep(i, m) {
      ws[i + 1] = ws[i] + w[i];
    }
    rep(i, n) {
      rep(j, i + 1, n + 1) {
        t[hs[j] - hs[i]]++;
      }
    }
    rep(i, m) {
      rep(j, i + 1, m + 1) {
        y[ws[j] - ws[i]]++;
      }
    }
    rep(i, 1, 1500001) {
      ans += t[i] * y[i];
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 300
memory_kb: 14968
final_rank: 4

code_snippet: #include <bits/stdc++.h>

#define rep(i, N) for(ll i = 0; i < (ll)(N); ++i)
#define per(i, N) for(ll i = (ll)(N) - 1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) (((n) >> (k)) & 1)
#define pcnt(n) (bitset<64>(n).count())
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back
template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& V) {
    for (auto& it : V) is >> it;
    return is;
}
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& V) {
    for (int i = 0 ; i < (int)V.size() ; i++) {
        os << V[i] << (i + 1 == (int)V.size() ? "" : " ");
    }
    return os;
}
using namespace std;

using ll = long long;

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    while(1){
        int N, M; cin >> N >> M;
        if(N == 0 && M == 0) break;

        vector<ll> H(N), W(M); cin >> H >> W;
        vector<ll> sumH(N + 1), sumW(M + 1);
        rep(i, N){
            sumH[i + 1] = sumH[i] + H[i];
        }
        rep(i, M){
            sumW[i + 1] = sumW[i] + W[i];
        }

        vector<ll> num(1000010, 0);
        rep(i, N){
            for(int j = i + 1; j <= N; j++){
                num[sumH[j] - sumH[i]]++;
            }
        }
        ll ans = 0;
        rep(i, M){
            for(int j = i + 1; j <= M; j++){
                ans += num[sumW[j] - sumW[i]];
            }
        }

        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 40
memory_kb: 11016
final_rank: 1