Problem: tourist (2018 quals)

The Facebook campus has **N** different attractions, numbered from 1 to **N**
in decreasing order of popularity. The name of the _i_th attraction is **Ai**,
a unique, non-empty string consisting of at most 20 characters. Each character
is either a lowercase letter ("a".."z"), uppercase letter ("A".."Z"), or digit
("0".."9").

Alex enjoys visiting the campus repeatedly for tours (including the free
food!). Each time he visits, he has time to see exactly **K** of the
attractions. To decide which **K** he'll see, he sorts the **N** attractions
in non-decreasing order of how many times he's already seen them before,
breaking ties in decreasing order of popularity, and then chooses the first
**K** attractions in the sorted list. In other words, he prioritizes seeing
attractions which he's seen the fewest number of times previously, but also
opts to see the most popular attractions out of the ones he's seen an equal
number of times.

Alex has visited the Facebook campus **V**-1 separate times already, and is
about to go for his **V**th visit. Given that he's always followed the rules
stated above, and that he'll continue to, he'd like to determine which **K**
attractions he'll see on his **V**th visit. He'd like to list them in
decreasing order of popularity (in other words, in the same relative order as
they appear in the given list of all **N** attractions).

### Input

Input begins with an integer **T**, the number of campuses. For each campus,
there is first a line containing the space-separated integers **N**, **K**,
and **V**. Then, **N** lines follow. The _i_th of these lines contains the
string **Ai**.

### Output

For the _i_th campus, print a line containing "Case #_i_: " followed by **K**
space-separated strings, the names of the attractions that Alex sees on his
**V**th visit, in decreasing order of popularity.

### Constraints

1 ≤ **T** ≤ 80  
1 ≤ **K** ≤ **N** ≤ 50  
1 ≤ **V** ≤ 1012  

### Explanation of Sample

In the first case, Alex saw the LikeSign on his first visit and the Arcade on
his second visit. On his third visit he sees the SweetStop as its the most
popular of the attractions he hasn't yet seen.

In the third and fourth cases, Alex sees {LikeSign, Arcade, SweetStop} on his
first visit, then {LikeSign, Arcade, SwagStore}, then {LikeSign, SweetStop,
SwagStore}.



Sample input:
6
4 1 3
LikeSign
Arcade
SweetStop
SwagStore
4 4 100
FoxGazebo
MPK20Roof
WoodenSculpture
Biryani
4 3 1
LikeSign
Arcade
SweetStop
SwagStore
4 3 3
LikeSign
Arcade
SweetStop
SwagStore
4 3 10
LikeSign
Arcade
SweetStop
SwagStore
2 1 1000000000000
RainbowStairs
WallOfPhones


Sample output:
Case #1: SweetStop
Case #2: FoxGazebo MPK20Roof WoodenSculpture Biryani
Case #3: LikeSign Arcade SweetStop
Case #4: LikeSign SweetStop SwagStore
Case #5: LikeSign Arcade SwagStore
Case #6: WallOfPhones



Code: // Tourist
// Solution by Jacob Plachta

#define DEBUG 0

#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <sstream>
using namespace std;

#define LL long long
#define LD long double
#define PR pair<int,int>

#define Fox(i,n) for (i=0; i<n; i++)
#define Fox1(i,n) for (i=1; i<=n; i++)
#define FoxI(i,a,b) for (i=a; i<=b; i++)
#define FoxR(i,n) for (i=(n)-1; i>=0; i--)
#define FoxR1(i,n) for (i=n; i>0; i--)
#define FoxRI(i,a,b) for (i=b; i>=a; i--)
#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Min(a,b) a=min(a,b)
#define Max(a,b) a=max(a,b)
#define Sz(s) int((s).size())
#define All(s) (s).begin(),(s).end()
#define Fill(s,v) memset(s,v,sizeof(s))
#define pb push_back
#define mp make_pair
#define x first
#define y second

template<typename T> T Abs(T x) { return(x<0 ? -x : x); }
template<typename T> T Sqr(T x) { return(x*x); }
string plural(string s) { return(Sz(s) && s[Sz(s)-1]=='x' ? s+"en" : s+"s"); }

const int INF = (int)1e9;
const LD EPS = 1e-12;
const LD PI = acos(-1.0);

#if DEBUG
#define GETCHAR getchar
#else
#define GETCHAR getchar_unlocked
#endif

bool Read(int &x)
{
	char c,r=0,n=0;
	x=0;
		for(;;)
		{
			c=GETCHAR();
				if ((c<0) && (!r))
					return(0);
				if ((c=='-') && (!r))
					n=1;
				else
				if ((c>='0') && (c<='9'))
					x=x*10+c-'0',r=1;
				else
				if (r)
					break;
		}
		if (n)
			x=-x;
	return(1);
}

int main()
{
		if (DEBUG)
			freopen("in.txt","r",stdin);
	// vars
	int T,t;
	int N,K;
	LL V;
	int i,j;
	char A[50][21];
	int vis[50];
	// testcase loop
	Read(T);
		Fox1(t,T)
		{
			// input
			scanf("%d%d%lld",&N,&K,&V);
				Fox(i,N)
					scanf("%s",&A[i]);
			// find visited attractions
			i=K*(V-1)%N;
				Fox(j,K)
					vis[j]=(i+j)%N;
			sort(vis,vis+K);
			// output
			printf("Case #%d:",t);
				Fox(i,K)
					printf(" %s",A[vis[i]]);
			printf("\n");
		}
	return(0);
}