https://vjudge.net/problem/UVA-11536
#include<bits/stdc++.h>
using namespace std;
const int maxn =1000005;
int n, m, k;
int x[maxn];
map<int,int>cnt;
void gendata(){
x[1]=1; x[2]=2; x[3]=3;
for(int i=4;i<=n;i++){
x[i]=(x[i-1]+x[i-2]+x[i-3])%m +1;
}
}
int main(){
int t, kcase=0;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
int ans=INT_MAX;// 最长不超过n
gendata();
cnt.clear();
int pos=1;
for(int i=1;i<=n;i++){
if(x[i]>k) continue;//不可能以i 结尾
cnt[x[i]]++;
//或的顺序不能搞乱了
while(pos<=i&&( x[pos]>k|| cnt[x[pos]]>1)){
if(x[pos]<=k) cnt[x[pos]]--;
pos++;
}
if(cnt.size()==k) ans=min(ans,i-pos+1);
}
if(ans == INT_MAX) printf("Case %d: sequence nai\n", ++kcase);
else printf("Case %d: %d\n", ++kcase, ans);
}
return 0;
}
#include<bits/stdc++.h>
#define _rep(i,a,b) for(int i = (a); i<=(b); i++)
const int maxn = 1000000 + 1000;
int n, m, k, A[maxn];
// 用长度为t的滑窗尝试
bool solve(int t) {
int tot = 0, cnt[1000 + 50]; //tot:存在于滑窗的小于等于k的元素的个数,cnt[i]:i在滑窗中个数
memset(cnt, 0, sizeof(cnt));
_rep(i, 1, n) {
if (tot == k) return true;
if (A[i ] <= k){
if(cnt[A[i ]]== 0) tot++;
cnt[A[i ]]++;
}
if (i > t && A[i - t ] <= k ){
cnt[A[i-t]]--;
if(cnt[A[i - t ]] == 0) tot--;
}
}
return false;
}
int main() {
int T, kase = 0;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &m, &k);
A[1] = 1; A[2] = 2; A[3] = 3;
_rep(i, 4, n) A[i] = (A[i - 1] + A[i - 2] + A[i - 3]) % m + 1;
// 二分滑窗长度
int L = 1, R = n ;
while (L < R) {
int M = L + (R - L) / 2;
if (solve(M)) R = M;
else L = M+1;
}
if (solve(L)) printf("Case %d: %d\n",++kase, L);
else printf("Case %d: sequence nai\n",++kase);
}
return 0;
}