Efficient technique for analyzing the planar periodic structures in layered media