Question meaning: For a k-ary number x, define d(x) as the k-ary representation of the sum of x digits. If the result exceeds one digit, continue to execute.
d(3504)=d(3+ 5+0+4)=d(15)=d(1+5)=d(6)=6
If d(x)=b, then x is called lucky.
2<=k<=1e9, 0< =b<k , 1<=n<=1e5.
Given the n digits [a[1], a[2]...a[n]], and b in the k base, ask how many children are there Is the string lucky?
Because the final value of each x is 1<=val<k. Then knowing the value x of [i, j-1], it is easy to know the value of [i+1, j] y.
val =x+a[j] val is up to 2*k-2 If val>=k then carry, the tens place is 1, the ones place is val-k. The final result: 1+val-k<=k-1.
Directly Enumeration calculation O(n^2) TLE.
(a[L]+a[L+1]+..a[R]) Each time if two numbers add more than k, a 1 and remainder will be used k part instead.
Then d[L,R] is equivalent to the value of (a[L]+a[L+1]+.a[R]) %(k-1) . (If the last remainder is 0, Then the result is k-1)
Now find the value of the prefix p[i]%(k-1).
Enumerate the right endpoint r, if you want to know how many left endpoints L there are, satisfy d[L, R]==b .then
d[L-1]+b = d[r] (mod k-1). Subtract b on both sides.
d[L-1] = d[r]-b (modk-1)
d(3504)=d(3+ 5+0+4)=d(15)=d(1+5)=d(6)=6
If d(x)=b, then x is called lucky.
2<=k<=1e9, 0< =b<k , 1<=n<=1e5.
Given the n digits [a[1], a[2]...a[n]], and b in the k base, ask how many children are there Is the string lucky?
Because the final value of each x is 1<=val<k. Then knowing the value x of [i, j-1], it is easy to know the value of [i+1, j] y.
val =x+a[j] val is up to 2*k-2 If val>=k then carry, the tens place is 1, the ones place is val-k. The final result: 1+val-k<=k-1.
Directly Enumeration calculation O(n^2) TLE.
(a[L]+a[L+1]+..a[R]) Each time if two numbers add more than k, a 1 and remainder will be used k part instead.
Then d[L,R] is equivalent to the value of (a[L]+a[L+1]+.a[R]) %(k-1) . (If the last remainder is 0, Then the result is k-1)
Now find the value of the prefix p[i]%(k-1).
Enumerate the right endpoint r, if you want to know how many left endpoints L there are, satisfy d[L, R]==b .then
d[L-1]+b = d[r] (mod k-1). Subtract b on both sides.
d[L-1] = d[r]-b (modk-1)
Note that when b==0, only the interval of a[i]==0 can be taken.
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+5; ll k,b,n,a[N],pre[N]; map<ll,ll> cnt; intmain() { ios::sync_with_stdio(false); cin.tie(0); cin>>k>>b>>n; ll res=0,z=0,t=0; for(int i=1;i<=n;i++) { cin>>a[i]; pre[i]=(pre[i-1]+a[i])%(k-1); if(a[i]==0) t++,z+=t; else t=0; } if(b==0) { cout<<z<<'\n'; return 0; } cnt[0]++; for(int i=1;i<=n;i++) { ll val=(pre[i]-b+k-1)%(k-1); res + = cnt [val]; cnt[pre[i]]++; } if(b==k-1) res- = z; cout<<res<<'\n'; return 0; }