This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has npassenger seats, seat i can be occupied only by a participant from the city ai.
Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times).
After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city.
Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.
The first line contains three integers n, k and m (1 ≤ n ≤ 105, 2 ≤ k ≤ 109, 1 ≤ m ≤ 109).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105), where ai is the number of city, person from which must take seat i in the bus.
Output the number of remaining participants in the line.
4 2 5 1 2 3 1
12
1 9 10 1
1
3 2 10 1 2 1
0
In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
两个要点,一个是状态压缩,用一个pair来压缩连续的点,第二个是情况的覆盖,即首位均回文了,中间一块可以重复m次。
#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
int IT_MAX = 1 << 17;
const ll MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-8;
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<n;i++)
vector <pll> Vu;
int main() {
int N, K, M, i, j;
scanf("%d %d %d", &N, &K, &M);
for (i = 1; i <= N; i++) {
int t;
scanf("%d", &t);
if (!Vu.empty() && Vu.back().first == t) {
Vu.back().second++;
if (Vu.back().second == K) Vu.pop_back();
}
else Vu.emplace_back(t, 1);
}
ll s = 0;
for (auto it : Vu) s += it.second;
if (M == 1 || s == 0) return !printf("%lld\n", s);
s *= M;
int st = 0, en = (int)Vu.size() - 1;
while (st < en) {
if (Vu[st].first != Vu[en].first) return !printf("%lld\n", s);
if (Vu[st].second + Vu[en].second == K) {
s -= (ll)(M - 1) * K;
st++, en--;
continue;
}
if (Vu[st].second + Vu[en].second > K) s -= (ll)(M - 1)*K;
return !printf("%lld\n", s);
}
if (st > en) return !printf("0\n");
if (st == en) {
ll x = Vu[st].second * M;
if (x % K == 0) {
// cout<<"hi"<<endl;
return !printf("0\n");
}
else{
return !printf("%lld\n", s - (x / K)*K);
}
}
return 0;
}