Golfscript 58 56 55 35 31 30字节
假设三个输入已经在堆栈中,顺序为n,k,q
~1$(1$%3$),@),-{\2$+\%}%\)])\;
该解决方案假定我需要摆脱最终答案之外的所有问题。
怎么运行的
有关j(n,k,q)更多详细信息,请参见我的Python 3解决方案。
~ Read the inputs n, k, q
1$( Duplicate k, decrement
1$ Duplicate q
% (k-1)%q
3$), Create array [0..n+1]
@), Create array [0..q+1]
- Subtract the second array from the first,
leaving only [q+1..n+1]
{ }% Map the following statement onto [q+1..n+1].
The numbers from this array will be denoted i.
\ Swap i and r
2$+ Duplicate k, add to r
\ Swap r and i
% r mod i
\) Swap the leftover array from map with r, increment
] Put the whole stack into an array
) Remove the last member of the array, r
\; Pop the array, leaving only the result
编辑1:使用@Doorknob的建议(添加了+,以将所有输入输入数组)
以前,
\.(2$2$*1$4$%-{.5$3$*>!}{~)2$*1$/~)}while 4$3$*\-)\;\;\;\;
编辑2:根据Wiki的规则,添加了〜,并缩短了代码。谢谢@丹尼斯
以前,
[\.(2$2$*1$4$%-{.5$3$*>!}{~)2$*1$/~)}while 4$3$*\-)]+)\;
编辑3:实施了较短的算法。
以前,
~\.(2$2$*1$4$%-{.5$3$*>!}{~)2$*1$/~)}while 4$3$*\-)]-1=
编辑4:弄清楚我可以%用作地图。
以前,
~1$(1$%{1$4$<}{\)\2$+1$%}while)])\;
编辑5:次要编辑。改变2$以@使[0..q-1]和3$到2$取回k。咬了一口
以前,
~1$(1$%3$),2$),-{\3$+\%}%\)])\;