列表

详情


NC25729. Binary Number

描述

    As a programmer, you are probably familiar with the binary representation of integers. That is, write an integer x as , where each is either 0 or 1. Particularly, n-digit binary number can be written as , in which must equal to 1.
    This time, to test your mastery of binary numbers, Leg Han raises a problem to you.
    Among all n-digit binary numbers whose amount of 1 is m, please print the k-th smallest one.
    It is guaranteed that k is legal.

输入描述

    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;
}

上一题