#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <ctime>

//#define DISPLAYTIME

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)

#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define REP_S(it, str) for (char*it=str;*it;++it)
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it])

#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())

#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define P(T) pair<T, T>
#define Pint pair<int, int>
#define fi first
#define se second
#define D(v) (cout << #v << " is equal to " << v << endl)
#define DP(v) (cout << #v << " is equal to " << v.fi << " " << v.se << endl)
#define ZERO(v) memset(v,0,sizeof(v))
#define MIO(v) memset(v,-1,sizeof(v))

const int maxn = 1e5 + 7;
LL sum[maxn];
int n, k;

int baka(int a) {
    return (a < 0 ? -a : a);
}
int judge(LL p) {
    if(p == 0) return 0;
    if(p == 1) return 1;

    int tk = abs(k);
    if(tk == 1) return 0;
    // exp even
    if(k > 0) {
        if(p < 0) return 0;
        double ex = log(p) / log(tk);
        int orz = ex + 1e-9;
        double mid = ex - orz;
        if(fabs(mid) < 1e-6) {
            return 1;
        }
    } else {
        if(p > 0) {
            double ex = log(p) / log(tk);
            int orz = ex + 1e-9;
            if(orz & 1) return 0;
            double mid = ex - orz;
            if(fabs(mid) < 1e-6) {
                return 1;
            }
        } else {
            //p<0
            //odd!
            int tp = -p;
            double ex = log(tp) / log(tk);
            int orz = ex + 1e-9;
            if(orz % 2 == 0) return 0;
            double mid = ex - orz;
            if(fabs(mid) < 1e-6) {
                return 1;
            }
        }
    }
    return 0;
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("o.txt","w",stdout);
#ifdef DISPLAYTIME
    clock_t start, end;
    start = clock();
#endif
#endif

#ifndef LOCAL
    std::ios::sync_with_stdio(false);
#endif
    cin >> n >> k;
    int a;
    //map<int, vector<int> > m;
    REP_1(i, n) {
        cin >> a;
        sum[i] += sum[i - 1] + a;
        //m[sum[i]].PB(i);
    }
    //cout << judge(-9) << endl;
    //return 0;
    int cnt = 0;
    for(int i = 0; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            LL del = sum[j] - sum[i];
            if(judge(del)) {
                //cout << i << " " << j << " " << del << endl;
                cnt++;
            }
        }
    }
    cout << cnt << endl;

#ifdef DISPLAYTIME
#ifdef LOCAL
    end = clock();
    cout << "*************************" << endl;
    cout << "Computing time: " << (double)(end - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
#endif
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <ctime>

//#define DISPLAYTIME

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)

#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define REP_S(it, str) for (char*it=str;*it;++it)
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it])

#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())

#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define P(T) pair<T, T>
#define Pint pair<int, int>
#define fi first
#define se second
#define D(v) (cout << #v << " is equal to " << v << endl)
#define DP(v) (cout << #v << " is equal to " << v.fi << " " << v.se << endl)
#define ZERO(v) memset(v,0,sizeof(v))
#define MIO(v) memset(v,-1,sizeof(v))
const int maxn = 1e5 + 7;
LL a[maxn], f[maxn];

int n, k;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("o.txt","w",stdout);
#ifdef DISPLAYTIME
    clock_t start, end;
    start = clock();
#endif
#endif

#ifndef LOCAL
    std::ios::sync_with_stdio(false);
#endif

    cin >> n >> k;
    REP_1(i, n) {
        cin >> a[i];
    }
    REP_1(i, n) {
        f[i] = f[i - 1] + a[i];
    }
    //for(int  i = 1; i <= n; i++) {
    //    cout << f[i] << endl;
    //}

    //int l = 0, r = 0;
    LL cur = 1;
    vector<LL> v;
    REP(i, 50) {
        v.PB(cur);
        cur *= k;
    }
    sort(ALL(v));
    int cnt = 0;
    //map<LL, int> m;
    for(int j = 0; j <= n; j++) {
        for(int i = 0; i < j; i++) {
            LL temp = f[j] - f[i];
            if(binary_search(ALL(v), temp)) {
                cnt++;
            }
        }
    }
    cout << cnt << endl;

#ifdef DISPLAYTIME
#ifdef LOCAL
    end = clock();
    cout << "*************************" << endl;
    cout << "Computing time: " << (double)(end - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
#endif
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <ctime>

//#define DISPLAYTIME

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)

#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define REP_S(it, str) for (char*it=str;*it;++it)
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it])

#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())

#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define P(T) pair<T, T>
#define Pint pair<int, int>
#define fi first
#define se second
#define D(v) (cout << #v << " is equal to " << v << endl)
#define DP(v) (cout << #v << " is equal to " << v.fi << " " << v.se << endl)
#define ZERO(v) memset(v,0,sizeof(v))
#define MIO(v) memset(v,-1,sizeof(v))
const int maxn = 1e5 + 7;
LL a[maxn], f[maxn];

int n, k;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("o.txt","w",stdout);
#ifdef DISPLAYTIME
    clock_t start, end;
    start = clock();
#endif
#endif

#ifndef LOCAL
    std::ios::sync_with_stdio(false);
#endif

    cin >> n >> k;
    REP_1(i, n) {
        cin >> a[i];
    }
    REP_1(i, n) {
        f[i] = f[i - 1] + a[i];
    }

    LL cur = 1;
    vector<LL> v;

    LL cnt = 0;
    map<LL, int> m;
    if(k == 1) {
        for(int i = n; i >= 0; i--) {
            cnt += m[f[i] + 1];
            m[f[i]]++;
        }
    } else if(k == -1) {
        v.PB(1);
        v.PB(-1);
        for(int i = n; i >= 0; i--) {
            cnt += m[f[i] + 1];
            cnt += m[f[i] - 1];
            m[f[i]]++;
        }
    } else {
        REP(i, 50) {
            v.PB(cur);
            cur *= k;
        }
        sort(ALL(v));
        for(int i = n; i >= 0; i--) {
            REP(j, SZ(v)) {
                cnt += m[f[i] + v[j]];
            }
            m[f[i]]++;
        }
    }
    cout << cnt << endl;

#ifdef DISPLAYTIME
#ifdef LOCAL
    end = clock();
    cout << "*************************" << endl;
    cout << "Computing time: " << (double)(end - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
#endif
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <ctime>

//#define DISPLAYTIME

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)

#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define REP_S(it, str) for (char*it=str;*it;++it)
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it])

#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())

#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define P(T) pair<T, T>
#define Pint pair<int, int>
#define fi first
#define se second
#define D(v) (cout << #v << " is equal to " << v << endl)
#define DP(v) (cout << #v << " is equal to " << v.fi << " " << v.se << endl)
#define ZERO(v) memset(v,0,sizeof(v))
#define MIO(v) memset(v,-1,sizeof(v))

const int maxn = 1e5 + 7;
LL sum[maxn];
int n, k;

int baka(int a) {
    return (a < 0 ? -a : a);
}
int judge(LL p) {
    if(p == 0) return 0;
    if(p == 1) return 1;

    int tk = abs(k);
    if(tk == 1) return 0;
    // exp even
    if(k > 0) {
        if(p < 0) return 0;
        double ex = log(p) / log(tk);
        int orz = ex + 1e-9;
        if(orz & 1 && (k > 0)) return 0;
        double mid = ex - orz;
        if(fabs(mid) < 1e-6) {
            return 1;
        }
    } else {
        if(p > 0) {
            double ex = log(p) / log(tk);
            int orz = ex + 1e-9;
            if(orz & 1) return 0;
            double mid = ex - orz;
            if(fabs(mid) < 1e-6) {
                return 1;
            }
        } else {
            //p<0
            //odd!
            int tp = -p;
            double ex = log(tp) / log(tk);
            int orz = ex + 1e-9;
            if(orz % 2 == 0) return 0;
            double mid = ex - orz;
            if(fabs(mid) < 1e-6) {
                return 1;
            }
        }
    }
    return 0;
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("o.txt","w",stdout);
#ifdef DISPLAYTIME
    clock_t start, end;
    start = clock();
#endif
#endif

#ifndef LOCAL
    std::ios::sync_with_stdio(false);
#endif
    cin >> n >> k;
    int a;
    //map<int, vector<int> > m;
    REP_1(i, n) {
        cin >> a;
        sum[i] += sum[i - 1] + a;
        //m[sum[i]].PB(i);
    }
    //cout << judge(-9) << endl;
    //return 0;
    int cnt = 0;
    for(int i = 0; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            LL del = sum[j] - sum[i];
            if(judge(del)) {
                //cout << i << " " << j << " " << del << endl;
                cnt++;
            }
        }
    }
    cout << cnt << endl;

#ifdef DISPLAYTIME
#ifdef LOCAL
    end = clock();
    cout << "*************************" << endl;
    cout << "Computing time: " << (double)(end - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
#endif
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <ctime>

//#define DISPLAYTIME

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)

#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define REP_S(it, str) for (char*it=str;*it;++it)
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it])

#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())

#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define P(T) pair<T, T>
#define Pint pair<int, int>
#define fi first
#define se second
#define D(v) (cout << #v << " is equal to " << v << endl)
#define DP(v) (cout << #v << " is equal to " << v.fi << " " << v.se << endl)
#define ZERO(v) memset(v,0,sizeof(v))
#define MIO(v) memset(v,-1,sizeof(v))
const int maxn = 1e5 + 7;
LL a[maxn], f[maxn];

int n, k;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("o.txt","w",stdout);
#ifdef DISPLAYTIME
    clock_t start, end;
    start = clock();
#endif
#endif

#ifndef LOCAL
    std::ios::sync_with_stdio(false);
#endif

    cin >> n >> k;
    REP_1(i, n) {
        cin >> a[i];
    }
    REP_1(i, n) {
        f[i] = f[i - 1] + a[i];
    }

    LL cur = 1;
    vector<LL> v;
    if(k == 1) {
        v.PB(1);
    } else if(k == -1) {
        v.PB(1);
        v.PB(-1);
    } else {
        REP(i, 50) {
            v.PB(cur);
            cur *= k;
        }
    }
    sort(ALL(v));
    int cnt = 0;
    map<LL, int> m;
    for(int i = n; i >= 0; i--) {
        REP(j, SZ(v)) {
            if(m[f[i] + v[j]]) {
                cnt++;
            }
        }
        m[f[i]]++;
    }
    cout << cnt << endl;

#ifdef DISPLAYTIME
#ifdef LOCAL
    end = clock();
    cout << "*************************" << endl;
    cout << "Computing time: " << (double)(end - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
#endif
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <ctime>

//#define DISPLAYTIME

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)

#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define REP_S(it, str) for (char*it=str;*it;++it)
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it])

#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())

#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define P(T) pair<T, T>
#define Pint pair<int, int>
#define fi first
#define se second
#define D(v) (cout << #v << " is equal to " << v << endl)
#define DP(v) (cout << #v << " is equal to " << v.fi << " " << v.se << endl)
#define ZERO(v) memset(v,0,sizeof(v))
#define MIO(v) memset(v,-1,sizeof(v))
const int maxn = 1e5 + 7;
LL a[maxn], f[maxn];
int n, k;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("o.txt","w",stdout);
#ifdef DISPLAYTIME
    clock_t start, end;
    start = clock();
#endif
#endif

#ifndef LOCAL
    std::ios::sync_with_stdio(false);
#endif
    cin >> n >> k;
    REP_1(i, n) {
        cin >> a[i];
    }
    REP_1(i, n) {
        f[i] = f[i - 1] + a[i];
    }
    LL cur = 1;
    vector<LL> v;
    LL cnt = 0;
    map<LL, int> m;
    if(k == 1) {
        for(int i = n; i >= 0; i--) {
            cnt += m[f[i] + 1];
            m[f[i]]++;
        }
    } else if(k == -1) {
        for(int i = n; i >= 0; i--) {
            cnt += m[f[i] + 1];
            cnt += m[f[i] - 1];
            m[f[i]]++;
        }
    } else {
        REP(i, 50) {
            if(cur >= 1e15) break;
            if(cur <= -1e15) break;
            v.PB(cur);
            cur *= k;
        }
        for(int i = n; i >= 0; i--) {
            REP(j, SZ(v)) {
                cnt += m[f[i] + v[j]];
            }
            m[f[i]]++;
        }
    }
    cout << cnt << endl;

#ifdef DISPLAYTIME
#ifdef LOCAL
    end = clock();
    cout << "*************************" << endl;
    cout << "Computing time: " << (double)(end - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
#endif
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <ctime>

//#define DISPLAYTIME

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)

#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)

#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define REP_S(it, str) for (char*it=str;*it;++it)
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it])

#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())

#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define P(T) pair<T, T>
#define Pint pair<int, int>
#define fi first
#define se second
#define D(v) (cout << #v << " is equal to " << v << endl)
#define DP(v) (cout << #v << " is equal to " << v.fi << " " << v.se << endl)
#define ZERO(v) memset(v,0,sizeof(v))
#define MIO(v) memset(v,-1,sizeof(v))
const int maxn = 1e5 + 7;
LL a[maxn], f[maxn];

int n, k;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("o.txt","w",stdout);
#ifdef DISPLAYTIME
    clock_t start, end;
    start = clock();
#endif
#endif

#ifndef LOCAL
    std::ios::sync_with_stdio(false);
#endif

    cin >> n >> k;
    REP_1(i, n) {
        cin >> a[i];
    }
    REP_1(i, n) {
        f[i] = f[i - 1] + a[i];
    }

    LL cur = 1;
    vector<LL> v;

    int cnt = 0;
    map<LL, int> m;
    if(k == 1) {
        for(int i = n; i >= 0; i--) {
            cnt += m[f[i] + 1];
            m[f[i]]++;
        }
    } else if(k == -1) {
        v.PB(1);
        v.PB(-1);
        for(int i = n; i >= 0; i--) {
            cnt += m[f[i] + 1];
            cnt += m[f[i] - 1];
            m[f[i]]++;
        }
    } else {
        REP(i, 50) {
            v.PB(cur);
            cur *= k;
        }
        sort(ALL(v));
        for(int i = n; i >= 0; i--) {
            REP(j, SZ(v)) {
                cnt += m[f[i] + v[j]];
            }
            m[f[i]]++;
        }
    }
    cout << cnt << endl;

#ifdef DISPLAYTIME
#ifdef LOCAL
    end = clock();
    cout << "*************************" << endl;
    cout << "Computing time: " << (double)(end - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
#endif
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define MS(x, y) memset(x, y, sizeof(x))
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORND(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; }
template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; }
using namespace std;
const int maxn = 100005;
/*
bool fu = false;
ll fp[maxn];

long long fast_pow( long long a, long long b ) {
	if (fp[b]) return fp[b];
	if (a == 1) return fp[b] = 1;
    long long ans = 1;
    ll curb = b;
    while( b ) {
        if ( b & 1 ) //若b当前最右位为1
            ans = ans * a;
        a = a * a; //(a^(2^k))^2=a^(2^(k+1))
        b >>= 1; //b向右位移
    }
	if (!fu && b & 1) fp[curb] = -ans;
	else fp[curb] = ans;
    return ans;
}

ll sum[maxn];
ll a[maxn];
int main(int argc, char const *argv[])
{
	int n, k; scanf("%d%d", &n, &k);
	if (k < 0) { fu = false, k = -k; }
	else fu = true;
	MS(sum, 0);
	MS(fp, 0);
	ll ans = 0;
	FORN(i, n) {
		scanf("%I64d", a + i);

		if (i) sum[i] = sum[i - 1] + a[i];
		else sum[i] = a[i];
		FOR(j, 0, i) {
			int pow = 1;
			ll cur;
			if (j) cur = sum[i] - sum[j - 1];
			else cur = sum[i];
			// cout << cur ;
			while ( abs(cur) >= abs(fast_pow(k, pow))) {
				if (cur == fast_pow(k, pow)) {
					ans++;
					//cout << j << ' ' << i << ' ' << cur <<  endl;
					break;
				} else {
					if (k == 1) break;
					pow++;
				}
			}
		}
	}
	printf("%I64d\n", ans);

	return 0;
}*/

map<ll, int> Map;
int main() {
	int n, k; scanf("%d%d", &n, &k);
	ll res = 0, sum = 0;;
	Map[0] = 1;
	FORN(i, n) {
		int a; scanf("%d", &a);
		sum += a;
		Map[sum]++;
		if (k == 1) {
			res += Map[sum - 1];
		} else if (k == -1) {
			res += Map[sum - 1] + Map[sum + 1];
			break;
		}

		ll t = 1;
		while (t < 1e15 && t > -1e15 ) {
			res += Map[sum - t];
			t *= k;
		}
	}
	printf("%I64d\n", res);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define MS(x, y) memset(x, y, sizeof(x))
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORND(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; }
template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; }
using namespace std;
const int maxn = 100005;

bool fu = false;
ll fp[maxn];

long long fast_pow( long long a, long long b ) {
	if (fp[b]) return fp[b];
	if (a == 1) return fp[b] = 1;
    long long ans = 1;
    ll curb = b;
    while( b ) {
        if ( b & 1 ) //若b当前最右位为1
            ans = ans * a;
        a = a * a; //(a^(2^k))^2=a^(2^(k+1))
        b >>= 1; //b向右位移
    }
	if (!fu && b & 1) fp[curb] = -ans;
	else fp[curb] = ans;
    return ans;
}

ll sum[maxn];
ll a[maxn];
int main(int argc, char const *argv[])
{
	int n, k; scanf("%d%d", &n, &k);
	if (k < 0) { fu = false, k = -k; }
	else fu = true;
	MS(sum, 0);
	MS(fp, 0);
	ll ans = 0;
	FORN(i, n) {
		scanf("%I64d", a + i);

		if (i) sum[i] = sum[i - 1] + a[i];
		else sum[i] = a[i];
		FOR(j, 0, i) {
			int pow = 1;
			ll cur;
			if (j) cur = sum[i] - sum[j - 1];
			else cur = sum[i];
			// cout << cur ;
			while ( abs(cur) >= abs(fast_pow(k, pow))) {
				if (cur == fast_pow(k, pow)) {
					ans++;
					//cout << j << ' ' << i << ' ' << cur <<  endl;
					break;
				} else {
					if (k == 1) break;
					pow++;
				}
			}
		}
	}
	printf("%I64d\n", ans);

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define MS(x, y) memset(x, y, sizeof(x))
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORND(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; }
template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; }
using namespace std;
const int maxn = 100005;

bool fu = false;
ll fp[maxn];

long long fast_pow( long long a, long long b ) {
	if (fp[b]) return fp[b];
    long long ans = 1;
    ll curb = b;
    while( b ) {
        if ( b & 1 ) //若b当前最右位为1
            ans = ans * a;
        a = a * a; //(a^(2^k))^2=a^(2^(k+1))
        b >>= 1; //b向右位移
    }
	if (!fu) fp[curb] = -ans;
	else fp[curb] = ans;
    return ans;
}

ll sum[maxn];
ll a[maxn];
int main(int argc, char const *argv[])
{
	int n, k; scanf("%d%d", &n, &k);
	if (k < 0) { fu = false, k = -k; }
	else fu = true;
	MS(sum, 0);
	MS(fp, 0);
	ll ans = 0;
	FORN(i, n) {
		scanf("%I64d", a + i);

		if (i) sum[i] = sum[i - 1] + a[i];
		else sum[i] = a[i];
		FOR(j, 0, i) {
			int pow = 1;
			ll cur;
			if (j) cur = sum[i] - sum[j - 1];
			else cur = sum[i];
			// cout << cur ;
			while ( abs(cur) >= abs(fast_pow(k, pow))) {
				//cout << ' ' << fp[pow] << endl;]
				if (cur == fp[pow]) {
					ans++;
					//cout << j << ' ' << i << ' ' << cur <<  endl;
					break;
				} else {
					pow++;
				}
			}
		}
	}
	printf("%I64d\n", ans);

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define MS(x, y) memset(x, y, sizeof(x))
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORND(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; }
template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; }
using namespace std;
const int maxn = 100005;

bool fu = false;
ll fp[maxn];

long long fast_pow( long long a, long long b ) {
	if (fp[b]) return fp[b];
	if (a == 1) return fp[b] = 1;
    long long ans = 1;
    ll curb = b;
    while( b ) {
        if ( b & 1 ) //若b当前最右位为1
            ans = ans * a;
        a = a * a; //(a^(2^k))^2=a^(2^(k+1))
        b >>= 1; //b向右位移
    }
	if (!fu) fp[curb] = -ans;
	else fp[curb] = ans;
    return ans;
}

ll sum[maxn];
ll a[maxn];
int main(int argc, char const *argv[])
{
	int n, k; scanf("%d%d", &n, &k);
	if (k < 0) { fu = false, k = -k; }
	else fu = true;
	MS(sum, 0);
	MS(fp, 0);
	ll ans = 0;
	FORN(i, n) {
		scanf("%I64d", a + i);

		if (i) sum[i] = sum[i - 1] + a[i];
		else sum[i] = a[i];
		FOR(j, 0, i) {
			int pow = 1;
			ll cur;
			if (j) cur = sum[i] - sum[j - 1];
			else cur = sum[i];
			// cout << cur ;
			while ( abs(cur) >= abs(fast_pow(k, pow))) {
				if (cur == fast_pow(k, pow)) {
					ans++;
					//cout << j << ' ' << i << ' ' << cur <<  endl;
					break;
				} else {
					if (k == 1) break;
					pow++;
				}
			}
		}
	}
	printf("%I64d\n", ans);

	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define MS(x, y) memset(x, y, sizeof(x))
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORND(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; }
template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; }
using namespace std;
const int maxn = 100005;
/*
bool fu = false;
ll fp[maxn];

long long fast_pow( long long a, long long b ) {
	if (fp[b]) return fp[b];
	if (a == 1) return fp[b] = 1;
    long long ans = 1;
    ll curb = b;
    while( b ) {
        if ( b & 1 ) //若b当前最右位为1
            ans = ans * a;
        a = a * a; //(a^(2^k))^2=a^(2^(k+1))
        b >>= 1; //b向右位移
    }
	if (!fu && b & 1) fp[curb] = -ans;
	else fp[curb] = ans;
    return ans;
}

ll sum[maxn];
ll a[maxn];
int main(int argc, char const *argv[])
{
	int n, k; scanf("%d%d", &n, &k);
	if (k < 0) { fu = false, k = -k; }
	else fu = true;
	MS(sum, 0);
	MS(fp, 0);
	ll ans = 0;
	FORN(i, n) {
		scanf("%I64d", a + i);

		if (i) sum[i] = sum[i - 1] + a[i];
		else sum[i] = a[i];
		FOR(j, 0, i) {
			int pow = 1;
			ll cur;
			if (j) cur = sum[i] - sum[j - 1];
			else cur = sum[i];
			// cout << cur ;
			while ( abs(cur) >= abs(fast_pow(k, pow))) {
				if (cur == fast_pow(k, pow)) {
					ans++;
					//cout << j << ' ' << i << ' ' << cur <<  endl;
					break;
				} else {
					if (k == 1) break;
					pow++;
				}
			}
		}
	}
	printf("%I64d\n", ans);

	return 0;
}*/

map<ll, int> Map;
int main() {
	int n, k; scanf("%d%d", &n, &k);
	ll res = 0, sum = 0;;
	Map[0] = 1;
	FORN(i, n) {
		int a; scanf("%d", &a);
		sum += a;
		Map[sum]++;
		if (k == 1) {
			res += Map[sum - 1];
		} else if (k == -1) {
			res += Map[sum - 1] + Map[sum + 1];
			break;
		}

		ll t = 1;
		while ( true ) {
			res += Map[sum - t];
			t *= k;
			if ( !(t < 1e15 && t > -1e15) ) break;
		}
	}
	printf("%I64d\n", res);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define MS(x, y) memset(x, y, sizeof(x))
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORND(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; }
template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; }
using namespace std;
const int maxn = 100005;
/*
bool fu = false;
ll fp[maxn];

long long fast_pow( long long a, long long b ) {
	if (fp[b]) return fp[b];
	if (a == 1) return fp[b] = 1;
    long long ans = 1;
    ll curb = b;
    while( b ) {
        if ( b & 1 ) //若b当前最右位为1
            ans = ans * a;
        a = a * a; //(a^(2^k))^2=a^(2^(k+1))
        b >>= 1; //b向右位移
    }
	if (!fu && b & 1) fp[curb] = -ans;
	else fp[curb] = ans;
    return ans;
}

ll sum[maxn];
ll a[maxn];
int main(int argc, char const *argv[])
{
	int n, k; scanf("%d%d", &n, &k);
	if (k < 0) { fu = false, k = -k; }
	else fu = true;
	MS(sum, 0);
	MS(fp, 0);
	ll ans = 0;
	FORN(i, n) {
		scanf("%I64d", a + i);

		if (i) sum[i] = sum[i - 1] + a[i];
		else sum[i] = a[i];
		FOR(j, 0, i) {
			int pow = 1;
			ll cur;
			if (j) cur = sum[i] - sum[j - 1];
			else cur = sum[i];
			// cout << cur ;
			while ( abs(cur) >= abs(fast_pow(k, pow))) {
				if (cur == fast_pow(k, pow)) {
					ans++;
					//cout << j << ' ' << i << ' ' << cur <<  endl;
					break;
				} else {
					if (k == 1) break;
					pow++;
				}
			}
		}
	}
	printf("%I64d\n", ans);

	return 0;
}*/

map<ll, int> Map;

int main() {
	int n, k; scanf("%d%d", &n, &k);
	ll res = 0, sum = 0;;
	Map[0] = 0;
	FORN(i, n) {
		int a; scanf("%d", &a);
		sum += a;
		Map[sum]++;
		if (k == 1) {
			res += Map[sum - 1];
		} else if (k == -1) {
			res += Map[sum - 1] + Map[sum + 1];
			break;
		}

		ll t = 1;
		while (t < 1e15 && t > -1e15 ) {
			res += Map[sum - t];
			t *= k;
		}
	}
	printf("%I64d\n", res);
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define MS(x, y) memset(x, y, sizeof(x))
#define FORN(i, n) for (int i = 0; i < n; i++)
#define FORND(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; --i)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; }
template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; }
using namespace std;
const int maxn = 100005;
/*
bool fu = false;
ll fp[maxn];

long long fast_pow( long long a, long long b ) {
	if (fp[b]) return fp[b];
	if (a == 1) return fp[b] = 1;
    long long ans = 1;
    ll curb = b;
    while( b ) {
        if ( b & 1 ) //若b当前最右位为1
            ans = ans * a;
        a = a * a; //(a^(2^k))^2=a^(2^(k+1))
        b >>= 1; //b向右位移
    }
	if (!fu && b & 1) fp[curb] = -ans;
	else fp[curb] = ans;
    return ans;
}

ll sum[maxn];
ll a[maxn];
int main(int argc, char const *argv[])
{
	int n, k; scanf("%d%d", &n, &k);
	if (k < 0) { fu = false, k = -k; }
	else fu = true;
	MS(sum, 0);
	MS(fp, 0);
	ll ans = 0;
	FORN(i, n) {
		scanf("%I64d", a + i);

		if (i) sum[i] = sum[i - 1] + a[i];
		else sum[i] = a[i];
		FOR(j, 0, i) {
			int pow = 1;
			ll cur;
			if (j) cur = sum[i] - sum[j - 1];
			else cur = sum[i];
			// cout << cur ;
			while ( abs(cur) >= abs(fast_pow(k, pow))) {
				if (cur == fast_pow(k, pow)) {
					ans++;
					//cout << j << ' ' << i << ' ' << cur <<  endl;
					break;
				} else {
					if (k == 1) break;
					pow++;
				}
			}
		}
	}
	printf("%I64d\n", ans);

	return 0;
}*/

map<ll, int> Map;
int main() {
	int n, k; scanf("%d%d", &n, &k);
	ll res = 0, sum = 0;;
	Map[0] = 1;
	FORN(i, n) {
		int a; scanf("%d", &a);
		sum += a;
		if (k == 1) {
			res += Map[sum - 1];
		} else if (k == -1) {
			res += Map[sum - 1] + Map[sum + 1];
		} else {
			ll t = 1;
			while ( true ) {
				res += Map[sum - t];
				t *= k;
				if ( !(t <= 1e15 && t >= -1e15) ) break;
			}
		}
		Map[sum]++;
	}
	printf("%I64d\n", res);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
set<ll> s;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	while(m[tot]<(ll)1e14){
		m[++tot]=m[tot-1]*k;
	}
	/*/
	for(int i=0;i<=tot;i++){
		cout<<i<<' '<<m[i]<<endl;
	}
	/*/
	s.insert(0);
//	cout<<vis[1]<<endl;//
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=1;j<=tot;j++){
			if(s.count((ll)sum[i]-m[j])) ans++;
		}
		s.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
set<ll> s;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	if(abs(k)!=1) while(m[tot]<(ll)1e14){
		m[++tot]=m[tot-1]*k;
	}
	if(k==-1) m[++tot]=-1;
	s.insert(0);
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=0;j<=tot;j++){
			ll tmp=sum[i]-m[j];
			if(s.count(tmp)) ans++;
		}
		s.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
set<ll> s;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	if(abs(k)!=1){
		while(abs(m[tot])<(ll)1e14){
			m[++tot]=m[tot-1]*k;
		}
		tot--;
	}
	if(k==-1) m[++tot]=-1;
	s.insert(0);
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=0;j<=tot;j++){
			ll tmp=sum[i]-m[j];
			if(s.count(tmp)) ans++;
		}
		s.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
set<ll> s;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	//cout<<k<<endl;//
	m[0]=1;
	if(abs(k)!=1){
		while(abs(m[tot])<(ll)1e16){
			tot++;
			m[tot]=m[tot-1]*k;
		}
	}
	if(k==-1) m[++tot]=-1;
	/*/
	for(int i=0;i<=tot;i++){
		cout<<i<<' '<<m[i]<<endl;
	}
	/*/
	s.insert(0);
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=0;j<=tot;j++){
			ll tmp=sum[i]-m[j];
			//cout<<tmp<<endl;//
			if(s.count(tmp)) ans++;
		}
		s.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
map<ll,int> vis;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	while(m[tot]<(ll)1e14){
		m[++tot]=m[tot-1]*k;
	}
	/*/
	for(int i=0;i<=tot;i++){
		cout<<i<<' '<<m[i]<<endl;
	}
	/*/
	vis[0]=1;
//	cout<<vis[1]<<endl;//
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		vis[sum[i]]=1;
		for(int j=1;j<=n;j++){
			if(vis[(ll)sum[i]-m[j]]==1) ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}


/*
User:Small
Language:C++
Problem No.:
*/
#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,k,sum[M],a[M],mi[105],tot,ans;
map<ll,int> ms;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	mi[0]=1;
	if(abs(k)!=1){
		while(mi[tot]<1e15){
			tot++;
			mi[tot]=mi[tot-1]*k;
			//cout<<mi[tot]<<endl;//
		}
	}
	else if(k==-1){
		mi[++tot]=-1;
	}
	ms[0]=1;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=0;j<=tot;j++){
			ans+=ms[sum[i]-mi[j]];
		}
		ms[sum[i]]++;
	}
	cout<<ans<<endl;
	return 0;
}


/*
User:Small
Language:C++
Problem No.:
*/
#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,k,sum[M],a[M],mi[105],tot,ans;
multiset<ll> ms;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	mi[0]=1;
	if(abs(k)!=1){
		while(mi[tot]<1e15){
			tot++;
			mi[tot]=mi[tot-1]*k;
			//cout<<mi[tot]<<endl;//
		}
	}
	else if(k==-1){
		mi[++tot]=-1;
	}
	ms.insert(0);
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=0;j<=tot;j++){
			ans+=ms.count(sum[i]-mi[j]);
		}
		ms.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
set<ll> s;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	if(abs(k)!=1) while(m[tot]<(ll)1e14){
		m[++tot]=m[tot-1]*k;
	}
	if(k==-1) m[++tot]=1;
	/*/
	for(int i=0;i<=tot;i++){
		cout<<i<<' '<<m[i]<<endl;
	}
	/*/
	s.insert(0);
//	cout<<vis[1]<<endl;//
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=0;j<=tot;j++){
			ll tmp=sum[i]-m[j];
			if(s.count(tmp)) ans++;
		}
		s.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
set<ll> s;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	while(m[tot]<(ll)1e14){
		m[++tot]=m[tot-1]*k;
	}
	/*/
	for(int i=0;i<=tot;i++){
		cout<<i<<' '<<m[i]<<endl;
	}
	/*/
	s.insert(0);
//	cout<<vis[1]<<endl;//
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=0;j<=tot;j++){
			ll tmp=sum[i]-m[j];
			if(s.count(tmp)) ans++;
		}
		s.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
set<ll> s;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	while(m[tot]<(ll)1e14){
		m[++tot]=m[tot-1]*k;
	}
	/*/
	for(int i=0;i<=tot;i++){
		cout<<i<<' '<<m[i]<<endl;
	}
	/*/
	s.insert(0);
//	cout<<vis[1]<<endl;//
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		for(int j=1;j<=tot;j++){
			ll tmp=sum[i]-m[j];
			if(s.count(tmp)) ans++;
		}
		s.insert(sum[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const int M=100005;
ll n,a[M],sum[M],k,m[105],tot,ans;
map<int,int> vis;
int main(){
//	freopen("data.in","r",stdin);//
	ios::sync_with_stdio(false);
	cin>>n>>k;
	m[0]=1;
	while(m[tot]<(ll)1e14){
		m[++tot]=m[tot-1]*k;
	}
	/*/
	for(int i=0;i<=tot;i++){
		cout<<i<<' '<<m[i]<<endl;
	}
	/*/
	vis[0]=1;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		vis[sum[i]]=1;
		for(int j=1;j<=n;j++){
			if(vis[(ll)sum[i]-m[j]]==1) ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,int>mmp;
    //mmp.clear();
    long long sum=0;
    long long cnt=0;
    long long total=0;;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) {scanf("%d",&x[i]);if(x[i]>0) total+=x[i];}
    long long p;
    for(p=1;p<=total;p*=k)
    {  mmp.clear();
       mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        } if(p==1) break;
    }
  printf("%I64d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,int>mmp;
    //mmp.clear();
    long long sum=0;
    long long cnt=0;
    long long total=0;;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) {scanf("%d",&x[i]);if(x[i]>0) total+=x[i];}
    long long p;
    for(p=1;p<=total;p*=k)
    {  mmp.clear();
       mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        }
    }
  printf("%I64d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,long long>mmp;
    mmp.clear();
    long long sum=0;
    long long cnt=0;
    long long total=0;;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) {scanf("%d",&x[i]);if(x[i]>0) total+=x[i];}
    long long p;
    for(p=1;p<=total;p*=k)
    {  mmp.clear();
       mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        }
    }
  printf("%I64d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,int>mmp;
    //mmp.clear();
    long long sum=0;
    long long cnt=0;
    long long total1=0,total2=0;;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) {scanf("%d",&x[i]);if(x[i]>=0) total1+=x[i];else total2+=x[i];}
    long long p;
    for(p=1;p>0&&p<=total1||p<0&&p>=total2;p*=k)
    {  mmp.clear();
       mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        } if(p*k==1) break;
    }

  printf("%I64d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,long long>mmp;
    mmp.clear();
    long long sum=0;
    long long cnt=0;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) scanf("%d",&x[i]);
    long long p;
    for(p=1;p<=1e15;p*=k)
    {  mmp.clear();
       mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        }
    }
  printf("%I64d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int powk(int a,int b)
{
   int ans=1;
   if(b==0) return 1;
   while(b)
   {
       if(b&1) ans*=a;
       a*=a;
       b>>=1;
   } return ans;
}
int main()
{
    int n,k,x[100005],i,j,y[100005];
    long long sum=0;
    int cnt=0;
  //printf("%d\n",powk(-2,7));
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++){
    scanf("%d",&x[i]);
    if(x[i]>0) sum+=x[i];
    if(i==1) y[i]=x[i];
    else y[i]=y[i-1]+x[i];}
    y[0]=0;
    for(i=0;powk(k,i)<=sum;i++)
    {   long long p=powk(k,i);
        for(j=1;j<=n;j++)
        {
           for(int h=0;h<j;h++)
           {
               if(y[j]-y[h]==p) cnt++;
           }

        }
    }printf("%d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,int>mmp;
    //mmp.clear();
    long long sum=0;
    long long cnt=0;
    long long total=0;;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) {scanf("%d",&x[i]);if(x[i]>0) total+=x[i];}
    long long p;
    for(p=1;p<=total;p*=k)
    {  mmp.clear();
       mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        } if(p*k==1) break;
    }

  printf("%I64d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,int>mmp;
    //mmp.clear();
    long long sum=0;
    long long cnt=0;
    long long total=0;;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) {scanf("%d",&x[i]);if(x[i]>0) total+=x[i];}
    long long p;
    for(p=1;p<=1e15;p*=k)
    {  mmp.clear();
       mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        } if(p*k==1) break;
    }

  printf("%I64d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k,x[100005],i,j,y[100005];
    map<long long,long long>mmp;
    mmp.clear();
    long long sum=0;
    int cnt=0;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++) scanf("%d",&x[i]);
    long long p;
    for(p=1;p<=1e15;p*=k)
    { mmp.clear();
     mmp[0]=1;sum=0;
        for(i=1;i<=n;i++)
        {
           sum+=x[i];
           cnt+=mmp[sum-p];
           mmp[sum]++;
        }

    }
  printf("%d\n",cnt);
}


#include<bits/stdc++.h>
using namespace std;
#define pc putchar_unlocked
#define gc getchar_unlocked
typedef long long ll;
typedef unsigned long long llu;
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scstr(x) scanf("%s",x)
#define pd(x) printf("%d",x)
#define pstr(x) printf("%s",x)
#define newl() printf("\n")
#define fl(i,n) for (i=0;i<n;i++)
#define fle(i,n) for (i=1;i<=n;i++)
#define fla(i,a,n) for (i=a;i<n;i++)
#define mem(a,i) memset(a,i,sizeof(a))
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define gcd __gcd
#define wl(n) while (n--)
#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) v.begin(),v.end()
#define tatt(cont) typeof(cont.begin())
const double eps=0.000000000000001 ;
#define MOD 1000000007
typedef long double LF;
typedef double lF;
#define hihihaha ((double)CLOCKS_PER_SEC)
#define nl putchar('\n')
//-------------
int n;
ll k;
int A[100010];
ll cm[100010];
int main()
{	//ios::sync_with_stdio(0), cin.tie(0);
	sc(n);scl(k);
	vector<ll>v;
	int i,j,mx;
	for (i=1;i<=n;i++)
	{
		sc(A[i]);
		mx=max(mx,A[i]);
		cm[i]=(ll)cm[i-1]+A[i];
	}
	ll x=1;
	while (1)
	{
		v.pb(x);
		x*=k;
		if (x>(1e15))
			break;

	}
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	map<ll,int>ma;
	ma[0]=1;
	ll ans=0;
	map<ll,int>::iterator it;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<v.size();j++)
		{
			it=ma.find(cm[i]-v[j]);
			ans+=it->se;
		}
		ma[cm[i]]++;
	}
	printf("%lld\n",ans);

	return 0;

}

//01010110 01101100 01100001 01100100 00101110


#include<bits/stdc++.h>
using namespace std;
#define pc putchar_unlocked
#define gc getchar_unlocked
typedef long long ll;
typedef unsigned long long llu;
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scstr(x) scanf("%s",x)
#define pd(x) printf("%d",x)
#define pstr(x) printf("%s",x)
#define newl() printf("\n")
#define fl(i,n) for (i=0;i<n;i++)
#define fle(i,n) for (i=1;i<=n;i++)
#define fla(i,a,n) for (i=a;i<n;i++)
#define mem(a,i) memset(a,i,sizeof(a))
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define gcd __gcd
#define wl(n) while (n--)
#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) v.begin(),v.end()
#define tatt(cont) typeof(cont.begin())
const double eps=0.000000000000001 ;
#define MOD 1000000007
typedef long double LF;
typedef double lF;
#define hihihaha ((double)CLOCKS_PER_SEC)
#define nl putchar('\n')
//-------------
int n;
ll k;
int A[100010];
ll cm[100010];
int main()
{	//ios::sync_with_stdio(0), cin.tie(0);
	sc(n);scl(k);
	vector<ll>v;
	int i,j,mx;
	for (i=1;i<=n;i++)
	{
		sc(A[i]);
		mx=max(mx,A[i]);
		cm[i]=(ll)cm[i-1]+A[i];
	}
	ll x=1;
	if (abs(k)==1)
	{
		v.pb(1);
		if (k<0)
			v.pb(-1);

	}
	else
	while (1)
	{
		v.pb(x);
		x*=k;
		if (abs(x)>(1000000000000000LL))
			break;

	}
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	map<ll,int>ma;
	ma[0]=1;
	ll ans=0;
	map<ll,int>::iterator it;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<v.size();j++)
		{
			it=ma.find(cm[i]-v[j]);
			ans+=it->se;
		}
		ma[cm[i]]++;
	}
	printf("%lld",ans);

	return 0;

}

//01010110 01101100 01100001 01100100 00101110


#include<bits/stdc++.h>
using namespace std;
#define pc putchar_unlocked
#define gc getchar_unlocked
typedef long long ll;
typedef unsigned long long llu;
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scstr(x) scanf("%s",x)
#define pd(x) printf("%d",x)
#define pstr(x) printf("%s",x)
#define newl() printf("\n")
#define fl(i,n) for (i=0;i<n;i++)
#define fle(i,n) for (i=1;i<=n;i++)
#define fla(i,a,n) for (i=a;i<n;i++)
#define mem(a,i) memset(a,i,sizeof(a))
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define gcd __gcd
#define wl(n) while (n--)
#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) v.begin(),v.end()
#define tatt(cont) typeof(cont.begin())
const double eps=0.000000000000001 ;
#define MOD 1000000007
typedef long double LF;
typedef double lF;
#define hihihaha ((double)CLOCKS_PER_SEC)
#define nl putchar('\n')
//-------------
int n;
ll k;
int A[100010];
ll cm[100010];
int main()
{	//ios::sync_with_stdio(0), cin.tie(0);
	sc(n);scl(k);
	vector<ll>v;
	int i,j,mx;
	for (i=1;i<=n;i++)
	{
		sc(A[i]);
		mx=max(mx,A[i]);
		cm[i]=cm[i-1]+A[i];
	}
	ll x=1;
	while (1)
	{
		v.pb(x);
		x*=k;
		if (x>(1e15))
			break;

	}
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	map<ll,int>ma;
	ma[0]=1;
	ll ans=0;
	map<ll,int>::iterator it;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<v.size();j++)
		{
			it=ma.find(cm[i]-v[j]);
			ans+=it->se;
		}
		ma[cm[i]]++;
	}
	printf("%lld\n",ans);

	return 0;

}

//01010110 01101100 01100001 01100100 00101110


#include<bits/stdc++.h>
using namespace std;
#define pc putchar_unlocked
#define gc getchar_unlocked
typedef long long ll;
typedef unsigned long long llu;
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scstr(x) scanf("%s",x)
#define pd(x) printf("%d",x)
#define pstr(x) printf("%s",x)
#define newl() printf("\n")
#define fl(i,n) for (i=0;i<n;i++)
#define fle(i,n) for (i=1;i<=n;i++)
#define fla(i,a,n) for (i=a;i<n;i++)
#define mem(a,i) memset(a,i,sizeof(a))
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define gcd __gcd
#define wl(n) while (n--)
#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) v.begin(),v.end()
#define tatt(cont) typeof(cont.begin())
const double eps=0.000000000000001 ;
#define MOD 1000000007
typedef long double LF;
typedef double lF;
#define hihihaha ((double)CLOCKS_PER_SEC)
#define nl putchar('\n')
//-------------
int n;
ll k;
int A[100010];
ll cm[100010];
int main()
{	//ios::sync_with_stdio(0), cin.tie(0);
	sc(n);scl(k);
	vector<ll>v;
	int i,j,mx;
	for (i=1;i<=n;i++)
	{
		sc(A[i]);
		mx=max(mx,A[i]);
		cm[i]=(ll)cm[i-1]+A[i];
	}
	ll x=1;
	if (abs(k)==1)
	{
		v.pb(1);
		if (k<0)
			v.pb(-1);

	}
	else
	while (1)
	{
		v.pb(x);
		x*=k;
		if (abs(x)>(1000000000000000LL))
			break;

	}
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	map<ll,int>ma;
	ma[0]=1;
	ll ans=0;
	map<ll,int>::iterator it;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<v.size();j++)
		{
			it=ma.find(cm[i]-v[j]);
			if (it!=ma.end())
				ans+=it->se;
		}
		ma[cm[i]]++;
	}
	printf("%lld\n",ans);

	return 0;

}

//01010110 01101100 01100001 01100100 00101110


#include<bits/stdc++.h>
using namespace std;
#define pc putchar_unlocked
#define gc getchar_unlocked
typedef long long ll;
typedef unsigned long long llu;
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scstr(x) scanf("%s",x)
#define pd(x) printf("%d",x)
#define pstr(x) printf("%s",x)
#define newl() printf("\n")
#define fl(i,n) for (i=0;i<n;i++)
#define fle(i,n) for (i=1;i<=n;i++)
#define fla(i,a,n) for (i=a;i<n;i++)
#define mem(a,i) memset(a,i,sizeof(a))
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define gcd __gcd
#define wl(n) while (n--)
#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) v.begin(),v.end()
#define tatt(cont) typeof(cont.begin())
const double eps=0.000000000000001 ;
#define MOD 1000000007
typedef long double LF;
typedef double lF;
#define hihihaha ((double)CLOCKS_PER_SEC)
#define nl putchar('\n')
//-------------
int n;
ll k;
int A[100010];
ll cm[100010];
int main()
{	//ios::sync_with_stdio(0), cin.tie(0);
	sc(n);scl(k);
	vector<ll>v;
	int i,j,mx;
	for (i=1;i<=n;i++)
	{
		sc(A[i]);
		mx=max(mx,A[i]);
		cm[i]=(ll)cm[i-1]+A[i];
	}
	ll x=1;
	while (1)
	{
		v.pb(x);
		x*=k;
		if (abs(x)>(1000000000000000LL))
			break;

	}
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	map<ll,int>ma;
	ma[0]=1;
	ll ans=0;
	map<ll,int>::iterator it;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<v.size();j++)
		{
			it=ma.find(cm[i]-v[j]);
			ans+=it->se;
		}
		ma[cm[i]]++;
	}
	printf("%lld",ans);

	return 0;

}

//01010110 01101100 01100001 01100100 00101110


#include<bits/stdc++.h>
using namespace std;
#define pc putchar_unlocked
#define gc getchar_unlocked
typedef long long ll;
typedef unsigned long long llu;
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scstr(x) scanf("%s",x)
#define pd(x) printf("%d",x)
#define pstr(x) printf("%s",x)
#define newl() printf("\n")
#define fl(i,n) for (i=0;i<n;i++)
#define fle(i,n) for (i=1;i<=n;i++)
#define fla(i,a,n) for (i=a;i<n;i++)
#define mem(a,i) memset(a,i,sizeof(a))
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define gcd __gcd
#define wl(n) while (n--)
#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) v.begin(),v.end()
#define tatt(cont) typeof(cont.begin())
const double eps=0.000000000000001 ;
#define MOD 1000000007
typedef long double LF;
typedef double lF;
#define hihihaha ((double)CLOCKS_PER_SEC)
#define nl putchar('\n')
//-------------
int n;
ll k;
int A[100010];
ll cm[100010];
int main()
{	//ios::sync_with_stdio(0), cin.tie(0);
	sc(n);scl(k);
	vector<ll>v;
	int i,j,mx;
	for (i=1;i<=n;i++)
	{
		sc(A[i]);
		mx=max(mx,A[i]);
		cm[i]=(ll)cm[i-1]+A[i];
	}
	ll x=1;
	while (1)
	{
		v.pb(x);
		x*=k;
		if (x>(1000000000000000LL))
			break;

	}
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	map<ll,int>ma;
	ma[0]=1;
	ll ans=0;
	map<ll,int>::iterator it;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<v.size();j++)
		{
			it=ma.find(cm[i]-v[j]);
			ans+=it->se;
		}
		ma[cm[i]]++;
	}
	printf("%lld",ans);

	return 0;

}

//01010110 01101100 01100001 01100100 00101110


#include<bits/stdc++.h>
using namespace std;
#define pc putchar_unlocked
#define gc getchar_unlocked
typedef long long ll;
typedef unsigned long long llu;
#define mp make_pair
#define pb push_back
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define scstr(x) scanf("%s",x)
#define pd(x) printf("%d",x)
#define pstr(x) printf("%s",x)
#define newl() printf("\n")
#define fl(i,n) for (i=0;i<n;i++)
#define fle(i,n) for (i=1;i<=n;i++)
#define fla(i,a,n) for (i=a;i<n;i++)
#define mem(a,i) memset(a,i,sizeof(a))
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<int,pair<int,int> > piii ;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
#define gcd __gcd
#define wl(n) while (n--)
#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define tr(container, it)  for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) v.begin(),v.end()
#define tatt(cont) typeof(cont.begin())
const double eps=0.000000000000001 ;
#define MOD 1000000007
typedef long double LF;
typedef double lF;
#define hihihaha ((double)CLOCKS_PER_SEC)
#define nl putchar('\n')
//-------------
int n;
ll k;
int A[100010];
ll cm[100010];
int main()
{	//ios::sync_with_stdio(0), cin.tie(0);
	sc(n);scl(k);
	vector<ll>v;
	int i,j,mx;
	for (i=1;i<=n;i++)
	{
		sc(A[i]);
		mx=max(mx,A[i]);
		cm[i]=(ll)cm[i-1]+A[i];
	}
	ll x=1;
	while (1)
	{
		v.pb(x);
		x*=k;
		if (x>(1000000000000000LL))
			break;

	}
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	map<ll,int>ma;
	ma[0]=1;
	ll ans=0;
	map<ll,int>::iterator it;
	for (i=1;i<=n;i++)
	{
		for (j=0;j<v.size();j++)
		{
			it=ma.find(cm[i]-v[j]);
			ans+=it->se;
		}
		ma[cm[i]]++;
	}
	printf("%lld\n",ans);

	return 0;

}

//01010110 01101100 01100001 01100100 00101110


#include<bits/stdc++.h>
using namespace std;
long long n,k,a,ta[1000005];
vector<long long>kp;
map<long long, vector<long long> >mps;

int main()
{
    cin>>n>>k;
    ta[0]=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        ta[i+1]=ta[i] + a;
    }

   // ta[1]=a[0];

    for(int i=0;i<=n;i++)
   {
       mps[ta[i]].push_back(i);
   }
   long long ans=0;
   if(k==1)
   {
      kp.push_back(1);
      //mps1[kp[0]]=1;
   }
   else if(k==-1)
   {
       kp.push_back(1);
       kp.push_back(-1);
      // mps1[kp[0]]=1;
       //mps1[kp[1]]=1;

   }
   else
   {
        kp.push_back(1);
    //    mps1[kp[0]]=1;
        for(int i=1;kp[i-1]< 2000000000000000000;i++)
        {
            kp.push_back(kp[i-1]*k);

           // cout<<kp[i]<<" kp"<<endl;

        }

   }
   for(int i=0;i<kp.size();i++)
   {
       for(int j=0;j<=n;j++)
       {
           long long x=kp[i]+ta[j];

           if(mps[x].size()>0)
           {

               long long ans1=upper_bound(mps[x].begin(),mps[x].end(),j)-mps[x].begin();
               int in=mps[x].size()-ans1;
              // cout<<kp[i] <<" "<<ta[j] <<" in "<<in<<endl;
               ans+=in;

           }

       }
   }

   cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
long long n,k,a[1000005],ta[1000005];
vector<long long>kp;
map<long long, vector<long long> >mps;

int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    ta[0]=0;
   // ta[1]=a[0];
    for(int i=1;i<=n;i++)
    {
        ta[i]=ta[i-1] + a[i-1];
    }
    for(int i=0;i<=n;i++)
   {
       mps[ta[i]].push_back(i);
   }
   long long ans=0;
   if(k==1)
   {
      kp.push_back(1);
      //mps1[kp[0]]=1;
   }
   else if(k==-1)
   {
       kp.push_back(1);
       kp.push_back(-1);
      // mps1[kp[0]]=1;
       //mps1[kp[1]]=1;

   }
   else
   {
        kp.push_back(1);
    //    mps1[kp[0]]=1;
        for(int i=1;kp[i-1]< 2000000000000000000;i++)
        {
            kp.push_back(kp[i-1]*k);

           // cout<<kp[i]<<" kp"<<endl;

        }

   }
   for(int i=0;i<kp.size();i++)
   {
       for(int j=0;j<=n;j++)
       {
           long long x=kp[i]+ta[j];

           if(mps[x].size()>0)
           {

               long long ans1=upper_bound(mps[x].begin(),mps[x].end(),j)-mps[x].begin();
               int in=mps[x].size()-ans1;
              // cout<<kp[i] <<" "<<ta[j] <<" in "<<in<<endl;
               ans+=in;

           }

       }
   }

   cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int n,k,a,ta[1000005];
vector<long long>kp;
map<long long, vector<int> >mps;

int main()
{
    cin>>n>>k;
    ta[0]=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        ta[i+1]=ta[i] + a;
    }

   // ta[1]=a[0];

    for(int i=0;i<=n;i++)
   {
       mps[ta[i]].push_back(i);
   }
   long long ans=0;
   if(k==1)
   {
      kp.push_back(1);
      //mps1[kp[0]]=1;
   }
   else if(k==-1)
   {
       kp.push_back(1);
       kp.push_back(-1);
      // mps1[kp[0]]=1;
       //mps1[kp[1]]=1;

   }
   else
   {
        kp.push_back(1);
    //    mps1[kp[0]]=1;
        for(int i=1;kp[i-1]< 2000000000000000000;i++)
        {
            kp.push_back(kp[i-1]*k);

           // cout<<kp[i]<<" kp"<<endl;

        }

   }
   for(int i=0;i<kp.size();i++)
   {
       for(int j=0;j<=n;j++)
       {
           long long x=kp[i]+ta[j];

           if(mps[x].size()>0)
           {

               long long ans1=upper_bound(mps[x].begin(),mps[x].end(),j)-mps[x].begin();
               int in=mps[x].size()-ans1;
              // cout<<kp[i] <<" "<<ta[j] <<" in "<<in<<endl;
               ans+=in;

           }

       }
   }

   cout<<ans<<endl;

}


#include <bits/stdc++.h>

#define ll long long

using namespace std;

map<ll, vector<ll> > mp;
ll n, k;
ll x[100010];
ll d[100010];
ll res = 0;

int main() {
    cin >> n >> k;
    x[0] = 0;//Petka ?
    d[0] = 0;
    for(int i = 1; i <= n; i++){
        cin >> x[i];
        d[i] = d[i-1] + x[i];
    }

//    for(int i = 0; i <= n; i++)
//        cout << d[i] << ' ';
//    cout << endl;

    for(ll i = 0; i <= n; i++){
        mp[d[i]].push_back(i);
        //cout<<"size "<<mp[2].size()<<endl;
    }

    int iter = 0;
    for(ll p = 1; p*k <= 2000000000000000000ll && p*k != 0; p *= k , iter++){
        //p is the current power
//        cout << "power is chosen " << p << endl;
        for(int i = 0; i <= n; i++){
            ll have = d[i];
            ll petka = have + p;
//            cout << "have " << have << endl;
//            cout << "petka " << petka << endl;
//            cout << "mp[petka]   ";
//            for(auto r : mp[petka]) cout << r << ' ';
//            cout << endl;
            if(mp[petka].size() != 0){

                    int index = upper_bound(mp[petka].begin(), mp[petka].end(), i) - mp[petka].begin();
//                    int ways = mp[petka].size() - greater + 1;
//                    cout << "index = " << index << endl;
                    int greaters = mp[petka].size() - index;
//                    cout << "-greaters " << greaters << endl;
                    res+= greaters;

            }
//            cout << "-----------\n";
        }
        if(k == 1)
            break;
        if(iter == 1 && k == -1)
            break;
    }
    cout << res << endl;
    return 0;
}
//    3 -6 -3 12
// 0  3 -3 -6  6


#include<bits/stdc++.h>
using namespace std;
long long n,k,cfv[1000005],kop;
map<long long,int>kmap;
int main()
{
    scanf("%I64d %I64d",&n,&k);
    kop=1;
    kmap[1]=1;
    for(int i=1;i<=63;i++)
    {
        kop=kop*k;
        kmap[kop]=1;
    }

    for(int i=0;i<n;i++)
    {
        scanf("%I64d",&cfv[i]);
    }
    int ans=0;
    for(int i=0;i<n;i++)
    {
        long long v=0;
        v=cfv[i];
        if(kmap[v]==1)
        {
            ans++;
        }
        for(int j=i+1;j<n;j++)
        {
            v=v+cfv[j];
            if(kmap[v]==1)
            {
                ans++;
            }

        }
    }
    cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int n,k,a,ta[1000005];
vector<long long>kp;
map<long long, vector<int> >mps;

int main()
{
    cin>>n>>k;
    ta[0]=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        ta[i+1]=ta[i] + a;
    }

   // ta[1]=a[0];

    for(int i=0;i<=n;i++)
   {
       mps[ta[i]].push_back(i);
   }
   long long ans=0;
   if(k==1)
   {
      kp.push_back(1);
      //mps1[kp[0]]=1;
   }
   else if(k==-1)
   {
       kp.push_back(1);
       kp.push_back(-1);
      // mps1[kp[0]]=1;
       //mps1[kp[1]]=1;

   }
   else
   {
        kp.push_back(1);
    //    mps1[kp[0]]=1;
        for(int i=1;kp[i-1]< 10000000000000000;i++)
        {
            kp.push_back(kp[i-1]*k);

           // cout<<kp[i]<<" kp"<<endl;

        }

   }
   for(int i=0;i<kp.size();i++)
   {
       for(int j=0;j<=n;j++)
       {
           long long x=kp[i]+ta[j];

           if(mps[x].size()>0)
           {

               long long ans1=upper_bound(mps[x].begin(),mps[x].end(),j)-mps[x].begin();
               int in=mps[x].size()-ans1;
              // cout<<kp[i] <<" "<<ta[j] <<" in "<<in<<endl;
               ans+=in;

           }

       }
   }

   cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
long long n,k,cfv[1000005],kop[1000];
map<long long,int>kmap;
int main()
{
    cin>>n>>k;
    kop[0]=1;
    kmap[1]=1;
    for(int i=1;i<=63;i++)
    {
        kop[i]=kop[i-1]*k;
        kmap[kop[i]]=1;
    }

    for(int i=0;i<n;i++)
    {
        cin>>cfv[i];
    }
    int ans=0;
    for(int i=0;i<n;i++)
    {
        long long v=0;
        v=cfv[i];
        if(kmap[v]==1)
        {
            ans++;
        }
        for(int j=i+1;j<n;j++)
        {
            v=v+cfv[j];
            if(kmap[v]==1)
            {
                ans++;
            }

        }
    }
    cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int n,k,a,ta[1000005];
vector<long long>kp;
map<long long, vector<int> >mps;

int main()
{
    cin>>n>>k;
    ta[0]=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        ta[i+1]=ta[i] + a;
    }

   // ta[1]=a[0];

    for(int i=0;i<=n;i++)
   {
       mps[ta[i]].push_back(i);
   }
   long long ans=0;
   if(k==1)
   {
      kp.push_back(1);
      //mps1[kp[0]]=1;
   }
   else if(k==-1)
   {
       kp.push_back(1);
       kp.push_back(-1);
      // mps1[kp[0]]=1;
       //mps1[kp[1]]=1;

   }
   else
   {
        kp.push_back(1);
    //    mps1[kp[0]]=1;
        for(int i=1;kp[i-1]< 1000000000000000;i++)
        {
            kp.push_back(kp[i-1]*k);

           // cout<<kp[i]<<" kp"<<endl;

        }

   }
   for(int i=0;i<kp.size();i++)
   {
       for(int j=0;j<=n;j++)
       {
           long long x=kp[i]+ta[j];

           if(mps[x].size()>0)
           {

               long long ans1=upper_bound(mps[x].begin(),mps[x].end(),j)-mps[x].begin();
               int in=mps[x].size()-ans1;
              // cout<<kp[i] <<" "<<ta[j] <<" in "<<in<<endl;
               ans+=in;

           }

       }
   }

   cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <cmath>
#include <climits>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()

vvvl G;
int N;
ll S, oneSum, twoSum;
priority_queue<vector<ll>> app;

int dfs(int S, int par) {
	if (G[S].size() == 1 && par != -1) return 1;
	vl curP;
	int v, ans = 0, d;
	for (int i = 0; i < (int) G[S].size(); i++) {
		curP = G[S][i];
		v = curP[0];
		if (v == par) continue;
		d = dfs(v, S);
		app.push({curP[1] * ((ll) d) - ((curP[1]/2) * ((ll) d)), curP[1] * ((ll) d), d, curP[2]});
		if (curP[2] == 1) oneSum += curP[1] * ((ll) d);
		else twoSum += curP[1] * ((ll) d);
		ans += d;
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int T, a, b, c, d;
	ll diff, fullCost, e, newCost;
	cin >> T;
	vl curP, ones, twos;
	while(T--) {
		cin >> N >> S;
		G.assign(N, vvl());
		for (int i = 0; i < N-1; i++) {
			cin >> a >> b >> c >> d;
			a--;
			b--;
			G[a].push_back({b, c, d});
			G[b].push_back({a, c, d});
		}
		while(!app.empty()) app.pop();
		oneSum = twoSum = 0ll;
		dfs(0, -1);
		ones.clear();
		twos.clear();
		ones.push_back(oneSum);
		twos.push_back(twoSum);
		while(oneSum || twoSum) {
			curP = app.top();
			app.pop();
			diff = curP[0];
			fullCost = curP[1];
			e = curP[2];
			if (curP[3] == 1) oneSum -= diff;
			else twoSum -= diff;
			newCost = fullCost - diff;
			app.push({newCost - ((newCost/e)/2)*e, newCost, e, curP[3]});
			if (curP[3] == 1) ones.push_back(oneSum);
			else twos.push_back(twoSum);
		}
		c = ones.size() + 2*twos.size();
		int j = twos.size()-1;
		for (int i = 0; i < (int) ones.size(); i++) {
			while(j > 0 && ones[i] + twos[j-1] <= S) j--;
			if (ones[i] + twos[j] <= S) {
				c = min(c, i + 2*j);
			}
		}
		cout << c << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <cmath>
#include <climits>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()

vvvl G;
int N;
ll S, oneSum, twoSum;
priority_queue<vector<ll>> app;

int dfs(int S, int par) {
	if (G[S].size() == 1 && par != -1) return 1;
	vl curP;
	int v, ans = 0, d;
	for (int i = 0; i < (int) G[S].size(); i++) {
		curP = G[S][i];
		v = curP[0];
		if (v == par) continue;
		d = dfs(v, S);
		app.push({curP[1] * ((ll) d) - ((curP[1]/2) * ((ll) d)), curP[1] * ((ll) d), d, curP[2]});
		if (curP[2] == 1) oneSum += curP[1] * ((ll) d);
		else twoSum += curP[1] * ((ll) d);
		ans += d;
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int T, a, b, c, d;
	ll diff, fullCost, e, newCost;
	cin >> T;
	vl curP, ones, twos;
	while(T--) {
		cin >> N >> S;
		G.assign(N, vvl());
		for (int i = 0; i < N-1; i++) {
			cin >> a >> b >> c >> d;
			a--;
			b--;
			G[a].push_back({b, c, d});
			G[b].push_back({a, c, d});
		}
		while(!app.empty()) app.pop();
		oneSum = twoSum = 0ll;
		dfs(0, -1);
		ones.clear();
		twos.clear();
		ones.push_back(oneSum);
		twos.push_back(twoSum);
		while(oneSum || twoSum) {
			curP = app.top();
			app.pop();
			diff = curP[0];
			fullCost = curP[1];
			e = curP[2];
			if (curP[3] == 1) oneSum -= diff;
			else twoSum -= diff;
			newCost = fullCost - diff;
			app.push({newCost - ((newCost/e)/2)*e, newCost, e, curP[3]});
			if (curP[3] == 1) ones.push_back(oneSum);
			else twos.push_back(twoSum);
		}
		c = ones.size() + 2*twos.size();
		int j = twos.size()-1;
		for (int i = 0; i < (int) ones.size(); i++) {
			while(j > 0 && ones[i] + twos[j] <= S) j--;
			if (ones[i] + twos[j] <= S) {
				c = min(c, i + 2*j);
			}
		}
		cout << c << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <cmath>
#include <climits>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()

vvvl G;
int N;
ll S, oneSum, twoSum;
priority_queue<vector<ll>> app;

int dfs(int S, int par) {
	if (G[S].size() == 1 && par != -1) return 1;
	vl curP;
	int v, ans = 0, d;
	for (int i = 0; i < (int) G[S].size(); i++) {
		curP = G[S][i];
		v = curP[0];
		if (v == par) continue;
		d = dfs(v, S);
		app.push({curP[1] * ((ll) d) - ((curP[1]/2) * ((ll) d)), curP[1] * ((ll) d), d, curP[2]});
		if (curP[2] == 1) oneSum += curP[1] * ((ll) d);
		else twoSum += curP[1] * ((ll) d);
		ans += d;
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int T, a, b, c, d;
	ll diff, fullCost, e, newCost;
	cin >> T;
	vl curP, ones, twos;
	while(T--) {
		cin >> N >> S;
		G.assign(N, vvl());
		for (int i = 0; i < N-1; i++) {
			cin >> a >> b >> c >> d;
			a--;
			b--;
			G[a].push_back({b, c, d});
			G[b].push_back({a, c, d});
		}
		while(!app.empty()) app.pop();
		oneSum = twoSum = 0ll;
		dfs(0, -1);
		ones.clear();
		twos.clear();
		ones.push_back(oneSum);
		twos.push_back(twoSum);
		while(oneSum || twoSum) {
			curP = app.top();
			app.pop();
			diff = curP[0];
			fullCost = curP[1];
			e = curP[2];
			if (curP[3] == 1) oneSum -= diff;
			else twoSum -= diff;
			newCost = fullCost - diff;
			app.push({newCost - ((newCost/e)/2)*e, newCost, e, curP[3]});
			if (curP[3] == 1) ones.push_back(oneSum);
			else twos.push_back(twoSum);
		}
		c = ones.size() + 2*twos.size();
		int j = twos.size()-1;
		for (int i = 0; i < (int) ones.size(); i++) {
			while(j > 0 && ones[i] + twos[j] <= S) j--;
			if (ones[i] + twos[j] <= S) {
				c = min(c, i + 2*j);
				break;
			}
		}
		cout << c << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <cmath>
#include <climits>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()

vvvl G;
int N;
ll S, oneSum, twoSum;
priority_queue<vector<ll>> app;

int dfs(int S, int par) {
	if (G[S].size() == 1 && par != -1) return 1;
	vl curP;
	int v, ans = 0, d;
	for (int i = 0; i < (int) G[S].size(); i++) {
		curP = G[S][i];
		v = curP[0];
		if (v == par) continue;
		d = dfs(v, S);
		app.push({curP[1] * ((ll) d) - ((curP[1]/2) * ((ll) d)), curP[1] * ((ll) d), d, curP[2]});
		if (curP[2] == 1) oneSum += curP[1] * ((ll) d);
		else twoSum += curP[1] * ((ll) d);
		ans += d;
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int T, a, b, c, d;
	ll diff, fullCost, e, newCost;
	cin >> T;
	vl curP, ones, twos;
	while(T--) {
		cin >> N >> S;
		G.assign(N, vvl());
		for (int i = 0; i < N-1; i++) {
			cin >> a >> b >> c >> d;
			a--;
			b--;
			G[a].push_back({b, c, d});
			G[b].push_back({a, c, d});
		}
		while(!app.empty()) app.pop();
		oneSum = twoSum = 0ll;
		dfs(0, -1);
		ones.clear();
		twos.clear();
		ones.push_back(oneSum);
		twos.push_back(twoSum);
		while(oneSum || twoSum) {
			curP = app.top();
			app.pop();
			diff = curP[0];
			fullCost = curP[1];
			e = curP[2];
			if (curP[3] == 1) oneSum -= diff;
			else twoSum -= diff;
			newCost = fullCost - diff;
			app.push({newCost - ((newCost/e)/2)*e, newCost, e, curP[3]});
			if (curP[3] == 1) ones.push_back(oneSum);
			else twos.push_back(twoSum);
		}
		c = ones.size() + 2*twos.size();
		int j = twos.size()-1;
		for (int i = 0; i < (int) ones.size(); i++) {
			while(j >= 0 && ones[i] + twos[j] <= S) j--;
			if (ones[i] + twos[j] <= S) {
				c = min(c, i + 2*j);
			}
		}
		cout << c << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <cmath>
#include <climits>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define all(v) v.begin(), v.end()

vvvl G;
int N;
ll S, oneSum, twoSum;
priority_queue<vector<ll>> app;

int dfs(int S, int par) {
	if (G[S].size() == 1 && par != -1) return 1;
	vl curP;
	int v, ans = 0, d;
	for (int i = 0; i < (int) G[S].size(); i++) {
		curP = G[S][i];
		v = curP[0];
		if (v == par) continue;
		d = dfs(v, S);
		app.push({curP[1] * ((ll) d) - ((curP[1]/2) * ((ll) d)), curP[1] * ((ll) d), d, curP[2]});
		if (curP[2] == 1) oneSum += curP[1] * ((ll) d);
		else twoSum += curP[1] * ((ll) d);
		ans += d;
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int T, a, b, c, d;
	ll diff, fullCost, e, newCost;
	cin >> T;
	vl curP, ones, twos;
	while(T--) {
		cin >> N >> S;
		G.assign(N, vvl());
		for (int i = 0; i < N-1; i++) {
			cin >> a >> b >> c >> d;
			a--;
			b--;
			G[a].push_back({b, c, d});
			G[b].push_back({a, c, d});
		}
		while(!app.empty()) app.pop();
		oneSum = twoSum = 0ll;
		dfs(0, -1);
		ones.clear();
		twos.clear();
		ones.push_back(oneSum);
		twos.push_back(twoSum);
		while(oneSum || twoSum) {
			curP = app.top();
			app.pop();
			diff = curP[0];
			fullCost = curP[1];
			e = curP[2];
			if (curP[3] == 1) oneSum -= diff;
			else twoSum -= diff;
			newCost = fullCost - diff;
			app.push({newCost - ((newCost/e)/2)*e, newCost, e, curP[3]});
			if (curP[3] == 1) ones.push_back(oneSum);
			else twos.push_back(twoSum);
		}
		c = ones.size() + 2*twos.size();
		for (int i = 0; i < (int) ones.size(); i++) {
			for (int j = 0; j < (int) twos.size(); j++) {
				if (ones[i] + twos[j] <= S) {
					c = min(c, i + 2*j);
					break;
				}
			}
		}
		cout << c << "\n";
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const ll N = 2e6 + 5, M = 2e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
ll _, n, s, idx;
ll e[M], ne[M], h[N], w1[M], w2[M];
ll leaf[N];
vector<ll>vec[3];
ll arr[3][N];
int cnt1, cnt2;

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void init() {
    cnt1 = cnt2 = idx = 0;
    for (int i = 1; i < min(N, n + n); ++i) {
        h[i] = -1;
        leaf[i] = 0;
    }
    vec[1].clear();
    vec[2].clear();
}

struct node {
    ll u, v, w, c, num;
    double res;
    bool operator<(const node &x)const {
        return res < x.res;
    }
}E[M];

void add(ll a, ll b, ll w, ll c) {
    e[idx] = b;
    w1[idx] = w;
    w2[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

void dfs(ll u, ll fa) {
    ll cnt = 0, ans = 0;
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (v == fa) continue;
        dfs(v, u);
        cnt++;
        ans += leaf[v];
    }
    if (!cnt) leaf[u] = 1;
    else leaf[u] = ans;
}

bool cmp(ll x, ll y) {
    return x > y;
}

int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    _ = read();
    while (_--) {
        n = read(); s = read();
        init();
        for (int i = 1; i < n; ++i) {
            ll u, v, w, c;
            u = read(); v = read(); w = read(); c = read();
            E[i] = {u, v, w, c};
            add(u, v, w, c);
            add(v, u, w, c);
        }
        dfs(1, -1);
        ll nn = 0;
        for (int i = 1; i < n; ++i) {
            int a = E[i].u, b = E[i].v;
            E[i].num = min(leaf[a], leaf[b]);
            nn += E[i].num * E[i].w;
            ll now = E[i].w;
            int tmp = E[i].c;
            ll qq = E[i].num;
            while (now) {
                 //printf("now = %d push = %d\n", now, (now - now / 2) * qq);
                vec[tmp].push_back((now - now / 2) * qq);
                now /= 2;
            }
        }

        sort(vec[1].begin(), vec[1].end(), cmp);
        sort(vec[2].begin(), vec[2].end(), cmp);

        //for (int i = 0; i < vec[1].size(); ++i)
            //cout << vec[1][i] << '\n';

        for (int i = 0; i < vec[1].size(); ++i)
            arr[1][cnt1 + 1] = arr[1][cnt1] + vec[1][i], cnt1++;

        for (int i = 0; i < vec[2].size(); ++i)
            arr[2][cnt2 + 1] = arr[2][cnt2] + vec[2][i], cnt2++;

        ll ans = 0, Min = 1e18;
        s = nn - s;
        if (cnt2 == 0)
            Min = lower_bound(arr[1], arr[1] + cnt1 + 1, s) - arr[1];
        else
        for (int i = 0; i <= cnt1; ++i) {
            ll tmp = s - arr[1][i];
            if (arr[2][cnt2] < tmp)
                continue;
            ans = i;
            int pos = lower_bound(arr[2], arr[2] + 1 + cnt2, tmp) - arr[2];
            ans += pos * 2;
            Min = min(Min, ans);
        }
        cout << Min << '\n';
    }
    #ifndef ONLINE_JUDGE
        system("pause");
    #endif
}

/*
2
10 28
8 2 8 1
5 1 4 1
6 1 10 1
10 2 7 1
7 2 1 1
9 2 1 1
2 1 5 1
4 1 9 1
3 2 5 1

7
*/


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const ll N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
ll _, n, s, idx;
ll e[M], ne[M], h[N], w1[M], w2[M];
ll leaf[N];
vector<ll>vec[3];
ll arr[3][N];
int cnt1, cnt2;

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void init() {
    cnt1 = cnt2 = idx = 0;
    for (int i = 1; i < min(N, n + n); ++i) {
        h[i] = -1;
        leaf[i] = 0;
    }
    vec[1].clear();
    vec[2].clear();
}

struct node {
    ll u, v, w, c, num;
    double res;
    bool operator<(const node &x)const {
        return res < x.res;
    }
}E[M];

void add(ll a, ll b, ll w, ll c) {
    e[idx] = b;
    w1[idx] = w;
    w2[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

void dfs(ll u, ll fa) {
    ll cnt = 0, ans = 0;
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (v == fa) continue;
        dfs(v, u);
        cnt++;
        ans += leaf[v];
    }
    if (!cnt) leaf[u] = 1;
    else leaf[u] = ans;
}

bool cmp(ll x, ll y) {
    return x > y;
}

int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    _ = read();
    while (_--) {
        n = read(); s = read();
        init();
        for (int i = 1; i < n; ++i) {
            ll u, v, w, c;
            u = read(); v = read(); w = read(); c = read();
            E[i] = {u, v, w, c};
            add(u, v, w, c);
            add(v, u, w, c);
        }
        dfs(1, -1);
        ll nn = 0;
        for (int i = 1; i < n; ++i) {
            int a = E[i].u, b = E[i].v;
            E[i].num = min(leaf[a], leaf[b]);
            nn += E[i].num * E[i].w;
            ll now = E[i].w;
            int tmp = E[i].c;
            ll qq = E[i].num;
            while (now) {
                 //printf("now = %d push = %d\n", now, (now - now / 2) * qq);
                vec[tmp].push_back((now - now / 2) * qq);
                now /= 2;
            }
        }

        sort(vec[1].begin(), vec[1].end(), cmp);
        sort(vec[2].begin(), vec[2].end(), cmp);

        //for (int i = 0; i < vec[1].size(); ++i)
            //cout << vec[1][i] << '\n';

        for (int i = 0; i < vec[1].size(); ++i)
            arr[1][cnt1 + 1] = arr[1][cnt1] + vec[1][i], cnt1++;

        for (int i = 0; i < vec[2].size(); ++i)
            arr[2][cnt2 + 1] = arr[2][cnt2] + vec[2][i], cnt2++;

        ll ans = 0, Min = 1e18;
        s = nn - s;
        if (cnt2 == 0)
            Min = lower_bound(arr[1], arr[1] + cnt1 + 1, s) - arr[1];
        else
        for (int i = 0; i <= cnt1; ++i) {
            ll tmp = s - arr[1][i];
            if (arr[2][cnt2] < tmp)
                continue;
            ans = i;
            int pos = lower_bound(arr[2], arr[2] + 1 + cnt2, tmp) - arr[2];
            ans += pos * 2;
            Min = min(Min, ans);
        }
        cout << Min << '\n';
    }
    #ifndef ONLINE_JUDGE
        system("pause");
    #endif
}

/*
2
10 28
8 2 8 1
5 1 4 1
6 1 10 1
10 2 7 1
7 2 1 1
9 2 1 1
2 1 5 1
4 1 9 1
3 2 5 1

7
*/


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const ll N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
ll _, n, s, idx;
ll e[M], ne[M], h[N], w1[M], w2[M];
ll leaf[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void init() {
    idx = 0;
    for (int i = 1; i < min(N, n + n); ++i) h[i] = -1;
}

struct node {
    int u, v, w, c, num;
    double res;
    bool operator<(const node &x)const {
        return res < x.res;
    }
}E[M];

void add(ll a, ll b, ll w, ll c) {
    e[idx] = b;
    w1[idx] = w;
    w2[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

void dfs(ll u, ll fa) {
    ll cnt = 0, ans = 0;
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (v == fa) continue;
        dfs(v, u);
        cnt++;
        ans += leaf[v];
    }
    if (!cnt) leaf[u] = 1;
    else leaf[u] = ans;
}

int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    _ = read();
    while (_--) {
        n = read(); s = read();
        init();
        for (int i = 1; i < n; ++i) {
            ll u, v, w, c;
            u = read(); v = read(); w = read(); c = read();
            E[i] = {u, v, w, c};
            add(u, v, w, c);
            add(v, u, w, c);
        }
        dfs(1, -1);
        ll now = 0;
        priority_queue<node>q;
        for (int i = 1; i < n; ++i) {
            int a = E[i].u, b = E[i].v;
            E[i].num = min(leaf[a], leaf[b]);
            now += E[i].num * E[i].w;
            E[i].res = 1.0 * (E[i].w - E[i].w / 2) * E[i].num / E[i].c;
            q.push(E[i]);
        }

        // while (!q.empty()) {
        //     node t = q.top();
        //     q.pop();
        //     printf("%d %d %d %d %d %lf\n", t.u, t.v, t.w, t.c, t.num, t.res);
        // }
        // continue;

        ll ans = 0;
        // cout << now << '\n';
        while (now > s) {
            node tt, t;
            t = q.top();
            // printf("%d %d %d %d %d %lf\n", t.u, t.v, t.w, t.c, t.num, t.res);
            tt = t;
            q.pop();
            ans += t.c;
            now -= t.num * (t.w - t.w / 2);
            tt.w = t.w / 2;
            tt.res = 1.0 * (tt.w - tt.w / 2) * tt.num / tt.c;
            q.push(tt);
        }
        // cout << now << '\n';
        cout << ans << '\n';
    }
    #ifndef ONLINE_JUDGE
        system("pause");
    #endif
}

/*
2
10 28
8 2 8 1
5 1 4 1
6 1 10 1
10 2 7 1
7 2 1 1
9 2 1 1
2 1 5 1
4 1 9 1
3 2 5 1

7
*/


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const ll N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
ll _, n, s, idx;
ll e[M], ne[M], h[N], w1[M], w2[M];
ll leaf[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void init() {
    idx = 0;
    for (int i = 1; i < min(N, n + n); ++i) h[i] = -1;
}

struct node {
    int u, v, w, c, num;
    double res;
    bool operator<(const node &x)const {
        return res < x.res;
    }
}E[M];

void add(ll a, ll b, ll w, ll c) {
    e[idx] = b;
    w1[idx] = w;
    w2[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

void dfs(ll u, ll fa) {
    ll cnt = 0, ans = 0;
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (v == fa) continue;
        dfs(v, u);
        cnt++;
        ans += leaf[v];
    }
    if (!cnt) leaf[u] = 1;
    else leaf[u] = ans;
}

int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    _ = read();
    while (_--) {
        n = read(); s = read();
        init();
        for (int i = 1; i < n; ++i) {
            ll u, v, w, c;
            u = read(); v = read(); w = read(); c = read();
            E[i] = {u, v, w, c};
            add(u, v, w, c);
            add(v, u, w, c);
        }
        dfs(1, -1);
        ll now = 0;
        priority_queue<node>q;
        for (int i = 1; i < n; ++i) {
            int a = E[i].u, b = E[i].v;
            E[i].num = min(leaf[a], leaf[b]);
            now += E[i].num * E[i].w;
            E[i].res = 1.0 * (E[i].w - E[i].w / 2) * E[i].num / E[i].c;
            q.push(E[i]);
        }

        // while (!q.empty()) {
        //     node t = q.top();
        //     q.pop();
        //     printf("%d %d %d %d %d %lf\n", t.u, t.v, t.w, t.c, t.num, t.res);
        // }
        // continue;

        ll ans = 0;
        // cout << now << '\n';
        while (now > s) {
            node tt, t;
            t = q.top();
            if (t.w == 1) continue;
            // printf("%d %d %d %d %d %lf\n", t.u, t.v, t.w, t.c, t.num, t.res);
            tt = t;
            q.pop();
            ans += t.c;
            now -= t.num * (t.w - t.w / 2);
            tt.w = t.w / 2;
            tt.res = 1.0 * (tt.w - tt.w / 2) * tt.num / tt.c;
            q.push(tt);
        }
        // cout << now << '\n';
        cout << ans << '\n';
    }
    #ifndef ONLINE_JUDGE
        system("pause");
    #endif
}

/*
2
10 28
8 2 8 1
5 1 4 1
6 1 10 1
10 2 7 1
7 2 1 1
9 2 1 1
2 1 5 1
4 1 9 1
3 2 5 1

7
*/