Language: C++

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = (long long)1e9 + 7; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

const long long MAX = 210000;

void solve(vector<int>&, int, int);

void simulation(vector<int> &A, int N, int K, int b){
	// vector<int> s(N*2);
	vector<int> s;
	// vector<int> p(N+10,-1);
	int end = 0, ssize = 0;
	// cout<<"K = "<<K<<" b = "<<b<<endl;
	
	if(N*K - b >= 1000){
		vector<int> X;
		for(int i = b+1; i < N*K; i++){
			if(A[i%N] == A[b]) break;
			X.push_back(A[i%N]);
			
		}
		solve(X, X.size(), 1, X.size());
		return ;
	}
	for(int i = b; i < N*K; i++){// cout<<"X "<<A[i%N]<<" "<<p[A[i%N]]<<" end "<<end<<endl;
		bool flag = false;
		
		for(int j = 0; j < s.size(); j++){
			if(s[j] == A[i%N]){
				s.erase(s.begin()+j, s.end());
				flag = true;
				break;
			}
		}
		if(!flag) s.push_back(A[i%N]);
		/*
		if(p[A[i%N]] != -1){
			end = p[A[i%N]];
			p[A[i%N]] = -1;
		} else {
			cout<<"Y "<<end<<"<><><> "<<p[A[i%N]]<<endl;
			// s.push_back(A[i%N]);
			if(p[A[i%N]] != -1)p[A[i%N]] = min(p[A[i%N]], end);
			else p[A[i%N]] = end;
			
			s[end++] = A[i%N];
		}
		// if(i%N != N-1) continue;
		// cout<<"i = "<<i<<" X = "<<A[i%N]<<endl;
		for(int j = 0; j < end; j++){
			cout<<""<<s[j]<<" ";
		}
		cout<<endl;
		*/
	}
	// cout<<"SSS"<<endl;q
	for(int j = 0; j < s.size(); j++){
			cout<<""<<s[j]<<" ";
	}
	cout<<endl;
	
}

void solve(vector<int> &A, int N, int K){
	vector<vector<pair<int,int>>> storage(MAX+10);
	vector<int> used(N+10, -1), data(N+10,0);
	int maximum = 0, minimum = INF;
	
	for(int i = 0; i < N; i++){
		storage[A[i]].push_back(make_pair(i,0));
		maximum = max(maximum, A[i]);
		minimum = min(minimum, A[i]);
	}
	for(int i = minimum; i <= maximum; i++){
		for(int j = 0; j < storage[i].size(); j++){
			if(j == storage[i].size()-1){
				storage[i][j].second = storage[i][0].first + (N - storage[i][j].first);
			} else {
				storage[i][j].second = storage[i][j+1].first - storage[i][j].first;
			}
			data[storage[i][j].first] = storage[i][j].second+1;
		}
	}
	
	// for(int i = 0; i < N; i++){
		// cout<<"data "<<data[i]<<endl; 
	// }
	for(int i = 0, now = 0, con = 0;;){ //cout<<con<<" "<<now<<endl;
		if(used[now] != -1){
			int num = con - used[now];
			
			// for(;;){ cout<<"X "<<con<<" "<<num<<endl;
				// if(num+con < K)con += num;
				// else break;
			// }
			// cout<<"num "<<num<<" "<<con<<endl;
			// if((N*K-con)%num) con = N*K - (num-(N*K-con)%num);
			if((N*K-con)%num) con = N*K - (N*K-con)%num;
			else con = N*K;
			
			// cout<<"Y "<<con<<endl;
			now = con%N;
			
			for(;;){ //cout<<"Z "<<con<<endl; 
				if(data[now] + con > K){
					simulation(A,N,K,con);
					break;
				} else {
					con += data[now];
				}
			}
			break;
		}
		used[now] = con;
		
		if(data[now] + con > K){
			simulation(A,N,K,con);
			break;
		} else {
			con += data[now];
			now = (con)%N;
		}
	}
}

signed main(){
	// cin.tie(0);
	// ios::sync_with_stdio(false);
	// cout<<fixed<<setprecision(10);
	
	int N, K;
	vector<int> A;
	
	cin>>N>>K;
	
	A.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>A[i];
	}
	
	solve(A,N,K);
	
	
	return 0;
}

Compiler output:
a.cc: In function 'void simulation(std::vector<long long int>&, long long int, long long int, long long int)':
a.cc:37:22: error: too many arguments to function 'void solve(std::vector<long long int>&, long long int, long long int)'
   37 |                 solve(X, X.size(), 1, X.size());
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:21:6: note: declared here
   21 | void solve(vector<int>&, int, int);
      |      ^~~~~