This problem is in Contest(s) below:
Contest 1233:算法竞赛入门经典第2版—第1章第2章练习