奶牛编号

题目描述

作为一个神秘的电脑高手,Farmer John 用二进制数字标识他的奶牛。
然而,他有点迷信,标识奶牛用的二进制数字,必须只含有K位“1” (1 <= K <= 10)。 当然,每个标识数字的首位必须为“1”。
FJ按递增的顺序,安排标识数字,开始是最小可行的标识数字(由“1”组成的一个K位数)。
不幸的是,他没有记录下标识数字。请帮他计算,第N个标识数字 (1 <= N <= 10^7)。

输入

第1行:空格隔开的两个整数,N和K。   

输出

如题,第N个标识数字  

输入样例

7 3
输出样例

10110

.
.
.
.
.
分析
首先第一位必须是1,所以后面还有k-1个1要放。k=1的时候直接特判。
在长度为len的01串中放入k-1个1的方案数就是C(len,k-1)
如果n比C(len,k-1)大,那么len++,n-=C(len,k-1)。继续找下一个。

.
.
.
.
.
.
程序:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

long long n,k,c[5005][35];

int main()
{
	for (int i=0;i<=5000;i++)
		c[i][0]=1;
	for (int i=1;i<=5000;i++)
	{
		for (int j=1;j<=30;j++)
			c[i][j]=c[i-1][j]+c[i-1][j-1];
	}
	scanf("%lld%lld",&n,&k);
	
	if (n==1)
	{
		for (int i=1;i<=k;i++)
			putchar('1');
		return 0;
	}
	
	if (k==1)
	{
		putchar('1');
		for (int i=1;i<n;i++)
			cout<<0;
		return 0;
	}
	
	long long tj=k;
	while (c[tj][k]<=n) tj++;
	tj--;
	k--;
	putchar('1');
	for (int i=k;i<tj;i++)
		n-=c[i][k];
	for (int i=1;i<=tj;i++)
	{
		if (k==0) putchar('0'); else
		if (c[tj-i][k]>=n) putchar('0'); else
		{
			putchar('1');
			n-=c[tj-i][k];
			k--;
		}	
	}
	return 0;
}
题目描述 Farmer John wants to take a picture of his entire herd of N (1 <= N <= 100,000) cows conveniently numbered 1..N so he can show off to his friends. On picture day, the cows run to form a single line in some arbitrary order with position i containing cow c_i (1 <= c_i <= N). Farmer John has his own ideas about how the cows should line up. FJ thinks cow i may stand only to the left of cow i+1 (for all i, 1 <= i <= N-1) and that cow N may only stand to the left of Cow 1. Of course, no cow will stand to the left of the first (leftmost) cow in the line. The cows are hungry for the promised post-photo dinner, so Farmer John wants to take the picture as quickly as possible. Cows are not great at following directions, so he will only choose a pair of adjacent cows and have them switch places once per minute. How quickly is Farmer John able to get them into some acceptable order? Consider a set of 5 cows whose initial lineup looks like this: Left Right 3 5 4 2 1 He can first swap the second pair of cows: 3 4 5 2 1 and then swap the rightmost pair: 3 4 5 1 2 to yield an acceptable lineup that required but two minutes of cow swapping. 输入格式 Line 1: A single integer: N Lines 2..N+1: Line i+1 contains the number of the i-th cow in line: c_i 输出格式 Line 1: The minimum amount of time, in minutes, that it takes Farmer John to get the cows into some appropriate order. 隐藏翻译 题意翻译 FJ希望给他的 N ( 1 ≤ N ≤ 100,000 ) 只奶牛拍照,这样他就可以向朋友们炫耀他的奶牛。这 N 只奶牛被标号为 1...N。 在照相的那一天,奶牛们排成了一排。其中第i个位置上是标号为 ci​​ ( 1 ≤ c​i​​ ≤ N ) 的奶牛。对于奶牛的站位,FJ有他自己的想法。 FJ是这么想的,标号为 i ( 1 ≤ i ≤ n−1 ) 的奶牛只能站在标号为 i+1 的奶牛的左边,而标号为 N 的奶牛只能站在标号为 1 的奶牛的左边。当然,没有牛可以站在队列中最左边的奶牛的左边了。(也就是说,最左边的奶牛编号是随意的。) 这些奶牛都非常饿,急切希望吃到FJ所承诺的拍照后的大餐,所以FJ想尽快的拍照。奶牛们的方向感非常不好,所以FJ每一分钟只可以选择相邻的两只奶牛然后让他们交换位置。FJ最少需要多少时间才能使奶牛站成一个可以接受的序列? 比方说一个有5只奶牛的例子,一开始的序列是这样的:3 5 4 2 1 第一分钟,FJ可以交换 5 号奶牛和 4 号奶牛,交换后队列变为:3 4 5 2 1 第二分钟,FJ可以交换 1 号奶牛和 2 号奶牛,之后序列就变成了这样:3 4 5 1 2 这样,只用了 2 分钟,序列就变为了FJ所希望的那样。 输入输出样例 输入 #1复制 \n5\n3\n5\n4\n2\n1\n\n 输出 #1复制 \n2\n
07-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值