Consider an integer sequence consisting of N elements where

X1 = 1

X2 = 2

X3 = 3

Xi = (Xi−1 + Xi−2 + Xi−3)%M + 1                                  for i = 4 to N Find 2

values a and b so that the sequence (Xa Xa+1 Xa+2 . . . Xb−1Xb) contains all the integers from [1, K]. If there are multiple solutions then make sure (b − a) is as low as possible.

In other words, find the smallest subsequence from the given sequence that contains all the integers from 1 to K.

Consider an example where N = 20, M = 12 and K = 4.

The sequence is {1 2 3 7 1 12 9 11 9 6 3 7 5 4 5 3 1 10 3 3}.

The smallest subsequence that contains all the integers {1 2 3 4} has length 13 and is highlighted in the following sequence:

{1 2 3 7 1 12 9 11 9 6 3 7 5 4 5 3 1 10 3 3}.

Input

First line of input is an integer T (T < 100) that represents the number of test cases. Each case consists of a line containing 3 integers N (2 < N < 1000001), M (0 < M < 1001) and K (1 < K < 101). The meaning of these variables is mentioned above.

Output

For each case, output the case number followed by the minimum length of the subsequence. If there is no valid subsequence, output ‘sequence nai’ instead. Look at the sample for exact format.

Sample Input

2

20 12 4

20 12 8

Sample Output

Case 1: 13

Case 2: sequence nai

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[1000005],vis[1000005];
const int maxn=1000000+10;
int main()
{int t;scanf("%d",&t);int n,m,k,casenum=1,ans;a[1]=1;a[2]=2;a[3]=3;while(t--){ans=maxn; scanf("%d %d %d",&n,&m,&k);for(int i=4;i<=n;i++){a[i]=(a[i-3]+a[i-2]+a[i-1])%m+1;}memset(vis,0,sizeof vis);int l=1,r=1;int cnt=0;while(cnt<k&&r<=n){if(a[r]<=k){if(vis[a[r]]==0)cnt++;vis[a[r]]++;}r++;}if(cnt<k){printf("Case %d: sequence nai\n",casenum);casenum++;continue;}--r;ans=r-l+1;while(r<=n){ans=min(ans,(r-l+1));if(a[l]<=k){if(vis[a[l]]>1){vis[a[l]]--; l++; }else { r++; vis[a[r]]++; }}elsel++;}printf("Case %d: %d\n",casenum,ans);casenum++;}return 0;
}

A - Smallest Sub-Array相关推荐

  1. c语言subscripted_c语言。数组的问题。急!

    题目是这样的:Useasingle-subscriptedarraytosolvethefollowingproblem.Readin20numbers,eachofwhichisbetween10a ...

  2. codeforces每日5题(均1700)-第七天

    Multicolored Cars 题面翻译 [题目描述] 定义cntx(i)cnt_{x}(i)cntx​(i)表示到iii时刻xxx出现过的个数. 现在给出nnn个数a1,a2--ana_{1}, ...

  3. cf----2019-10-23( Posterized,Pair Of Lines,Greedy Arkady)

    一场游戏一场空,最终 最初都由我掌控,好像一身从容,不曾有狼狈伤痛,可深夜一个人该如何相拥? Professor Ibrahim has prepared the final homework for ...

  4. 老男孩上海校区Python面试题

    python面试题 第一章:python基础 数据类型: 1 字典: 1.1 现有字典 dict={'a':24,'g':52,'i':12,'k':33}请按字典中的 value 值进行排序? 1. ...

  5. 373. Find K Pairs with Smallest Sums (java,优先队列)

    题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...

  6. [CareerCup] 17.6 Sort Array 排列数组

    17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elem ...

  7. [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. 697. Degree of an Array 频率最高元素的最小覆盖子数组

    [抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...

  9. OpenGL Vertex Array

    原文地址 http://www.songho.ca/opengl/gl_vertexarray.html<-------这博客不错!貌似博主是个棒子. OpenGL Vertex Array R ...

  10. leetcode 697 Degree of an Array

    题目详情 Given a non-empty array of non-negative integers nums, the degree of this array is defined as t ...

最新文章

  1. BCH与BCE共享比特币之名
  2. linux标准IO实验,Linux系统编程(第三篇) 标准IO.pdf
  3. 深夜,学妹遇到了数据分析师生涯的第一个疑问
  4. java第二章_JAVA第二章知识点
  5. pulsar 容量_Pulsar 负载均衡设计
  6. 创建Web Service后,客户端不能调用的解决办法(提示:此方法只有在本地才可以使用)...
  7. 课程设计旅游景点咨询系统
  8. IGP-LAB-EIGRP-1
  9. 用javascript制作简易的QQ登录网页
  10. android mt4 macd,超准确的4小时MACD交易策略
  11. Unity C#编程优化——枚举
  12. SRE(运维工程师)一文详解技术体系和架构师成长之路
  13. 超码、候选码和主码有什么区别?
  14. How to deal with Imbalanced Datasets in PyTorch - Weighted Random Sampler Tutorial
  15. 中国科学院计算机所张浩,航天科技集团调研组到计算所交流
  16. 51nod2943 旅行者
  17. Fighting!Hack for wuhan 黑客马拉松等你来战
  18. 指令系统——CISC和RISC
  19. 计算机实时控制和过程控制,实时控制程序
  20. 小学语文哪课适合用计算机,小学语文新课心得体会

热门文章

  1. webview 个人小程序_微信小程序webview中,拉起小程序的微信支付 | 剑花烟雨江南...
  2. 组件之间数据信息传递方法
  3. Ffmpeg 将视频分解成多张图片以及多张图片合成视频
  4. 财报前瞻:Q4财报将至,靠收购能否提振IBM在云业务增长困境?
  5. C++ STL 关联式容器操作总结
  6. 全国计算机一级考ps吗,2016年3月全国计算机一级考试PS模拟试题及答案(一)
  7. 中国34个省(地区)简称
  8. 服务器的作用都有哪些
  9. java 斗地主_基于java实现斗地主代码实例解析
  10. 个人简历(markdown)