[NYIST32]组合数(状压,枚举,暴力)

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32

求n个数中挑出r个数字的所有情况,最后倒序输出所有情况。

状压枚举所有情况就是了,判断长度是否符合条件。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rll(a) scanf("%I64d", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(LL i = 0; i < (len); i++)
 49 #define For(i, a, len) for(LL i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Fuint(a) memset((a), 0x7f7f, sizeof(a))
 53 #define lrt rt << 1
 54 #define rrt rt << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onenum(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long Uint;
 62 typedef pair<LL, LL> pii;
 63 typedef pair<string, LL> psi;
 64 typedef map<string, LL> msi;
 65 typedef vector<LL> vi;
 66 typedef vector<LL> vl;
 67 typedef vector<vl> vvl;
 68 typedef vector<bool> vb;
 69 
 70 const int maxn = 15;
 71 
 72 int a[maxn];
 73 int t[maxn];
 74 int n, r, cnt;
 75 vector<vi> v;
 76 
 77 int main() {
 78     // FRead();
 79     Rep(i, maxn+1) a[i] = i + 1;
 80     while(~scanf("%d%d", &n, &r)) {
 81         int nn = 1 << n;
 82         vi p; v.cl();
 83         For(i, 1, nn) {
 84             cnt = 0;
 85             Rep(j, n) {
 86                 if(i & (1 << j)) t[cnt++] = a[j];
 87             }
 88             if(cnt == r) {
 89                 p.cl();
 90                 for(int j = cnt - 1; j >= 0; j--) p.pb(t[j]);
 91                 v.pb(p);
 92             }
 93         }
 94         sort(v.begin(), v.end());
 95         for(int i = v.size() - 1; i >= 0; i--) {
 96             Rep(j, r) {
 97                 printf("%d", v[i][j]);
 98             }
 99             printf("
");
100         }
101     }
102     RT 0;
103 }
原文地址:https://www.cnblogs.com/kirai/p/5603229.html