Haskell, 495 442442 402
Who needs formatting anyway?
I might be able to shave off a few more bytes, but I'll do that later since I'm a lazy bastard.
import Data.List
d=1:1:1%2;f=filter;p1%2
f=filter
p 0="0";p0="0"
p 1="1";p1="1"
p n=p(n`div`2div n 2)++show++p(mod n 2)
u[al=length
u z[a,b]=headb]=sort.head.dropWhile((<b).lengthl)$map$m(nub.foldl1 intersect.y"1"y(reversetail.p$a+16p$31-a).(`map`[y"0"(z>>=p)z,z#1`m`[d,f(v.group.sort.p)[1..],d]z#1,y(z>>=p)z]).take)z
v[a]=1>2;vw=(=='0')
v[a]=1>2
v x=all(tall w.lengthtail.p.l)x;t 1=1<2;t n|n`mod`2==1=1>2|1<2=t$n`div`2x
y[c]w=mapy fstx=m snd.f((==c).snd)w.(`zip`wfst);z=[0..];x#n=n`take`x++dropzip x
x#n=n`take`x++drop(n+n+1)x#(n+2)
n%m=d!!(m-d!!n)+d!!(m-d!!(n-1)):m%(m+1);main=interact$show
main=interact$show.uu[0.map.].m read.words
m=map
It even performs faster than I thought it wouldreasonably well. Here are a couple of OP's examples:
Flonk@home:~>echo 15 10 | codegolf
[10,23,40,57,58,139,147,149,212,228]
Flonk@home:~>echo 9 12 | codegolf
[3,8,10,14,19,20,21,23,24,31,37,40]
Flonk@home:~>echo 2 10 | codegolf
[0,3,4,5,10,11,12,13,14,21]
Flonk@home:~>echo 12 6 | codegolf
[2,4,5,6,9,10]