An algorithm about cartesian product