HDU 4350 Card
打表找规律,比赛应付了一下,其实还可以把内存再优化一半掉,下面的0都是手动填充的,可以优化掉
题意:
T个测试数据
下面52个数字表示 从栈顶到栈底的52个数
n l r表示 从栈顶下数 [l,r] 这个区间的数都放到栈顶
这样n次操作后的结果
这里可以猜想 n 操作应该是有重复的,也就是取模
n%MOD , MOD是个关于 l ,r 的表达式
省时间就直接打表了
#include<stdio.h> int s1[53],s2[53],st2; void outstack(int l,int r){ st2=1; int i; for(i=l;i<=r;i++) s2[st2++]=s1[i]; for(i=l;i+r-l+1<=52;i++) s1[i]=s1[i+r-l+1]; st2=1; for(;i<=52;i++) s1[i]=s2[st2++]; } int mod[52][52]={52,26,52,13,52,26,52,13,52,26,52,13,4,26,52,13,52,26,52,13,52,26,52,13,52,2,52,13,52,26,52,13,52,26,52,13,52,26,4,13,52,26,52,13,52,26,52,13,52,26,52,1,0,51,51,17,51,51,17,51,51,17,51,51,17,51,51,17,51,3,17,51,51,17,51,51,17,51,51,17,51,51,17,51,51,17,3,51,17,51,51,17,51,51,17,51,51,17,51,51,17,51,51,1,0,0,50,25,50,25,10,25,50,25,50,5,50,25,50,25,10,25,50,25,50,5,50,25,50,25,2,25,50,25,50,5,50,25,50,25,10,25,50,25,50,5,50,25,50,25,10,25,50,25,50,1,0,0,0,49,49,49,49,49,49,7,49,49,49,49,49,49,7,49,49,49,49,49,49,7,49,49,49,49,49,49,7,49,49,49,49,49,49,7,49,49,49,49,49,49,7,49,49,49,49,49,49,1,0,0,0,0,48,24,16,12,48,8,48,6,16,24,48,4,48,24,16,3,48,8,48,12,16,24,48,2,48,24,16,12,48,8,48,3,16,24,48,4,48,24,16,6,48,8,48,12,16,24,48,1,0,0,0,0,0,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,1,0,0,0,0,0,0,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,2,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,23,46,1,0,0,0,0,0,0,0,45,45,15,45,9,15,45,45,5,9,45,15,45,45,3,45,45,5,45,9,15,45,45,15,9,45,5,45,45,3,45,45,15,45,9,5,45,45,15,9,45,15,45,45,1,0,0,0,0,0,0,0,0,44,22,44,11,44,22,44,11,44,22,4,11,44,22,44,11,44,22,44,11,44,2,44,11,44,22,44,11,44,22,44,11,4,22,44,11,44,22,44,11,44,22,44,1,0,0,0,0,0,0,0,0,0,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,1,0,0,0,0,0,0,0,0,0,0,42,21,14,21,42,7,6,21,14,21,42,7,42,3,14,21,42,7,42,21,2,21,42,7,42,21,14,3,42,7,42,21,14,21,6,7,42,21,14,21,42,1,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,1,0,0,0,0,0,0,0,0,0,0,0,0,40,20,40,10,8,20,40,5,40,4,40,10,40,20,8,5,40,20,40,2,40,20,40,5,8,20,40,10,40,4,40,5,40,20,8,10,40,20,40,1,0,0,0,0,0,0,0,0,0,0,0,0,0,39,39,13,39,39,13,39,39,13,39,39,13,3,39,13,39,39,13,39,39,13,39,39,13,39,3,13,39,39,13,39,39,13,39,39,13,39,39,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,38,19,38,19,38,19,38,19,38,19,38,19,38,19,38,19,38,19,2,19,38,19,38,19,38,19,38,19,38,19,38,19,38,19,38,19,38,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,36,18,12,9,36,6,36,9,4,18,36,3,36,18,12,9,36,2,36,9,12,18,36,3,36,18,4,9,36,6,36,9,12,18,36,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,35,35,35,35,7,35,5,35,35,7,35,35,35,5,7,35,35,35,35,7,5,35,35,35,7,35,35,5,35,7,35,35,35,35,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,34,17,34,17,34,17,34,17,34,17,34,17,34,17,34,17,2,17,34,17,34,17,34,17,34,17,34,17,34,17,34,17,34,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,33,11,33,33,11,33,33,11,33,3,11,33,33,11,33,33,11,33,33,11,3,33,11,33,33,11,33,33,11,33,33,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,16,32,8,32,16,32,4,32,16,32,8,32,16,32,2,32,16,32,8,32,16,32,4,32,16,32,8,32,16,32,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30,15,10,15,6,5,30,15,10,3,30,5,30,15,2,15,30,5,30,3,10,15,30,5,6,15,10,15,30,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,28,14,28,7,28,14,4,7,28,14,28,7,28,2,28,7,28,14,28,7,4,14,28,7,28,14,28,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,27,27,9,27,27,9,27,27,3,27,27,9,27,27,9,27,27,3,27,27,9,27,27,9,27,27,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,13,26,13,26,13,26,13,26,13,26,13,2,13,26,13,26,13,26,13,26,13,26,13,26,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25,25,25,25,5,25,25,25,25,5,25,25,25,25,5,25,25,25,25,5,25,25,25,25,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,12,8,6,24,4,24,3,8,12,24,2,24,12,8,3,24,4,24,6,8,12,24,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22,11,22,11,22,11,22,11,22,11,2,11,22,11,22,11,22,11,22,11,22,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,21,21,7,21,21,7,3,21,7,21,21,7,21,3,7,21,21,7,21,21,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,10,20,5,4,10,20,5,20,2,20,5,20,10,4,5,20,10,20,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,9,6,9,18,3,18,9,2,9,18,3,18,9,6,9,18,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,1,0,0,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7,7,7,7,7,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,3,2,3,6,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,5,5,5,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,2,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; int main(){ int i,n,T,Cas=1,l,r;scanf("%d",&T); while(T--) { for(i=52;i>=1;i--)scanf("%d",&s1[i]); scanf("%d%d%d",&n,&l,&r); n%=mod[52-r][52-l]; while(n--) outstack(53-r,53-l); printf("Case #%d: ",Cas++); for(i=52;i>1;i--)printf("%d ",s1[i]); printf("%d\n",s1[i]); } return 0; }
#include<stdio.h> #define N 53 int s1[N],s2[N],st2; void outstack(int l,int r){ st2=1; int i; for(i=l;i<=r;i++) s2[st2++]=s1[i]; for(i=l;i+r-l+1<=52;i++) s1[i]=s1[i+r-l+1]; st2=1; for(;i<=52;i++) s1[i]=s2[st2++]; } bool Is(){ for(int i=1;i<=52;i++)if(i!=s1[i])return false; return true; } int main(){ FILE *fp; fp=fopen("HDU 4350","w"); for(int i=1;i<=52;i++)s1[i]=i; for(int i=1;i<=52;i++) for(int j=1;j<=52;j++) if(i>j)fprintf(fp,"0,"); else for(int k=1;;k++) { outstack(i,j); if(Is()) { fprintf(fp,"%d,",k); break; } } fclose(fp); }