#include<bits/stdc++.h>
using namespace std;
int n,red,blue,ans;
string a;
int main()
{
    scanf("%d",&n);
    cin>>a;
    for(int i=0;i<n;i++)
    {
        if(a[i]=='(')
        {
        if(red<blue)
        red++,ans=max(red,ans);
        else blue++,ans=max(blue,ans);
        }
        else
        {
            if(blue>red)blue--;
            else red--;
        }
    }
    printf("%d",ans);
}


#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
template <typename T>
inline void read(T& x) {
    T res = 0, flag = 1;
    char ch;
    ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') flag = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        res = res * 10 + ch - 48;
        ch = getchar();
    }
    x = res * flag;
}
int main() {
    int n, depth;
    std::string s;
    std::ios::sync_with_stdio(false);
    std::cin >> n >> s;
    std::string t(n, '0');
    for(int i = 0; i < n; ++i) {
        if(s[i] == ')')
            --depth;
        t[i] += depth & 1;
        if(s[i] == '(')
            ++depth;
    }
    std::cout << t << '\n';
}


#include <iostream>
#include <stack>

using namespace std;

int _long;

stack<char> _red;
stack<char> _blue;
int _res[200009];

void ParseIn () {
	cin >> _long;
}

void Core () {

	char curCha;

	for (int i = 0; i < _long; i ++) {
		cin >> curCha;

		if (curCha == '(' ) {
			if (_red.size() > _blue.size()) {//   blue -> 1, red -> 0
				_blue.push(curCha);
				_res[i] = 1;
			}
			else {
				_red.push(curCha);
				_res[i] = 0;
			}
		}
		else {
			if (_red.size() > _blue.size()) {
				_red.pop();
				_res[i] = 0;
			}
			else {
				_blue.pop();
				_res[i] = 1;
			}
		}
	}
}

void CWriteOut () {
	for (int i = 0; i < _long; i ++) {
		cout << _res[i];
	}
}

int main () {

	ParseIn ();
	Core ();
	CWriteOut ();

	return 0;
}


//左 -> 深度不深

#include <iostream>
#include <queue>

using namespace std;

int _numN;
string _myStr;
int _sZhi , _qZhi;
int _resList[10000003];

void ParseIn () {

	cin >> _numN;
	cin >> _myStr;
}

void Core () {

	for (int i = 0; i < _numN; i ++) {

		if (_myStr[i] == '(') {

			if (_sZhi > _qZhi) {

				_qZhi ++;

				_resList[i] = 0;
			}

			else {

				_sZhi ++;

				_resList[i] = 1;
			}
		}

		else {

			if (_sZhi < _qZhi) {

				_qZhi --;

				_resList[i] = 0;
			}

			else {

				_sZhi --;

				_resList[i] = 1;
			}
		}
	}

}

void CWriteout () {

	for (int i = 0; i < _numN; i ++) {

		cout << _resList[i];
	}
}

int main () {

	ParseIn ();
	Core ();
	CWriteout ();

	return 0;
}


#include <iostream>
#include <stack>

using namespace std;

int _n;
string _str;
stack<char> _s0, _s1;
int _ans[200009];

void ParseIn () {
	cin >> _n >> _str;
}

void Core () {
	char curCh;

	for (int i = 0; i < _n; i++) {
		curCh = _str[i];

		if (curCh == '(') {
			if (_s0.size() < _s1.size()) {
				_ans[i] = 0;
				_s0.push('(');
			}

			else {
				_ans[i] = 1;
				_s1.push('(');
			}
		}

		else if (curCh == ')') {
			if (_s0.size() < _s1.size()) {
				_ans[i] = 1;
				_s1.pop();
			}

			else {
				_ans[i] = 0;
				_s0.pop();
			}
		}
	}
}

void WriteOut () {
	for (int i = 0; i < _n; i++) {
		cout << _ans[i];
	}

	cout << endl;
}

int main () {
	ParseIn();
	Core();
	WriteOut();

	return 0;
}


#include<bits/stdc++.h>
//#define TIME
using namespace std;
typedef long long ll;
const int N=100005;
const ll inf=0x3f3f3f3f;
const int mod=998244353;
int n,m;
string s;
int cnt1,cnt2;
int main(){
	ios::sync_with_stdio(0);
	int i,j;
	cin>>n;
	cin>>s;
	s=" "+s;
	for(i=1;i<=n;i++){
		if(s[i]=='('){
			if(cnt1<cnt2){
				cout<<0;
				cnt1++;
			}
			else{
				cout<<1;
				cnt2++;
			}
		}
		else{
			if(cnt1<cnt2){
				cout<<1;
				cnt2--;
			}
			else{
				cout<<0;
				cnt1--;
			}
		}
	}

	return 0;
}


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<deque>
#include<bitset>
#define ls 2*tr
#define rs 2*tr+1
#define ll  long long
#define pb push_back
#define PI (acos(-1))
#define fi first
#define sc second
#define INF 0x3f3f3f3f
using namespace std;

int dep[200200];
vector<int> v;
stack<char> st;
int ans[202002];
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    dep[0]=1;
    v.pb(0);
    st.push('(');
    for(int i=1;i<s.size();i++)
    {
        if(st.top()==s[i])
        {
            dep[i]=dep[v.back()]+1;
            st.push(s[i]);
            v.pb(i);
        }
        else
        {
            dep[i]=dep[v.back()];
            v.pop_back();
            st.pop();
        }
    }
    for(int i=0;i<s.size();i++)
    {
        if(dep[i]&1) ans[i]=0;
        else ans[i]=1;
    }
    for(int i=0;i<s.size();i++) cout<<ans[i];
}


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<deque>
#include<bitset>
#define ls 2*tr
#define rs 2*tr+1
#define ll  long long
#define pb push_back
#define PI (acos(-1))
#define fi first
#define sc second
#define INF 0x3f3f3f3f
using namespace std;

int dep[200200];
vector<int> v;
stack<char> st;
int ans[202002];
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(st.empty())
        {
            dep[i]=1;
            v.pb(i);
            st.push('(');
        }
        else if(st.top()==s[i])
        {
            dep[i]=dep[v.back()]+1;
            st.push(s[i]);
            v.pb(i);
        }
        else
        {
            dep[i]=dep[v.back()];
            v.pop_back();
            st.pop();
        }
    }
    for(int i=0;i<s.size();i++)
    {
        if(dep[i]&1) ans[i]=0;
        else ans[i]=1;
    }
    for(int i=0;i<s.size();i++) cout<<ans[i];
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 5e5 + 7;

char ans[N];

int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n; cin >> n; ans[n] = '\0';
    string s; cin >> s;
    int cnt = 0, mx = -1;
    for(int i = 0; i < n; i++) {
        if(s[i] == '(') cnt++;
        else cnt--;
        mx = max(mx, cnt);
    }
    vector<int> pos;
    cnt = 0; int f = 0;
    for(int i = 0; i < n; i++) {
        if(s[i] == '(') {
            cnt++; pos.push_back(i);
        } else if(s[i] == ')') {
            cnt--;
            if(f) ans[pos.back()] = ans[i] = '1', f--;
            else ans[pos.back()] = ans[i] = '0';
            pos.pop_back();
        }
        if(cnt == mx) f = mx / 2;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 5e5 + 7;

char ans[N];

int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n; cin >> n; ans[n] = '\0';
    string s; cin >> s;
    int cnt = 0, mx = -1;
    for(int i = 0; i < n; i++) {
        if(s[i] == '(') cnt++;
        else cnt--;
        mx = max(mx, cnt);
    }
    vector<int> pos;
    cnt = 0; int f = 0;
    for(int i = 0; i < n; i++) {
        if(s[i] == '(') {
            cnt++; pos.push_back(i);
        } else if(s[i] == ')') {
            if(cnt >= (mx + 1) / 2)
                ans[pos.back()] = ans[i] = '1', f--;
            else ans[pos.back()] = ans[i] = '0';
            pos.pop_back();
            cnt--;
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 5e5 + 7;

char ans[N];

int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n; cin >> n; ans[n] = '\0';
    string s; cin >> s;
    int cnt = 0, mx = -1;
    for(int i = 0; i < n; i++) {
        if(s[i] == '(') cnt++;
        else cnt--;
        mx = max(mx, cnt);
    }
    vector<int> pos;
    cnt = 0; int f = 0;
    for(int i = 0; i < n; i++) {
        if(s[i] == '(') {
            cnt++; pos.push_back(i);
        } else if(s[i] == ')') {
            if(cnt >= mx / 2 + 1)
                ans[pos.back()] = ans[i] = '1', f--;
            else ans[pos.back()] = ans[i] = '0';
            pos.pop_back();
            cnt--;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define read(i) scanf("%d",&i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=2e6+5;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>void gmod(T &a,T b){a=((a+b)%mod+mod)%mod;}

ll gcd(ll a,ll b){ while(b) b^=a^=b^=a%=b; return a;}
ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll qpow(ll n,ll m)
{
    n%=mod;
    ll ans=1;
    for(;m;m>>=1)
    {
        if(m&1)ans=ans*n%mod;
        n=n*n%mod;
    }
    return ans;
}

struct node{
    int t,next;
    ll len;
}edge[MAX<<1];
int head[MAX],cnt;
void initedge(int n)
{
    memset(head,-1,sizeof(head[0])*(n+1));cnt=0;
}
void addedge(int u,int v,ll len=0)
{
    edge[cnt]=node{v,head[u],len};
    head[u]=cnt++;
}

int n,depth;
char str[MAX];
int st[MAX];

void dfs(int u,int dep=0)
{
    int top=0;
    for(int i=head[u];~i;i=edge[i].next)st[top++]=edge[i].t;
    for(int i=top-1;i>=0;i--)
    {
        printf("%d",dep>=depth/2);
        dfs(st[i],dep+1);
        printf("%d",dep>=depth/2);
    }
}
int main()
{
    cin>>n;
    scanf("%s",str);
    initedge(1010100);
    st[0]=0;
    int top=0,nodeCnt=0;
    depth=0;
    for(int i=0;str[i];i++)
    {
        if(str[i]=='(')
        {
            st[++top]=++nodeCnt;
            depth=max(depth,top);
            addedge(st[top-1],st[top]);
        }
        else
        {
            top--;
        }
    }
   // cout<<depth<<endl;
    dfs(0);
}


#include <bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define read(i) scanf("%d",&i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=2e6+5;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>void gmod(T &a,T b){a=((a+b)%mod+mod)%mod;}

ll gcd(ll a,ll b){ while(b) b^=a^=b^=a%=b; return a;}
ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll qpow(ll n,ll m)
{
    n%=mod;
    ll ans=1;
    for(;m;m>>=1)
    {
        if(m&1)ans=ans*n%mod;
        n=n*n%mod;
    }
    return ans;
}

struct node{
    int t,next;
    ll len;
}edge[MAX<<1];
int head[MAX],cnt;
void initedge(int n)
{
    memset(head,-1,sizeof(head[0])*(n+1));cnt=0;
}
void addedge(int u,int v,ll len=0)
{
    edge[cnt]=node{v,head[u],len};
    head[u]=cnt++;
}

int n,depth;
char str[MAX];
int st[MAX];

void dfs(int u,int dep=0)
{
    int top=0;
    for(int i=head[u];~i;i=edge[i].next)st[top++]=edge[i].t;
    for(int i=top-1;i>=0;i--)
    {
        printf("%d",dep>=depth/2);
        dfs(st[i],dep+1);
        printf("%d",dep>=depth/2);
    }
}
int main()
{
    cin>>n;
    scanf("%s",str);
    initedge(1010100);
    st[0]=0;
    int top=0,nodeCnt=0;
    depth=0;
    for(int i=0;str[i];i++)
    {
        if(str[i]=='(')
        {
            st[++top]=++nodeCnt;
            depth=max(depth,top);
            addedge(st[top-1],st[top]);
        }
        else
        {
            top--;
        }
    }
    cout<<depth<<endl;
    dfs(0);
}


#include <bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define read(i) scanf("%d",&i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=2e6+5;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>void gmod(T &a,T b){a=((a+b)%mod+mod)%mod;}

ll gcd(ll a,ll b){ while(b) b^=a^=b^=a%=b; return a;}
ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll qpow(ll n,ll m)
{
    n%=mod;
    ll ans=1;
    for(;m;m>>=1)
    {
        if(m&1)ans=ans*n%mod;
        n=n*n%mod;
    }
    return ans;
}

struct node{
    int t,next;
    ll len;
}edge[MAX<<1];
int head[MAX],cnt;
void initedge(int n)
{
    memset(head,-1,sizeof(head[0])*(n+1));cnt=0;
}
void addedge(int u,int v,ll len=0)
{
    edge[cnt]=node{v,head[u],len};
    head[u]=cnt++;
}

int n,depth;
char str[MAX];
int st[MAX];

void dfs(int u,int dep=0)
{
    int top=0;
    for(int i=head[u];~i;i=edge[i].next)st[top++]=edge[i].t;
    for(int i=top-1;i>=0;i--)
    {
        printf("%d",dep<=depth/2);
        dfs(st[i],dep+1);
        printf("%d",dep<=depth/2);
    }
}
int main()
{
    cin>>n;
    scanf("%s",str);
    initedge(1010100);
    st[0]=0;
    int top=0,nodeCnt=0;
    depth=0;
    for(int i=0;str[i];i++)
    {
        if(str[i]=='(')
        {
            st[++top]=++nodeCnt;
            depth=max(depth,top);
            addedge(st[top-1],st[top]);
        }
        else
        {
            top--;
        }
    }
    dfs(0);
}


#include <bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define read(i) scanf("%d",&i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=2e6+5;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>void gmod(T &a,T b){a=((a+b)%mod+mod)%mod;}

ll gcd(ll a,ll b){ while(b) b^=a^=b^=a%=b; return a;}
ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll qpow(ll n,ll m)
{
    n%=mod;
    ll ans=1;
    for(;m;m>>=1)
    {
        if(m&1)ans=ans*n%mod;
        n=n*n%mod;
    }
    return ans;
}

struct node{
    int t,next;
    ll len;
}edge[MAX<<1];
int head[MAX],cnt;
void initedge(int n)
{
    memset(head,-1,sizeof(head[0])*(n+1));cnt=0;
}
void addedge(int u,int v,ll len=0)
{
    edge[cnt]=node{v,head[u],len};
    head[u]=cnt++;
}

int n,depth;
char str[MAX];
int st[MAX];

void dfs(int u,int dep=0)
{
    vector<int>vec;
    for(int i=head[u];~i;i=edge[i].next)vec.push_back(edge[i].t);
    for(int i=vec.size()-1;i>=0;i--)
    {
        //printf("(");
        printf("%d",dep>=depth/2);
        dfs(vec[i],dep+1);
        //printf(")");
        printf("%d",dep>=depth/2);
    }
}
int main()
{
    cin>>n;
    scanf("%s",str);
    initedge(1010100);
    st[0]=0;
    int top=0,nodeCnt=0;
    depth=0;
    for(int i=0;str[i];i++)
    {
        if(str[i]=='(')
        {
            st[++top]=++nodeCnt;
            depth=max(depth,top);
            addedge(st[top-1],st[top]);
            //cout<<st[top-1]<<" "<<st[top]<<endl;
        }
        else
        {
            top--;
        }
    }
   // cout<<depth<<endl;
    dfs(0);
}


#include<iostream>
#include<cstring>
using namespace std;
char s[200005];
int b[200005];
int main()
{
	int n;
	while(cin>>n)
	{
		memset(b,0,sizeof(b));
		cin>>s;
		int num=0;
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(s[i]=='(')
				num++;
			b[i]=num;
			flag=max(flag,num);
			if(s[i]==')')
				num--;
		}
		for(int i=1;i<=n;i++)
		{
			if(b[i]>flag/2)
				cout<<1;
			else
				cout<<0;
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
char s[200005];
int b[200005];
int main()
{
	int n;
	while(cin>>n)
	{
		memset(b,0,sizeof(b));
		cin>>s;
		int num=0;
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(s[i]=='(')
				num++;
			b[i]=num;
			flag=max(flag,num);
			if(s[i]==')')
				num--;
		}
		for(int i=1;i<=n;i++)
		{
			if(b[i]>flag/2)
				cout<<1;
			if(b[i]<=flag/2)
			{
				cout<<0;
				flag/=2;
			}
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
char s[200005];
int b[200005];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
	}
		int num=0;
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(s[i]=='(')
				num++;
			b[i]=num;
			flag=max(flag,num);
			if(s[i]==')')
				num--;
		}
		for(int i=1;i<=n;i++)
		{
			if(b[i]>flag/2)
				cout<<1;
			if(b[i]<=flag/2)
			{
				cout<<0;

			}
		}
		cout<<endl;

	return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
char s[200005];
int b[200005];
int main()
{
	int n;
	while(cin>>n)
	{
		memset(b,0,sizeof(b));
		cin>>s;
		int num=0;
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(s[i]=='(')
				num++;
			b[i]=num;
			flag=max(flag,num);
			if(s[i]==')')
				num--;
		}
		for(int i=1;i<=n;i++)
		{
			if(b[i]>flag/2)
				cout<<1;
			if(b[i]<=flag/2)
				cout<<0;
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#define N 200100
using namespace std;
int n,rd,bl;
int ans[N];
char str[N];
int main(){
	scanf("%d%s",&n,str+1);
    for(int i=1;i<=n;i++)
        if(str[i]==')')
           (rd>bl)?(--rd,ans[i]=0):(--bl,ans[i]=1);
        else if(str[i]=='(')
           (rd<bl)?(++rd,ans[i]=0):(++bl,ans[i]=1);
    for(int i=1;i<=n;i++)
        printf("%d",ans[i]);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
#define N 200005

int n;
char s[N];
int c1; int c2;

int main(){
    scanf("%d",&n);
    scanf("%s",s+1);
    for(int i=1;i<=n;++i){
        if(s[i]=='('){
            ++c1;
            printf("%d",c1&1);
        }
        else{
            ++c2;
            printf("%d",c2&1);
        }
    }
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2*1e5+10;
int n,rs=0,bs=0;char a[maxn];
int main(){
    scanf("%d%s",&n,a);
    for(int i=0;i<n;i++){
        if(a[i]==')') (rs>bs) ? (--rs,printf("0")) : (--bs,printf("1"));
        else if(a[i]=='(') (rs<bs) ? (++rs,printf("0")) : (++bs,printf("1"));
    }
    return 0;
}


#include <bits/stdc++.h>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
    int f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar();}
    while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
    return f * x;
}
int n, ans[200005] = {0};
char s[200005];
void init(){
    scanf("%d%s", &n, s);
}
void solve(){
    stack<int> a, b;
    for (int i = 0; i < n; ++i){
        if(s[i] == '('){
            if(a.size() <= b.size()){
                ans[i] = 0, a.push(1);
            }else{
                ans[i] = 1, b.push(1);
            }
        }else{
            if(a.size() < b.size()){
                ans[i] = 1, b.pop();
            }else{
                ans[i] = 0, a.pop();
            }
        }
    }
    for (int i = 0; i < n; ++i)
        printf("%d", ans[i]);
}
int main(){
    init();
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define MAXN 1000500
#define res register
#define db double
#define standard 1e-5
#define ll long long
using namespace std;
int n,ans[MAXN];
ll read()
{
	int s=0,w=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-48;ch=getchar();}
	return s*w;
}
int main()
{
	int a=0,b=0;
	n=read();
	string s;
	cin>>s;
	for(int i=0;i<n;i++)
	{
		if(s[i]=='(')
		{
			if(a<b)
			{
				a++;
				ans[i]=0;
			}
			else
			{
				b++;
				ans[i]=1;

			}
		}
		else
		{
			if(a>b)
			{
				a--;
				ans[i]=0;
			}
			else
			{
				b--;
				ans[i]=1;
			}
		}
	}
	for(res int i=0;i<n;i++)
	cout<<ans[i];
	return 0;
}


#include <bits/stdc++.h>
#include <stack>

using namespace std;

int _n;
stack<int> red;
stack<int> blue;
char a;
string ans;

void ParseIn(){
	cin >> _n;
	for(int i = 1; i <= _n; i++){
		cin >> a;
		if(a == '('){
			if(red.size() > blue.size()){
				blue.push(a);
				ans += '1';
			}else{
				red.push(a);
				ans += '0';
			}
		}else{
			if(red.size() > blue.size()){
				red.pop();
				ans += '0';
			}else{
				blue.pop();
				ans += '1';
			}
		}
	}

	cout << ans << endl;
}

int main(){
	ParseIn();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int l, r, a[200001], n;
char t[200001];
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> t[i];
        if (t[i] == '(') {
            if (l > r) a[i] = 1, r++;
            else a[i] = 0, l++;
        } else {
            if (l < r) a[i] = 0, r--;
            else a[i] = 1, l--;
        }
    }
    for (int i = 1; i <= n; i++) cout << a[i];
    return 0;
}


// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;
int n,point,ans=-1;
char a[1000050];
stack <char>s;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		s.push(a[i]);
		if(s.top()=='(')
		point++;
		else
		s.pop(),point--;
		ans=max(ans,point);
	}
	if(ans%2==1)
	cout<<ans/2+1<<endl;
	else
	cout<<ans/2<<endl;
}


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

const int N = 1e6 + 10;
const int inf = INT_MAX;

#define ll long long
#define FOR(i,a,b) for(int i = a;i <= b;i++)
#define _FOR(i,a,b) for(int i = a;i >= b;i--)

template<typename T> void read(T &x)
{
    x = 0;int f = 1;
    char c = getchar();
    for(;!isdigit(c);c = getchar()) if(c == '-') f = -1;
    for(;isdigit(c);c = getchar()) x = x * 10 + c - '0';
    x *= f;
}

int n,a[N];
char s[N];

void solve(int l,int r,int dep)
{
	//printf("!!!%d %d %d\n",l,r,dep);
	if(l > r) return ;
	int num = 0,tmp = 0;
	FOR(i,l,r)
	{
		if(s[i] == '(') num++;
		else num--;
		if(num == 0 && s[i] == ')') {tmp = i;break;}
	}
	a[l] = a[tmp] = dep & 1;
	solve(l + 1,tmp - 1,dep + 1);
	solve(tmp + 1,r,dep);
}

int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
	read(n);
	scanf("%s",s + 1);
	solve(1,n,1);
	FOR(i,1,n)
		printf("%d",a[i]);
    return 0;
}


#include<bits/stdc++.h>

constexpr auto MAXN = (int)2e5+500;

char s[MAXN];

signed main(void)
{
    int len,depth=0,max=0;

    std::cin>>len>>s;
    for(int i=0;i<len;i++){
        if(s[i]=='(') depth++;
        else depth--;
        max=std::max(max,depth);
    }
    max=max+1>>1; depth=0;

    for(int i=0;i<len;i++){
        if(s[i]=='('){ depth++; putchar(depth<=max?'0':'1'); }
        else{ putchar(depth<=max?'0':'1'); depth--; }
    } putchar('\n');

    return 0;
}


#include <bits/stdc++.h>
#include <stack>

using namespace std;

int _n;
stack<int> red;
stack<int> blue;
int red2;
int blue2;
char a;
string ans;

void ParseIn(){
	cin >> _n;
	for(int i = 1; i <= _n; i++){
		cin >> a;
		if(a == '('){
			if(red.size() > blue.size()){
				blue.push(a);
				ans += '1';
			}else{
				red.push(a);
				ans += '0';
			}
		}else{
			if(red.size() > blue.size()){
				red.pop();
				ans += '1';
			}else{
				blue.pop();
				ans += '0';
			}
		}
	}

	cout << ans << endl;
}

int main(){
	ParseIn();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,tot,maxx,js,a[10000001],color[10000001];
char s[10000001];
int main() {
	scanf("%d",&n);
	scanf("%s",s);
	for(int i=0; i<n; i++) {
		if(s[i]=='(')
			color[i]=1,tot++;
		else
			color[i]=-1,tot--;

		maxx=max(maxx,tot);
	}
	maxx=ceil(maxx/2.0);
	for(int i=0; i<n; i++) {
		if(color[i]==1&&js==maxx)
			a[i]=0;
		else if(color[i]==1)
			a[i]=1,js++;
		if(color[i]==-1&&js!=0)
			a[i]=1,js--;
		else if(color[i]==-1)
			a[i]=0;
	}
	for(int i=0; i<n; i++)
		printf("%d",a[i]);
	return 0;
}


#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

const int mod=1e9+7;
const int mo=998244353;
const int N=2e5+5;

char ch[N];
int n,m,ans,a[N];

inline bool pd(int x)
{
	int A=0,B=0;
	For(i,1,n)
	{
		if(ch[i]=='(')
		{
			if(A<B) A++,a[i]=0;
			else B++,a[i]=1;
		}
		if(ch[i]==')')
		{
			if(A) A--,a[i]=0;
			else B--,a[i]=1;
		}
	}
	return (!A&&!B);
}

int main()
{
	n=read();
	scanf("%s",ch+1);
	int l=1,r=n;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if(pd(mid)) r=mid-1,ans=mid;
		else l=mid+1;
	}
	pd(ans);
	For(i,1,n) printf("%d",a[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int l, r, a[200001], n;
char t[200001];
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> t[i];
        if (t[i] == '(') {
            if (l > r) a[i] = 1, r++;
            else a[i] = 0, l++;
        } else {
            if (l < r) a[i] = 0, r--;
            else a[i] = 1, l--;
        }
        cout << a[i];
    }
    return 0;
}


#include <bits/stdc++.h>
#include <stack>

using namespace std;

int _n;
stack<char> red;
stack<char> blue;
int _res[200005];

void Core () {
	char check;
	int cc = 0, temp = 0;
	cin >> _n;
	for (int i = 0; i < _n; i++) {
		cin >> check;
		if (check == '(') {
 			if (blue.size() >= red.size()) {
				blue.push('(');
				_res[i] = 1;
			}
			else {
				red.push('(');
				_res[i] = 0;
			}
		}
		else {
			if (blue.size() <= red.size()) {
				blue.pop();
				_res[i] = 1;
			}
			else {
				red.pop();
				_res[i] = 0;
			}
		}
	}
}

void WriteOut () {
	for (int i = 0; i < _n; i++) {
		cout << _res[i];
	}
	cout << endl;
}

int main () {

	Core();
	WriteOut();

	return 0;
}


#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const double esp = 1e-12;
const int inf = 0x3f3f3f3f;

int flag[200005];
int ff[200005];

int main()
{
    int n;
    cin>>n;
    string str;
    cin>>str;

    int sum=0,Max=0;
    for(int i=0;i<str.size();i++)
    {
        if(str[i]=='(')
            sum++;
        else
            sum--;
        ff[i]=sum;
        Max=max(sum,Max);
    }

    Max/=2;

    int right=0;
    for(int i=0;i<str.size();i++)
    {
        if(ff[i]<=Max&&str[i]=='(')
        {
            right=i;
            flag[i]=1;
        }
    }

    for(int i=str.size()-1;i>=right+1;i--)
    {
        if(Max>0&&str[i]==')')
        {
            flag[i]=1;
            Max--;
        }

    }

    for(int i=0;i<str.size();i++)
    {
        cout<<flag[i];
    }
    return 0;
}


#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const double esp = 1e-12;
const int inf = 0x3f3f3f3f;

int flag[200005];

int main()
{
    int n;
    cin>>n;
    string str;
    cin>>str;

    int left=0;
    int right=0;

    while(left<str.size())
    {
        if(str[left]=='(')
        {
            if(str[right]=='(')
                right++;
            else
            {
                flag[left]=1;
                flag[right]=1;
                left=right+1;
                right++;
            }
        }
        else
        {
            left++;
            right++;
        }
    }

    for(int i=0;i<str.size();i++)
    {
        cout<<flag[i];
    }
    return 0;
}


#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const double esp = 1e-12;
const int inf = 0x3f3f3f3f;

int flag[200005];
int ff[200005];

int main()
{
    int n;
    cin>>n;
    string str;
    cin>>str;

    int sum=0,Max=0;
    for(int i=0;i<str.size();i++)
    {
        if(str[i]=='(')
            sum++;
        else
            sum--;
        ff[i]=sum;
        Max=max(sum,Max);
    }

    Max/=2;

    int right=0;
    for(int i=0;i<str.size();i++)
    {
        if(ff[i]<=Max&&str[i]=='(')
        {
            right=i;
            flag[i]=1;
        }
    }

    for(int i=right+1;i<str.size();i++)
    {
        if(Max>0&&str[i]==')')
        {
            flag[i]=1;
            Max--;
        }

    }

    for(int i=0;i<str.size();i++)
    {
        cout<<flag[i];
    }
    return 0;
}


#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const double esp = 1e-12;
const int inf = 0x3f3f3f3f;

int flag[200005];
int ff[200005];

int main()
{
    int n;
    cin>>n;
    string str;
    cin>>str;

    int sum=0,Max=0;
    for(int i=0;i<str.size();i++)
    {
        if(str[i]=='(')
            sum++;
        else
            sum--;
        ff[i]=sum;
        Max=max(sum,Max);
    }

    Max/=2;

    stack<int> a;
    for(int i=0;i<str.size();i++)
    {
        if(str[i]=='(')
        {
            a.push(str[i]);
            if(a.size()<=Max)
                flag[i]=1;
        }
        else
        {
            a.pop();
            if(a.size()<Max)
                flag[i]=1;
        }
    }

    for(int i=0;i<str.size();i++)
    {
        cout<<flag[i];
    }
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int cc;
int list[1000000];
vector<int>blue;
vector<int>red;
vector<bool>tlist;

void ParseIn (){
	cin >> cc;
	char cur;
	for(int i = 0; i < cc; i++){
		cin >> cur;
		if (cur == '('){
			list[i] = 1;
		}else {
			list[i] = 0;
		}

	}

}
void Core (){

	for (int i = 0; i < cc; i++){
		if (list[i]){
			if (red.size() >= blue.size()){
				cout << 0;
				blue.push_back(1);
				tlist.push_back(true);

			}else {
				cout << 1;
				red.push_back(1);
				tlist.push_back(false);
			}

		}else {

			if (tlist.at(0)){
				cout << 0;

			}else{
				cout << 1;
			}

		}
	}

}

int main (){
	ParseIn();
	Core();

	return 0;
}


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

const int N = 1e6 + 10;
const int inf = INT_MAX;

#define ll long long
#define FOR(i,a,b) for(int i = a;i <= b;i++)
#define _FOR(i,a,b) for(int i = a;i >= b;i--)

template<typename T> void read(T &x)
{
    x = 0;int f = 1;
    char c = getchar();
    for(;!isdigit(c);c = getchar()) if(c == '-') f = -1;
    for(;isdigit(c);c = getchar()) x = x * 10 + c - '0';
    x *= f;
}

stack<int> S;
int n,a[N];
char s[N];

int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
	read(n);
	scanf("%s",s + 1);
	S.push(0);
	FOR(i,1,n)
	{
		if(s[i] == '(')
		{
			a[i] = S.top() + 1;
			S.push(S.top() + 1);
		}
		else
		{
			a[i] = S.top();
			S.pop();
		}
	}
	FOR(i,1,n)
		printf("%d",a[i] & 1);
    return 0;
}


#include<bits/stdc++.h>
#define MAX 100005
using namespace std;
stack<char> st;
int a[MAX];
int main()
{
	int n;
	cin>>n;
	int num=0;
	cin.get();
	for(int i=1;i<=n;i++)
	{
		char ch;
		ch=getchar();
//		cout<<ch<<" ";
//		cout<<i<<" || "<<endl;
		if(ch=='(')
		{
			a[i]=num;
			num=1-num;
		//	cout<<num<<"  "<<i<<endl;
		}
		if(ch==')')
		{
			a[i]=1-num;
			num=1-num;
		//	cout<<num<<"-"<<endl;
		}
	}
	for(int i=1;i<=n;i++)
	{
		printf("%d",a[i]);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;

LL n,m,k;
LL col[500005] = {0};
LL ans[500005] = {0};
LL L,R,M;
string s;

LL ck(LL x){
	memset(col,0,sizeof(col));
	LL r = 0,b = 0;
	for(LL i = 0;i < n;i ++){
		if(s[i] == '('){
			if(r < x){ r ++; col[i] = 0; }
			else{ b ++; col[i] = 1; }
			if(b > x) return 0;
		}
		if(s[i] == ')'){
			if(b > r){ b --; col[i] = 1; }
			else{ r --; col[i] = 0; }
		}
    }
	for(LL i = 0;i < n;i ++) ans[i] = col[i];
	return 1;
}

int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	cin >> s;
	L = 1; R = n;
	while(L <= R){
		M = (L + R) >> 1;
		if(!ck(M)) L = M + 1;
		else R = M - 1;
	}
	for(LL i = 0;i < n;i ++){
		cout << ans[i];
	}
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
#include <stack>

using namespace std;

int _n;
stack<char> red;
stack<char> blue;
int _res[200005];

void Core () {
	char check;
	int cc = 0, temp = 0;
	cin >> _n;
	for (int i = 0; i < _n; i++) {
		cin >> check;
		if (check == '(') {
 			if (blue.size() <= red.size()) {
				blue.push('(');
				_res[i] = 1;
			}
			else {
				red.push('(');
				_res[i] = 0;
			}
		}
		else {
			if (blue.size() >= red.size()) {
				blue.pop();
				_res[i] = 1;
			}
			else {
				red.pop();
				_res[i] = 0;
			}
		}
	}
}

void WriteOut () {
	for (int i = 0; i < _n; i++) {
		cout << _res[i];
	}
	cout << endl;
}

int main () {

	Core();
	WriteOut();

	return 0;
}


#include <cstdio>
#include <algorithm>

const int N = 200010;

char s[N];
int n, top, ans[N], st[N], id[N][3], cur, dep;

int main() {
	bool flag = 0;
	scanf("%d%s", &n, s + 1);
	for(int i = 1; i <= n; ++i) {
		if(s[i] == '(') st[++top] = i, dep++;
		else {
			id[++cur][0] = i;
			id[cur][1] = st[top--];
			id[cur][2] = dep & 1;
			--dep;
		}
		if(!top) {
			for(int i = 1; i <= cur / 2; ++i)
				ans[id[i][0]] = ans[id[i][1]] = id[i][2];
			for(int i = cur / 2 + 1; i <= cur; ++i)
				ans[id[i][0]] = ans[id[i][1]] = id[i][2];
			dep = 0;
		}
	}
	for(int i = 1; i <= n; ++i) printf("%d", ans[i]);
}


#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

inline int read(){
    int x = 0; int zf = 1; char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}

int rd = 0;
int bl = 0;
int ans[200005];

int main(){
	int n = read(); int now=111;
	string s; cin >> s;
	for (int i = 0; i < n; ++i){
		if (s[i] == ')')
			(rd > bl) ? (--rd, ans[i] = 0) : (--bl, ans[i] = 1);
		else if (s[i] == '(')
			(rd < bl) ? (++rd, ans[i] = 0) : (++bl, ans[i] = 1);
	}
	for (int i = 0; i < n; ++i)
		printf("%d", ans[i]);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;

LL n,m,k;
LL col[500005] = {0};
LL ans[500005] = {0};
LL L,R,M;
string s;

LL ck(LL x){
	memset(col,0,sizeof(col));
	LL r = 0,b = 0;
	for(LL i = 0;i < n;i ++){
		if(s[i] == '('){
			if(r < x){ r ++; col[i] = 0; }
			else{ b ++; col[i] = 1; }
			if(b > x) return 0;
		}
		if(s[i] == ')'){
			if(b > r){ b --; col[i] = 1; }
			else{ r --; col[i] = 0; }
		}
    }
	for(LL i = 0;i < n;i ++) ans[i] = col[i];
	return 1;
}

int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	cin >> s;
	L = 1; R = n;
	while(L <= R){
		M = (L + R) >> 1;
		if(!ck(M)) L = M + 1;
		else R = M - 1;
	}
	for(LL i = 0;i < n;i ++){
		cout << ans[i];
	}
	cout << endl;
	return 0;
}
//


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char s[200005];
int n,ans[200005];
bool check(int x)
{
	int num1=0,num2=0;
	for(int i=1;i<=n;i++)
	{
		if(s[i]=='(')
		{
			if(num1<x)ans[i]=1,num1++;
			else if(num2<x)ans[i]=0,num2++;
			else return 0;
		}
		else
		{
			if(num1)num1--,ans[i]=1;
			else num2--,ans[i]=0;
		}
	}
	return 1;
}
int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	int l=0,r=n;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(check(mid))r=mid-1;
		else l=mid+1;
	}
	check(l);
	for(int i=1;i<=n;i++)printf("%d",ans[i]);
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
#define INL inline
#define REG register
#define DB double
#define LDB long double
#define ULL unsigned long long
#define LL long long

#define RPT(i,x,y) for (REG int i=(x);i<(y);i++)
#define DRPT(i,x,y) for (REG int i=(x);i>(y);i--)
#define MST(a,b) memset((a),(b),sizeof(a))
#define MRK() cout<<"Mark"<<endl;
#define WRT(x) cout<<#x<<" = "<<(x)<<endl;

#define MAXN 510000
#define MAXM 10000
#define MOD 998244353
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-5

#define _ 0
using namespace std;

int n1,n2;
char str[MAXN];
int col[MAXN];
int len;

int main()
{
	cin>>len;
	cin>>str;
	RPT(i,0,len)
	{
		if (str[i]=='(')
		{
			if (n1>n2)
			{
				col[i]=1;
				n2++;
			}
			else
			{
				col[i]=0;
				n1++;
			}
		}
		else
		{
			if (n1<n2)
			{
				col[i]=1;
				n2--;
			}
			else
			{
				col[i]=0;
				n1--;
			}

		}
	}
	RPT(i,0,len)
		cout<<col[i];
	return ~~(0^_^0);
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
typedef vector<int> VI;

#define NN 200200

char s[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; scanf("%d %s", &n, s+1);
	for(int i=1, f=0, ans; i<=n; i++) {
		if(s[i]=='(') {
			f^=1;
			ans=f;
		}
		else {
			ans=f;
			f^=1;
		}
		printf("%d", ans);
	}
}


#include <iostream>
#include <unordered_set>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
unordered_set<long long> s;

int main(){
    s.reserve(131072);
    s.max_load_factor(0.25);
    int n,m;
    cin >> n >> m;
    int r = 0;
    s.insert(0);
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        for (long long j = 1; j <= MAXX; j*=m)
            r+=s.count(a[i]-j);
        s.insert(a[i]);
    }
    cout << r;
}


#include <iostream>
#include <unordered_set>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 100000000000005

long long a[DIM];
unordered_set<long long> s;

int main(){
    s.reserve(131072);
    s.max_load_factor(0.25);
    int n,m;
    cin >> n >> m;
    int r = 0;
    s.insert(0);
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        for (long long j = 1; j <= MAXX; j*=m)
            r+=s.count(a[i]-j);
        s.insert(a[i]);
    }
    cout << r;
}


#include <iostream>
#include <unordered_map>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
unordered_map<long long, long long> s;

int main(){
    long long n,m;
    cin >> n >> m;
    long long r = 0;
    s[0]++;
    for (int i = 1; i <= n; i++){
        long long x;
        cin >> x;
        a[i]=a[i-1]+x;
        if (m==1)
            r+=s[a[i]-1];
        else if (m==-1)
            r+=s[a[i]-1]+s[a[i]+1];
        else{
            for (long long j = 1; j <= MAXX; j*=m)
                r+=s[a[i]-j];
        }
        s[a[i]]++;
    }
    cout << r;
}


#include <iostream>
#include <unordered_map>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
unordered_map<long long, long long> s;

int main(){
    int n,m;
    cin >> n >> m;
    long long r = 0;
    s[0]++;
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        if (m==1)
            r+=s[a[i]-1];
        else if (m==-1)
            r+=s[a[i]-1]+s[a[i]+1];
        else{
            for (long long j = 1; j <= MAXX; j*=m)
                r+=s[a[i]-j];
        }
        s[a[i]]++;
    }
    cout << r;
}


#include <iostream>
#include <unordered_map>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
unordered_map<long long, long long> s;

int main(){
    int n,m;
    cin >> n >> m;
    long long r = 0;
    s[0]++;
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        for (long long j = 1; j <= MAXX; j*=m)
            r+=s[a[i]-j];
        s[a[i]]++;
    }
    cout << r;
}


 #include <iostream>
#include <unordered_set>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
unordered_set<long long> s;

int main(){
    s.reserve(131072);
    s.max_load_factor(0.25);
    int n,m;
    cin >> n >> m;
    long long r = 0;
    s.insert(0);
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        for (long long j = 1; j <= MAXX; j*=m)
            r+=s.count(a[i]-j);
        s.insert(a[i]);
    }
    cout << r;
}


#include <iostream>
#include <set>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 100000000000005

long long a[DIM];
multiset<long long> s;

int main(){
    int n,m;
    cin >> n >> m;
    int r = 0;
    s.insert(0);
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        for (long long j = 1; j <= MAXX; j*=m)
            r+=s.count(a[i]-j);
        s.insert(a[i]);
    }
    cout << r;
}


#include <iostream>
#include <map>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
map<long long, long long> s;

int main(){
    long long n,m;
    cin >> n >> m;
    long long r = 0;
    s[0]++;
    for (int i = 1; i <= n; i++){
        long long x;
        cin >> x;
        a[i]=a[i-1]+x;
        if (m==1)
            r+=s[a[i]-1];
        else if (m==-1)
            r+=s[a[i]-1]+s[a[i]+1];
        else{
            for (long long j = 1; j <= MAXX; j*=m)
                r+=s[a[i]-j];
        }
        s[a[i]]++;
    }
    cout << r;
}


#include <iostream>
#include <map>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
map<long long, long long> s;

int main(){
    int n,m;
    cin >> n >> m;
    int r = 0;
    s[0]++;
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        for (long long j = 1; j <= MAXX; j*=m)
            r+=s[a[i]-j];
        s[a[i]]++;
    }
    cout << r;
}


#include <iostream>
#include <map>
#include <climits>

using namespace std;

#define DIM 100005
#define MAXX 1000000000000005

long long a[DIM];
map<long long, long long> s;

int main(){
    int n,m;
    cin >> n >> m;
    long long r = 0;
    s[0]++;
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        a[i]=a[i-1]+x;
        for (long long j = 1; j <= MAXX; j*=m)
            r+=s[a[i]-j];
        s[a[i]]++;
    }
    cout << r;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 1;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        if(k < 0){ mi.clear();
            r = p(k, 1);
            for(int j = 0; j < n; j++){
                mi[s[j]]++;
                if(s[j] == r) e++;
                e += mi[s[j] - r];
            }
        }
        cout << e << endl;
        return 0;
    }
    for(int i = 0; i < mc*2; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <unordered_map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
unordered_map<string, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 0;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[lts(s[j])]++;
            if(s[j] == r) e++;
            e += mi[lts(s[j] - r)];
        }
        mi.clear();
        r = p(k, 1);
        for(int j = 0; j < n; j++){
            mi[lts(s[j])]++;
            if(s[j] == r) e++;
            e += mi[lts(s[j] - r)];
        }
    }
    for(int i = 0; i < mc; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[lts(s[j])]++;
            if(s[j] == r) e++;
            e += mi[lts(s[j] - r)];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 0;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        mi.clear();
        r = p(k, 1);
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    for(int i = 0; i < mc; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 0;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        if(k < 0){ mi.clear();
            r = p(k, 1);
            for(int j = 0; j < n; j++){
                mi[s[j]]++;
                if(s[j] == r) e++;
                e += mi[s[j] - r];
            }
        }
    }
    for(int i = 0; i < mc; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 0;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        if(k < 0){ mi.clear();
            r = p(k, 1);
            for(int j = 0; j < n; j++){
                mi[s[j]]++;
                if(s[j] == r) e++;
                e += mi[s[j] - r];
            }
        }
        cout << e << endl;
        return 0;
    }
    for(int i = 0; i < mc+1; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <unordered_map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
int n, k;
vector<long long> s;
unordered_map<string, int> mi;

int main()
{
    long long a, e= 0;
    cin >> n >> k;
    s.insert(s.begin(), n+1, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
    for(int i = 0; i < 15; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[lts(s[j])]++;
            if(s[j] == r) e++;
            e += mi[lts(s[j] - r)];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 0;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        if(k < 0){ mi.clear();
            r = p(k, 1);
            for(int j = 0; j < n; j++){
                mi[s[j]]++;
                if(s[j] == r) e++;
                e += mi[s[j] - r];
            }
        }
    }
    for(int i = 0; i < mc+1; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <unordered_map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
unordered_map<string, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 0;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[lts(s[j])]++;
            if(s[j] == r) e++;
            e += mi[lts(s[j] - r)];
        } }
    for(int i = 0; i < mc; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[lts(s[j])]++;
            if(s[j] == r) e++;
            e += mi[lts(s[j] - r)];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 1;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        if(k < 0){ mi.clear();
            r = p(k, 1);
            for(int j = 0; j < n; j++){
                mi[s[j]]++;
                if(s[j] == r) e++;
                e += mi[s[j] - r];
            }
        }
        cout << e << endl;
        return 0;
    }
    for(int i = 0; i < 51; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <unordered_map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
unordered_map<string, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 0;
    for(; am; am /= k) mc++;
    for(int i = 0; i < mc; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[lts(s[j])]++;
            if(s[j] == r) e++;
            e += mi[lts(s[j] - r)];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 1;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        if(k < 0){ mi.clear();
            r = p(k, 1);
            for(int j = 0; j < n; j++){
                mi[s[j]]++;
                if(s[j] == r) e++;
                e += mi[s[j] - r];
            }
        }
        cout << e << endl;
        return 0;
    }
    for(int i = 0; i < mc+1; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <iostream>
#include <map>
#include <cstring>
#include <vector>

using namespace std;

string lts(long long a){
    string b = "";
    if(a < 0) b = "-";
    if(!a) b = "0";
    for(; a; a /= 10){
        int v = a%10+48;
        b+=v;
    }
    for(int i = 0, j = b.size()-1; i < b.size()/2; i++, j--){
        char k = b[i];
        b[i] = b[j];
        b[j] = k;
    }
    return b;
}
long long p(long long a, int x){
    int g = a;
    a = 1;
    for(int i = 0; i < x; i++)
        a *= g;
    return a;
}
long long n, k;
vector<long long> dp, s;
map<long long, int> mi;

long long max(long long a, long long b){
    return (a>b)?a:b;
}
int main()
{
    long long a, e= 0;
    cin >> n >> k;
s.insert(s.begin(), n+1, 0);    long long am = 0;
    dp.insert(dp.begin(), n, 0);
    for(int i = 0; i < n; i++){
        cin >> a;
    if(!i) dp[0] = max(0, a);
    else dp[i] = max(dp[i-1] + a, 0);
    am = max(dp[i], am);
        if(i > 0) s[i] = s[i-1] + a;
        else s[i] =a;
    }
//    cout << am << endl;
    int mc = 1;
    if(k > 1 || k < -1)for(; am; am /= k) mc++;
    else {         long long r = p(k, 0);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
        if(k < 0){ mi.clear();
            r = p(k, 1);
            for(int j = 0; j < n; j++){
                mi[s[j]]++;
                if(s[j] == r) e++;
                e += mi[s[j] - r];
            }
        }
        cout << e << endl;
        return 0;
    }
    for(int i =0; p(k, i) < 1e15; i++){
        long long r = p(k, i);
        mi.clear();
        for(int j = 0; j < n; j++){
            mi[s[j]]++;
            if(s[j] == r) e++;
            e += mi[s[j] - r];
        }
    }
    cout << e;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			cnt += mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0;; i++) {
		ll num = pow(k, i);
		if (num > 1e15 || num < -1e15)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			++cnt;
		mp[currSum] = i;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0;; i++) {
		ll num = pow(k, i);
		if (num > 1e15 || num < -1e15)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	cout << ans;
	return 0;
}
/*

 */


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
//	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

int n, k;
int a[N];
unordered_map<ll, int> mp;
ll getSegment(ll sum) {
	mp.clear();
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = (ll)currSum + a[i];
		if (currSum == sum)
			++cnt;
		cnt += (ll)mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	scanf("%d%d" , &n , &k) ;
	for (int i = 0; i < n; ++i)
		scanf("%d" , a+ i) ;
	if (k == 1)
		power.pb(1);
	else if (k == -1)
		power.pb(1), power.pb(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			power.pb(val), val *= k;
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	printf("%I64d" , ans) ;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		cnt += mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	if (k == 1)
		power.pb(1);
	else if (k == -1)
		power.pb(1), power.pb(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			power.pb(val), val *= k;
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			cnt += mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	if (k == 1)
		power.pb(1);
	else if (k == -1)
		power.pb(1), power.pb(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			power.pb(val), val *= k;
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
//	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		cnt += mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	scanf("%I64d%I64d" , &n , &k) ;
	for (int i = 0; i < n; ++i)
		scanf("%I64d" , a+ i) ;
	if (k == 1)
		power.pb(1);
	else if (k == -1)
		power.pb(1), power.pb(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			power.pb(val), val *= k;
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	printf("%I64d" , ans) ;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll fastPower(ll b,ll p) {
	ll r = 1;
	while (p > 0) {
		if (p % 2 == 1)
			r = (r * b);
		b = (b * b);
		p = p / 2;
	}
	return r;
}
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			++cnt;
		mp[currSum] = i;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0;; i++) {
		ll num = fastPower(k, i);
		if (num > 1e9 || num < -1e9)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll)getSegment(power[i]);
	cout << ans;
	return 0;
}
/*

 */


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll fastPower(ll b,ll p) {
	ll r = 1;
	while (p > 0) {
		if (p % 2 == 1)
			r = (r * b);
		b = (b * b);
		p = p / 2;
	}
	return r;
}
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			++cnt;
		mp[currSum] = i;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0;; i++) {
		ll num = fastPower(k, i);
		if (num > 1e12 || num < -1e12)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll)getSegment(power[i]);
	cout << ans;
	return 0;
}
/*

 */


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
//	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 1e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

int n, k;
int a[N];
unordered_map<ll, int> mp;
ll getSegment(ll sum) {
	mp.clear();
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = (ll)currSum + a[i];
		if (currSum == sum)
			++cnt;
		cnt += (ll)mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	scanf("%d%d" , &n , &k) ;
	for (int i = 0; i < n; ++i)
		scanf("%d" , a+ i) ;
	if (k == 1)
		power.emplace_back(1);
	else if (k == -1)
		power.emplace_back(1), power.emplace_back(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			power.emplace_back(val), val *= k;
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	printf("%I64d" , ans) ;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll fastPower(ll b, ll p) {
	ll r = 1;
	while (p > 0) {
		if (p % 2 == 1)
			r = (r * b);
		b = (b * b);
		p = p / 2;
	}
	return r;
}
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			++cnt;
		mp[currSum] = i;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0;; i++) {
		ll num = fastPower(k, i);
		if (num > 1e15 || num < -1e15)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	cout << ans;
	return 0;
}
/*

 */


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			cnt += mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	if (k == 1)
		power.pb(1);
	else if (k == -1)
		power.pb(1), power.pb(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			power.pb(val), val *= k;
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll fastPower(ll b,ll p) {
	ll r = 1;
	while (p > 0) {
		if (p % 2 == 1)
			r = (r * b);
		b = (b * b);
		p = p / 2;
	}
	return r;
}
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			++cnt;
		mp[currSum] = i;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0;; i++) {
		ll num = fastPower(k, i);
		if (num > 1e18 || num < -1e18)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll)getSegment(power[i]);
	cout << ans;
	return 0;
}
/*

 */


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define lp(i,n) 	for(int i=0;i<(int)n;i++)
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
vector<ll> powers;
map<ll, ll> mp;
int main() {
	Yahia74();
	cin >> n >> k;
	if (k == 1)
		powers.pb(1);
	else if (k == -1)
		powers.pb(1), powers.pb(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			powers.pb(val), val *= k;
	}
	ll cum = 0, x , ans = 0 ;
	for (int i = 0; i < n; i++) {
		cin >> x;
		cum += x;
		for (int j = 0; j < sz(powers); j++) {
			if (cum == powers[j])
				ans++;
			ans += mp[cum - powers[j]];
		}
		mp[cum]++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
//	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 1e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

int n, k;
int a[N];
map<ll, int> mp;
ll getSegment(ll sum) {
	mp.clear();
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = (ll)currSum + a[i];
		if (currSum == sum)
			++cnt;
		cnt += (ll)mp[currSum - sum];
		mp[currSum]++;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	scanf("%d%d" , &n , &k) ;
	for (int i = 0; i < n; ++i)
		scanf("%d" , a+ i) ;
	if (k == 1)
		power.emplace_back(1);
	else if (k == -1)
		power.emplace_back(1), power.emplace_back(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			power.emplace_back(val), val *= k;
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll) getSegment(power[i]);
	printf("%I64d" , ans) ;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
ll a[N];
ll fastPower(ll b,ll p) {
	ll r = 1;
	while (p > 0) {
		if (p % 2 == 1)
			r = (r * b) % (24739954287740860);
		b = (b * b) % (24739954287740860);
		p = p / 2;
	}
	return r;
}
int getSegment(ll sum) {
	unordered_map<ll, ll> mp;
	ll currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			++cnt;
		mp[currSum] = i;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0;; i++) {
		ll num = fastPower(k, i);
		if (num > 1e16 || num < -1e16)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll)getSegment(power[i]);
	cout << ans;
	return 0;
}
/*

 */


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

int n, k;
int a[N];
ll fastPower(ll b,ll p) {
	ll r = 1;
	while (p > 0) {
		if (p % 2 == 1)
			r = (r * b);
		b = (b * b);
		p = p / 2;
	}
	return r;
}
int getSegment(int sum) {
	unordered_map<int, int> mp;
	int currSum = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		currSum = currSum + a[i];
		if (currSum == sum)
			++cnt;
		if (mp.find(currSum - sum) != mp.end())
			++cnt;
		mp[currSum] = i;
	}
	return cnt;
}
vector<ll> power;
int main() {
	Yahia74();
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	for (int i = 0;; i++) {
		ll num = fastPower(k, i);
		if (num > 1e9 || num < -1e9)
			break;
		if (k < 0 && (i & 1))
			power.pb(-num);
		else
			power.pb(num);
	}
	ll ans = 0;
	for (int i = 0; i < sz(power); ++i)
		ans += (ll)getSegment(power[i]);
	cout << ans;
	return 0;
}
/*

 */


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define lp(i,n) 	for(int i=0;i<(int)n;i++)
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 2e5 + 74, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll n, k;
vector<ll> powers;
map<ll, ll> mp;
int main() {
	Yahia74();
	cin >> n >> k;
	if (k == 1)
		powers.pb(1);
	else if (k == -1)
		powers.pb(1), powers.pb(-1);
	else {
		ll val = 1;
		while (abs(val) <= 1e15)
			powers.pb(val), val *= k;
	}
	ll cum = 0, x , ans = 0 ;
	for (int i = 0; i < n; i++) {
		cin >> x;
		cum += x;
		for (int j = 0; j < sz(powers); j++) {
			if (cum == powers[j])
				ans++;
			ans += mp[cum - powers[j]];
		}
		mp[cum]=1;
	}
	cout << ans;
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 101010;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int flag[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    if(k==1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1) ans++;
        cout<<ans<<endl;
        return 0;
    }
    if(k==-1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1||arr[i]==-1) ans++;
        cout<<ans<<endl;
        return 0;
    }

    rep(i,1,64){
        pp[i] = pow(k,i);
        if(pp[i]>100000000000005||pp[i]<-100000000000005) break;
        len++;
    }

    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,int> mymap;
    ll ans = 0;
    rep(i,1,n+1) if(arr[i]==1) ans++;

    mymap[0] = 1;
    rep(i,1,n+1){
        rep(j,1,len+1){
            ll tmp = sum[i]-pp[j];
            if(arr[i]==0&&tmp==0) continue;
            if(mymap.count(tmp)){
                //dd(i);dd(j);dd(tmp);de(mymap[tmp]);
                ans+=mymap[tmp];
            }
        }
        mymap[sum[i]]++;
        mymap[0] = 1;
    }
    cout<<ans<<endl;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 1e5+5;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    if(k==1){
        pp[0] = 1;
        len = 0;
    }
    else if(k==-1){
        pp[0] = 1;
        pp[1] = -1;
        len = 1;
    }
    else if(k!=1 && k!=-1){
        len = 0;
        pp[0] = 1;
        rep(i,1,64){
            pp[i] = k*pp[i-1];
            if(pp[i]>200000000000005||pp[i]<-200000000000005) break;
            len++;
        }
    }

    /*set<ll> myset;
    myset.insert(1);
    ll tmp = 1;
    rep(i,1,60){
        tmp *= k;
        if(abs(tmp)>100000000000000) break;
        myset.insert(tmp);
    }*/

    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,ll> mymap;
    ll ans = 0;
    mymap[0] = 1;
    rep(i,1,n+1){
        //dd(i);de(sum[i]);
        rep(j,0,len+1){
            ll tmp = sum[i]-pp[j];
            ans+=mymap[tmp];
        }
        /*set<ll>::iterator it;
        for(it = myset.begin();it!=myset.end();++it) ans+=mymap[sum[i]-*it];*/
        mymap[sum[i]]++;
        //mymap[0] = 1;
    }
    cout<<ans<<endl;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 101010;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int flag[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    if(k==1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1) ans++;
        cout<<ans<<endl;
        return 0;
    }
    if(k==-1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1||arr[i]==-1) ans++;
        cout<<ans<<endl;
        return 0;
    }
    rep(i,1,64){
        pp[i] = pow(k,i);
        if(pp[i]>100000000000005||pp[i]<-100000000000005) break;
        len++;
    }
    //rep(i,1,len+1) printf("%lld\n",pp[i],i==len?'\n':' ');
    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,int> mymap;
    ll ans = 0;
    rep(i,1,n+1) if(arr[i]==1) ans++;

    mymap[0] = 1;
    rep(i,1,n+1){
        rep(j,1,len+1){
            ll tmp = sum[i]-pp[j];
            if(mymap.count(tmp)){
                //dd(i);dd(j);dd(tmp);de(mymap[tmp]);
                ans+=mymap[tmp];
            }
        }
        mymap[sum[i]]++;
    }
    cout<<ans<<endl;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 1e5+5;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    if(k==1){
        pp[0] = 1;
        len = 0;
    }
    else if(k==-1){
        pp[0] = 1;
        pp[1] = -1;
        len = 1;
    }
    else if(k!=1 && k!=-1){
        len = 0;
        pp[0] = 1;
        rep(i,1,64){
            pp[i] = pow(k,i);
            if(pp[i]>200000000000005||pp[i]<-200000000000005) break;
            len++;
        }
    }

    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,ll> mymap;
    ll ans = 0;
    mymap[0] = 1;
    rep(i,1,n+1){
        //dd(i);de(sum[i]);
        rep(j,0,len+1){
            ll tmp = sum[i]-pp[j];
            ans+=mymap[tmp];
            //if(arr[i]==0&&tmp==0) continue;
            //dd(i);dd(j);dd(tmp);de(mymap[tmp]);
        }
        mymap[sum[i]]++;
        //mymap[0] = 1;
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define LL long long
const LL INF=1e14;

int n;
LL k,sum[100050];
map<LL,LL>x;
set<LL>a;
set<LL>::iterator it;

int main()
{
    while(~scanf("%d %lld",&n,&k))
    {
        x.clear();
        a.clear();
        sum[0]=0;
        for(int i=1;i<=n;i++) scanf("%lld",&sum[i]),sum[i]+=sum[i-1];
        a.insert(1);
        LL temp=k;
        for(int i=1;i<=60;i++)
        {
            if(temp>INF) break;
            a.insert(temp);
            temp*=k;
        }
        LL ans=0;
        x[0]=1;
        for(int i=1;i<=n;i++)
        {
            for(it=a.begin();it!=a.end();it++) ans+=x[sum[i]-*it];
            x[sum[i]]++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 101010;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int flag[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    if(k==1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1) ans++;
        cout<<ans<<endl;
        return 0;
    }
    if(k==-1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1||arr[i]==-1) ans++;
        cout<<ans<<endl;
        return 0;
    }

    rep(i,1,64){
        pp[i] = pow(k,i);
        if(pp[i]>100000000000005||pp[i]<-100000000000005) break;
        len++;
    }

    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,int> mymap;
    ll ans = 0;
    rep(i,1,n+1) if(arr[i]==1) ans++;

    mymap[0] = 1;
    rep(i,1,n+1){
        rep(j,1,len+1){
            ll tmp = sum[i]-pp[j];
            if(arr[i]==0&&tmp==0) continue;
            if(mymap.count(tmp)){
                //dd(i);dd(j);dd(tmp);de(mymap[tmp]);
                ans+=mymap[tmp];
            }
        }
        mymap[sum[i]]++;
        //mymap[0] = 1;
    }
    cout<<ans<<endl;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 1e5+5;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    /*if(k==1){
        pp[0] = 1;
        len = 0;
    }
    else if(k==-1){
        pp[0] = 1;
        pp[1] = -1;
        len = 1;
    }
    else if(k!=1 && k!=-1){
        len = 0;
        pp[0] = 1;
        rep(i,1,64){
            pp[i] = pow(k,i);
            if(pp[i]>200000000000005||pp[i]<-200000000000005) break;
            len++;
        }
    }*/
    set<ll> myset;
    myset.insert(1);
    ll tmp = 1;
    rep(i,1,60){
        tmp *= k;
        if(abs(tmp)>100000000000000) break;
        myset.insert(tmp);
    }

    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,ll> mymap;
    ll ans = 0;
    mymap[0] = 1;
    rep(i,1,n+1){
        //dd(i);de(sum[i]);
        /*rep(j,0,len+1){
            ll tmp = sum[i]-pp[j];
            ans+=mymap[tmp];
            //if(arr[i]==0&&tmp==0) continue;
            //dd(i);dd(j);dd(tmp);de(mymap[tmp]);
        }*/
        set<ll>::iterator it;
        for(it = myset.begin();it!=myset.end();++it) ans+=mymap[sum[i]-*it];
        mymap[sum[i]]++;
        //mymap[0] = 1;
    }
    cout<<ans<<endl;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 101010;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int flag[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    if(k==1){
        pp[0] = 1;
        len = 0;
    }
    else if(k==-1){
        pp[0] = 1;
        pp[1] = -1;
        len = 1;
    }
    else if(k!=1 && k!=-1){
        len = 0;
        pp[0] = 1;
        rep(i,1,64){
            pp[i] = pow(k,i);
            if(pp[i]>200000000000005||pp[i]<-200000000000005) break;
            len++;
        }
    }

    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,ll> mymap;
    ll ans = 0;

    mymap[0] = 1;
    rep(i,1,n+1){
        //dd(i);de(sum[i]);
        rep(j,0,len+1){
            ll tmp = sum[i]-pp[j];
            //if(arr[i]==0&&tmp==0) continue;
            if(mymap.count(tmp)){
                //dd(i);dd(j);dd(tmp);de(mymap[tmp]);
                ans+=mymap[tmp];
            }
        }
        mymap[sum[i]]++;
        //mymap[0] = 1;
    }
    cout<<ans<<endl;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define dd(x) cout << #x << "=" << x << ", "
#define mset(a,i) memset(a,i,sizeof(a))
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 101010;
const int MOD = 1e9 + 7;

ll arr[maxn] , sum[maxn];
int flag[maxn];
int main()
{
    ll n,k;
    ll pp[64];
    int len = 0;
    cin>>n>>k;

    rep(i,1,n+1) cin>>arr[i];

    if(k==1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1) ans++;
        cout<<ans<<endl;
        return 0;
    }
    if(k==-1){
        ll ans = 0;
        rep(i,1,n+1) if(arr[i]==1||arr[i]==-1) ans++;
        cout<<ans<<endl;
        return 0;
    }

    rep(i,1,64){
        pp[i] = pow(k,i);
        if(pp[i]>100000000000005||pp[i]<-100000000000005) break;
        len++;
    }

    rep(i,1,n+1) sum[i] = sum[i-1]+arr[i];

    map<ll,ll> mymap;
    ll ans = 0;
    rep(i,1,n+1) if(arr[i]==1) ans++;

    mymap[0] = 1;
    rep(i,1,n+1){
        //dd(i);de(sum[i]);
        rep(j,1,len+1){
            ll tmp = sum[i]-pp[j];
            if(arr[i]==0&&tmp==0) continue;
            if(mymap.count(tmp)){
                //dd(i);dd(j);dd(tmp);de(mymap[tmp]);
                ans+=mymap[tmp];
                //ans+=upper_bound()
            }
        }
        mymap[sum[i]]++;
        //mymap[0] = 1;
    }
    cout<<ans<<endl;
}