Golfscript、58 56 55 35 31 30バイト
3つの入力が既に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:ウィキのルールに従って〜を追加し、コードを短縮しました。ありがとう@デニス
以前は、
[\.(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$+\%}%\)])\;