#include<bits/stdc++.h>
#define ll long long int
#define N 1009
using namespace std;
int preposition[1000001];
int main()
{
int t, x = 1;
cin >> t;
while(t--)
{
ll n, m, k;
cin >> n >> m >> k;
vector<ll>v(n);
v[0] = 1, v[1] = 2, v[2] = 3;
for(int i=3; i<n; i++)
v[i] = (v[i-1]+v[i-2]+v[i-3])%m + 1;
int position[N];
for(int i=0;i<N;i++)
position[i] = -1;
for(int i=n-1; i>=0; i--)
{
preposition[i] = position[v[i]];
position[v[i]] = i;
}
int cnt = 0, left = 0, right = -1;
bool flag[N] = {0};
for(int i=0; i<n; i++)
{
if(v[i] <= k && flag[v[i]] == false)
{
flag[v[i]] = true;
cnt++;
}
if(cnt == k)
{
right = i;
break;
}
}
cout << "Case " << x++ << ": ";
if(right == -1)
{
cout << "sequence nai" << "\n";
continue;
}
int minimum = right-left+1;
while(left < right)
{
if(v[left] > k)
left++;
else if(preposition[left] == -1)
break;
else if(preposition[left] <= right)
left++;
else
{
right = preposition[left];
left++;
}
minimum = min(minimum, (right-left+1));
}
cout << minimum << "\n";
}
return 0;
}
Comments
Post a Comment