let ``No.179 塗り分け`` () = let bitcount64 bit = let bit = (bit &&& 0x5555555555555555L)+(bit >>> 1 &&& 0x5555555555555555L) let bit = (bit &&& 0x3333333333333333L)+(bit >>> 2 &&& 0x3333333333333333L) let bit = (bit &&& 0x0F0F0F0F0F0F0F0FL)+(bit >>> 4 &&& 0x0F0F0F0F0F0F0F0FL) let bit = (bit &&& 0x00FF00FF00FF00FFL)+(bit >>> 8 &&& 0x00FF00FF00FF00FFL) let bit = (bit &&& 0x0000FFFF0000FFFFL)+(bit >>> 16 &&& 0x0000FFFF0000FFFFL) (bit &&& 0x00000000FFFFFFFFL)+(bit >>> 32 &&& 0x00000000FFFFFFFFL) let bc l = Seq.map bitcount64 l |> Seq.sum let arr = stdin.ReadLine().Split(' ') let H = int(arr.[0]) let W = int(arr.[1]) // '#' と '.' を数値 1 と 0 に変換 (ビットに変換する)。 let read() = stdin.ReadLine() |> Seq.mapi (fun i c -> (int64(c)%46L%34L) <<< (W-i-1)) |> Seq.reduce (|||) let bits = [ for i in 1..H -> read() ] let c = bc bits let p = if c = 0L then 0 elif c &&& 1L = 1L then 0 else let mask = (1L <<< W) - 1L let mv (h,w) = let blank = List.init h (fun _ -> 0L) let ``→`` b = List.map (fun n -> n >>> w) b let ``←`` b = List.map (fun n -> n <<< w &&& mask) b let ``↑`` b = (List.skip h b)@(blank) let ``↓`` b = (blank)@(List.take (H-h) b) let ``↗`` = ``↑`` >> ``→`` let ``↘`` = ``↓`` >> ``→`` let ``↙`` = ``↓`` >> ``←`` let ``↖`` = ``↑`` >> ``←`` let mask2 l = List.zip bits l |> List.map (fun (a,b) -> a &&& b) let f (a,b) = let c = mask2(a bits) in c,b c seq [ ``→``,``←`` ``←``,``→`` ``↑``,``↓`` ``↓``,``↑`` ``↗`` ,``↙`` ``↘`` ,``↖`` ``↙`` ,``↗`` ``↖`` ,``↘`` ] |> Seq.map f let check (la,lb) = let l = List.zip la lb |> List.map (fun (a,b) -> a ^^^ b) bits = l // 総当たり // 移動後のビットと移動後に消失したビットのXORを取れば初期配置と一致するかどうかで判別できる。 let b = seq { for h in 1..H do for w in 1..W -> h,w } |> Seq.collect mv |> Seq.exists check if b then 1 else 0 printfn "%s" ["NO";"YES";].[p] ``No.179 塗り分け`` ()