链接:https://ac.nowcoder.com/acm/contest/897/J

来源:牛客网

Binary Number

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 32768K,其他语言65536K

64bit IO Format: %lld

题目描述

As a programmer, you are probably familiar with the binary representation of integers. That is, write an integer x as



a

i

2

i

∑ai2i, where each

a

i

ai is either 0 or 1. Particularly, n-digit binary number can be written as



n



1

i

0

a

i

2

i

∑i=0n−1ai2i, in which

a

n



1

an−1 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.

i

t

h

ith of each next T lines contains three integers n, m, k(

1



m



n



31

,

1



k



2

×

10

8

1≤m≤n≤31,1≤k≤2×108).

输出描述:

For each test case print the \(k\)-th smallest one.

示例1

输入

复制

2

5 2 2

5 3 3

输出

复制

10010

10110

题意:



思路:

我是反着来求的,要求第k小,我把所有情况sum算出来,令id=sum-k+1.然后求第id大的

从高位到低位,每一位我们考虑如果填了1,剩下还有种组合情况x,是否还够达到第id大,如果够就填1,不够就填0,并且id-=x。这样操作即可。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ ll getpc(ll m, ll n) //m 中 选 n 个
{
long long ans = 1;
for (long long k = 1; k <= n; k++) {
ans = (ans * (m - n + k)) / k;
}
return ans;
}
ll n, m, k;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout); int t;
gbtb;
cin >> t;
while (t--) {
cin >> n >> m >> k;
ll sum = getpc(n - 1, m - 1);
ll id = sum - k + 1ll;// 反过来求第id大的数。
ll temp = 0ll;
ll rm = m;// 剩余1的数量
for (ll i = n; i > 0; --i) {
ll x = getpc(i - 1, rm - 1);
if (x >= id && rm) {
cout << 1;
rm--;
} else {
id -= x;
cout << 0;
}
cout << flush;
}
cout << endl;
} return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

最新文章

  1. 模拟--poj1835宇航员的故事
  2. 个人Github-欢迎交流探讨
  3. 我的第一个 JSP (SSH) 个人网站【开源】
  4. Nginx localtion匹配规则
  5. 每日学习心得:UEditor样式被过滤无法显示问题
  6. .tostring()格式化大全
  7. 全面解释StringBuilder、StringBuffer和String的关系
  8. Android窗口为弹出框样式
  9. 第一章:1-06、&#160;试将TCP/IP和OSI的体系结构进行比较。讨论其异同之处?
  10. ubuntu将默认中文改成英文
  11. 8个实用的SVG工具,20 个有用的 SVG 工具,五款超实用的开源SVG工具
  12. sonar结合jenkins
  13. Pycharm中flask框架应用
  14. 英雄无敌HoMM3-死亡阴影SOD-神之苏醒WOG-封神NABI-MOD等相关文件
  15. ISP与DSP的区别【转】
  16. keepalived nginx 双机热备图文讲解
  17. php安装xdebug后var_dump输出没有格式化的问题
  18. php的$GLOBALS例子
  19. ubuntu18.10安装网易云音乐
  20. 【BZOJ 2749】 2749: [HAOI2012]外星人 (数论-线性筛?类积性函数)

热门文章

  1. 谈一谈 Android 的安全机制?
  2. Tensorflow所遇坑
  3. 工具栏对象GUI Status 与GUI Title
  4. freebsd 隐藏ssh版本号
  5. CentOS 升级 openSSH+ sh脚本自动运维
  6. 用linux主机做网关搞源地址转换(snat)
  7. yum源迁移(思路具体操作之后加)
  8. python 并发编程 多线程 Thread对象的其他属性或方法
  9. C++ 全局变量 静态变量 全局函数 静态函数
  10. etcd数据单机部署