HDOJ1003: add a optimized version