牛客挑战赛46 D
include
include
include
include
include
include
include
include
include
include
include
include
//#include
include
include
include<unordered_map>
pragma GCC diagnostic error "-std=c++11"
pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)
pragma GCC target("avx","sse2")
define up(i,a,b) for(int i=a;i<b;i++)
define dw(i,a,b) for(int i=a;i>b;i--)
define upd(i,a,b) for(int i=a;i<=b;i++)
define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
define lson l,mid,root<<1
define rson mid+1,r,root<<1|1
define lrt root<<1
define rrt root<<1|1
const int N = 1e6 + 10;
const ull base1 = 1e9 + 7;
const ull base2 = 23333333;
ull hash1[N], hash2[N];
ull cnt[N];
int n, k;
void init() {
hash1[0] = 1; hash2[0] = 1;
upd(i, 1, 1e6) {
hash1[i] = hash1[i - 1] * base1;
hash2[i] = hash2[i - 1] * base2;
}
}
unordered_map<ull, int>mp1,mp2;
int main() {
n = read(), k = read();
init();
mp1[0] = mp2[0] = 1;
ull tmp1 = 0, tmp2 = 0; int a;
ll ans = 0;
upd(i, 1, n) {
a = read();
tmp1 -= cnt[a] * hash1[a];
tmp2 -= cnt[a] * hash2[a];
cnt[a]++; cnt[a] %= k;
tmp1 += cnt[a] * hash1[a];
tmp2 += cnt[a] * hash2[a];
if (mp1[tmp1] && mp2[tmp2]) {
ans += mp1[tmp1];
}
mp1[tmp1]++; mp2[tmp2]++;
}
printf("%lld\n", ans);
return 0;
}