题目描述

Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitious, and only labels cows with binary numbers that have exactly K "1" bits (1 <= K <= 10). The leading bit of each label is always a "1" bit, of course. FJ assigns labels in increasing numeric order, starting from the smallest possible valid label -- a K-bit number consisting of all "1" bits. Unfortunately, he loses track of his labeling and needs your help: please determine the Nth label he should assign (1 <= N <= 10^7).

FJ给他的奶牛用二进制进行编号,每个编号恰好包含K 个"1" (1 <= K <= 10),且必须是1开头。FJ按升序编号,第一个编号是由K个"1"组成。

请问第N(1 <= N <= 10^7)个编号是什么。

输入输出格式

输入格式:

  • Line 1: Two space-separated integers, N and K.

输出格式:

输入输出样例

输入样例#1:

7 3
输出样例#1:

10110 

题解:
20暴力
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,k,pos,a[];
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=+k;i++)a[i]=;
pos=;
for(int i=;i<=n;i++){
int p;
for(int j=+k;j>=pos-;j--)
if(a[j]==&&a[j+]==){
p=j+;break;
}
if(p==pos){
pos--;memset(a,,sizeof(a));
a[pos]=;
for(int j=+k;j>=;j--)a[j]=;
}else {
a[p]=;a[p-]=;
}
}
for(int i=pos;i<=+k;i++)
printf("%d",a[i]);
return ;
}

完全不懂这个AC代码

在洛谷上问了写这个代码的大佬..今天大佬给我讲了一下!

太神啦!这个题的题解网上都是一坨..(*゜ロ゜)ノ实在不愿意看..

(逃ヽ(゚∀゚*)ノ━━━ゥ♪

a[i]表示的是第i个1的位置,初始值为(按阳历来说)第一个1的位置是1,第二个

1的位置是2,第三个1的位置是3.二进制也就是1 1 1 。

我们开始找啦,样例是升序的第7个,我把1--7都列出来。

1 1 1

1 0 1 1

1 1 0 1

1 1 1 0

1 0 0 1 1

1 0 1 0 1

1 0 1 1 0

发现的规律是,每次将二进制串的从右往左数的第1个前面为0的1往左移一位。

这个1右边的1全部靠后。因为二进制串的大小取决于1的位置,尽量别让高位有1。

然后我们从右往左找那个能移动的1吧。能移动的条件是,当前1的位置+1不等于

下一个1的位置,也就是前面是空的。然后就这样啊....

高质量的模拟确实应该学习一下...

代码:


#include<cstdio>
using namespace std;
int n,k,j;
int a[];
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=k;i++)a[i]=i;
for(int i=;i<=n;i++){
j=;
while(){
a[j]++;
if(a[j]!=a[j+])break;
a[j]=j;
j++;
}
}
j=k;
for(int i=a[k];i>=;i--){
if(a[j]==i)printf(""),j--;
else printf("");
}
return ;
}

 

洛谷 P3048 [USACO12FEB]牛的IDCow IDs的更多相关文章

  1. 洛谷P3048 [USACO12FEB]牛的IDCow IDs

    P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 ...

  2. LUOGU P3048 [USACO12FEB]牛的IDCow IDs(组合数)

    传送门 解题思路 组合数学.首先肯定是要先枚举位数,假如枚举到第\(i\)位.我们可以把第一位固定,然后那么后面的随意放\(1\),个数就为\(C_{i-1}^{k-1}\).然后每次枚举时如果方案\ ...

  3. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  4. [USACO12FEB]牛的IDCow IDs

    题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...

  5. [USACO12FEB]牛的IDCow IDs 一题多解(求二进制中有k个1 ,第n大的数)

    题目: FJ给他的奶牛用二进制进行编号,每个编号恰好包含K 个"1" (1 <= K <= 10),且必须是1开头.FJ按升序编号,第一个编号是由K个"1&q ...

  6. 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game

    洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...

  7. 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

  8. 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup

    https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...

  9. 洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

随机推荐

  1. Web前端开发的基本要求和认识

    Web前端开发技术包括三个要素:HTML.CSS和JavaScript,但随着RIA的流行和普及,Flash/Flex.Silverlight.XML和服务器端语言也是前端开发工程师应该掌握的.Web ...

  2. java 分页工具类

    //13年写的,今天(17)拷贝到博客园 package com.sicdt.sicsign.web.utils; import java.io.Serializable; import java.u ...

  3. Spring 全局异常捕捉

    Spring全局异常捕捉类 注解@ControllerAdvice package com.sicdt.sicsign.web.bill.controller; import org.springfr ...

  4. 通过调节坐标进行jfree图的放大缩小

    http://blog.csdn.net/lt1983lt/article/details/5665085 import Java.awt.BorderLayout;import java.awt.C ...

  5. linux之下载工具wget

    常用格式:wget options URL -b,  --background        启动后转入后台执行 -c,  --continue               接着下载没下载完的文件 - ...

  6. JVM内存的堆、栈和方法区

    JVM的内存分为堆.栈.方法区和程序计数器4个区域 存储内容:基本类型,对象引用,对象本身,class,常量,static变量 堆: 拥有者:所有线程 内容:对象本身,不存放基本类型和对象引用 垃圾回 ...

  7. linux下firefox显示中文乱码的问题

    只需要yum install "@Chinese Support" 然后注销,再登录一下,刷新浏览器就可以正常显示中文了,当然前提是浏览器的字符编码为utf-8以及默认显示中文,这 ...

  8. echache缓存的简单使用方法

    1.需要echache的jar包 2.需要配置文件ehcache.xml和ehcache.xsd,主要是在ehcache.xml中进行配置 3.修改配置文件ehcache.xml  ,例如添加配置如下 ...

  9. 重置密码解决MySQL for Linux错误 ERROR 1045 (28000)

    重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...

  10. wareshark网络协议分析之DHCP

    声明:本文关于DHCP协议介绍部分摘自百度百科 一.DHCP协议介绍: DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用 ...