The inversion number of an integer sequence a1, a2, . . . , an is the number of pairs (ai, aj) that satisfy i < j and ai > aj . Given n and the inversion number m, your task is to find the smallest permutation of the set { 1, 2, . . . , n }, whose inversion
number is exactly m.
A permutation a1, a2, . . . , an is smaller than b1, b2, . . . , bn if and only if there exists an integer k such that aj = bj for 1 <= j < k but ak < bk.
输入格式
The input consists of several test cases. Each line of the input contains two integers n and m. Both of the integers at the last line of the input is −1, which should not be processed. You may assume that 1 <= n <= 50000 and 0 <= m <= n(n − 1)/2.
输出格式
For each test case, print a line containing the smallest permutation as described above, separates the numbers by single spaces.
样例输入
5 9 7 3 -1 -1
样例输出
4 5 3 2 1 1 2 3 4 7 6 5
#include <stdio.h>
int n,m;
int main(){
int i,j,sp;
while(scanf("%d%d",&n,&m)==2) {
if(n==-1&&m==-1) break;
sp=1;
while(sp*(sp-1)/2<m) sp++;
int k=n-sp;
for(i=1; i<=k; i++) printf("%d ",i);
int first=m-(sp-1)*(sp-2)/2;
printf("%d ",first+1+k);
for(i=sp; i>1; i--) {
if(i==first+1) continue;
printf("%d ",i+k);
}
printf("%d\n",i+k);
}
return 0;
}