NC25729. Binary Number
描述
输入描述
The first line contains an integer number T, the number of test cases.
of each next T lines contains three integers n, m, k().
输出描述
For each test case print the $k$-th smallest one.
示例1
输入:
2 5 2 2 5 3 3
输出:
10010 10110
C++(g++ 7.5.0) 解法, 执行用时: 95ms, 内存消耗: 424K, 提交时间: 2022-10-18 09:35:26
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 5e5 + 10, mod = 1e9 + 7; void solve() { int n, m, k; cin >> n >> m >> k; string num = "1"; for(int i = 0; i < n - m; i ++)num += "0"; for(int i = 1; i < m; i ++)num += "1"; while(--k) next_permutation(num.begin(), num.end()); cout << num << endl; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T = 1; cin >> T; while(T--)solve(); return 0; }
C++14(g++5.4) 解法, 执行用时: 95ms, 内存消耗: 488K, 提交时间: 2019-05-15 15:02:05
#include <bits/stdc++.h> using namespace std; int a[35]; int main() { int T, t; scanf("%d", &T); while (T--) { int n, m, k, cnt = 0; scanf("%d%d%d", &n, &m, &k); a[0] = 1; for (int i = 1; i <= n - m; i++) a[i] = 0; for (int i = n - m + 1; i < n; i++) a[i] = 1; do { cnt++; if (cnt == k) break; } while (next_permutation(a, a + n)); for (int i = 0; i < n; i++) printf("%d", a[i]); printf("\n"); } }
C++11(clang++ 3.9) 解法, 执行用时: 76ms, 内存消耗: 504K, 提交时间: 2020-09-05 15:28:13
#include<bits/stdc++.h> using namespace std; int main() { int t,n,m,k; char f[35]; cin>>t; while(t--){ cin>>n>>m>>k; n--,m--,k--; for(int i=0;i<n;i++){ f[i]='0'; } for(int i=n-1;i>=n-m;i--){ f[i]='1'; } f[n]='\0'; while(k>0){ k--; next_permutation(f,f+n); } cout<<1<<f<<endl; } return 0; }