国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Subset sequence

2019-11-08 01:36:11
字體:
來源:轉載
供稿:網友
Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is defined as a array of a non-empty subset. Sort all the subset sequece of An in lexicography order. Your task is to find the m-th one. InputThe input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20, 0< m<= the total number of the subset sequence of An ).OutputFor each test case, you should output the m-th subset sequence of An in one line.Sample Input
1 12 12 22 32 43 10Sample Output111 222 12 3 1我的思路 : 看了標準代碼懵逼好久最終搞定。

按照數字大小順序排序,

可以發現,輸出的序列第一個是m除以n個數的排序數,比如2有2種組合,3每次有5種組合,如果組合數以數組a(n)表示,則有a(n)=(n-1)*a(n-1)+1的規律

剩下的依序輸出,比如輸入的n和m是3 10,則第一個輸出的應當是10/5,考慮到3有5種組合,但是第五種是以1開頭的最后一種序列,所以,m=10時,其實是2開頭的最后一種序列。若是m=11,雖然m/5=2,但是已經是3開頭的序列了,所以,計算第一個輸出的數時,應該是t=m/n組合數+m%n組合數?1:0;意思是如果求余不為0,加一,這樣就符合實際情況。以此類推,每當輸出一個t,應當將t從常數數組內去除,也就是將t之后的數全部提前一位。

以下是我的代碼:#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<iomanip>#include<string.h>using namespace std;int main( ){   int a[21] = {0}, i;   long long p[21] = {0}, n, m, t;   while (cin >> n >> m)   {        for (i = 0; i < 21; i++)        a[i] = i;        p[1] = 1;        for (i = 2; i < 21; i++)        p[i] = p[i-1] * (i - 1) + 1; //計算出每個數字有幾種排列,如N=1時,數字1開頭有1種排列;        //N=2時,數字1開頭有2種排列;N=3時, 有5種排列。         while (n-- && m)       {           t = m / p[n+1] + (m % p[n+1] ? 1 : 0); //計算出該m是在哪一組           cout << a[t];           for (i = t; i <= n; i++)            a[i] = a[i+1];//刪去已經排列那個數字           m = m - ((t - 1) * p[n + 1] + 1); // 去掉前面的小于t開頭的組合,且將去掉一個僅有t的集合            if (m==0)            cout << "/n";           else cout << " ";       }   }    return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江孜县| 东至县| 亳州市| 沧源| 宜州市| 镇雄县| 乌拉特前旗| 六安市| 枣强县| 垦利县| 会宁县| 雅江县| 太谷县| 宜丰县| 防城港市| 深水埗区| 平潭县| 盘山县| 湘西| 富蕴县| 武定县| 金坛市| 饶阳县| 册亨县| 攀枝花市| 金秀| 深州市| 安仁县| 岳普湖县| 民丰县| 高雄市| 吐鲁番市| 苏州市| 南涧| 尼玛县| 达孜县| 太谷县| 孝感市| 稷山县| 抚松县| 舞阳县|