UVA-11321 - Sort! Sort!! And Sort!!!

  • UVa Online Judge 解題結果請於 Submit 後,參閱 uHunt
  • 如果你有任何建議與指教,歡迎於下方留言一起討論喔!
  • 相同題目:ZOJ-d750 / NCTU-11069

題意概要

題目給定兩個整數 (), (),你要依照某些規則排序 個整數。先利用每個數字除以 的餘數由小到大排,若排序中比較的兩數為一奇一偶且兩數除以 的餘數相等,則奇數要排在偶數前面。若兩奇數除以 餘數大小相等,則原本數值較大的奇數排在前面。同樣的,若兩偶數除以 餘數大小相等,則較小的偶數排在前面。至於負數的餘數計算和 C 語言裡的定義相同,即負數的餘數絕對不會大於零。例如 -100 MOD 3 = -1, -100 MOD 4 = 0 依此類推。


Input

The input file contains sets of inputs. Each set starts with two integers () and () which denotes how many numbers are within this set. Each of the next lines contains one number each. These numbers should all fit in 32-bit signed integer. Input is terminated by a line containing two zeroes.


Output

For each set of input produce lines of outputs. The first line of each set contains the value of and . The next lines contain numbers, sorted according to the rules mentioned above. Print the last two zeroes of the input file in the output file also.


Sample Input

15 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0 0

Sample Output

15 3
15
9
3
6
12
13
7
1
4
10
11
5
2
8
14
0 0
profile-image
David Lu
Hello, I'm David Lu. I am a graduate student in Department of Computer Science at National Chiao Tung University, Taiwan. I am in the Networking and Sensing Systems (NSS) Lab at NCTU. If you have any question, please feel free to contact with me.
comments powered by Disqus