题目描述

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. Kattis - register 【水】

    题意 就是 有一堆容器,然后可以执行加的操作,每个容量是 2, 3, 5, 7, 11, 13, 17, 19 然后 有进位 比如第一个 容器,到2了,就会重置为0,然后 下一个容器+ 1, 但是要保 ...

  2. 初学JQuery相关知识点

    [简单的JQuery]注册事件的函数. $(document).ready(function(){}) [JQuery提供的函数]$.map(array,fn) 对数组array中每个元素调用fn函数 ...

  3. FreeRtos堆栈检测应用

    Free rtos每个任务都有自己的栈空间,每个任务需要的栈大小也是不同的.如果堆栈过小就会造成栈溢出,有时候栈溢出发生在某种特定顺序的任务切换中,比较难检测出.所以前期测试和监控任务栈用量就显得尤其 ...

  4. CMD 删除脚本

    CMD 删除脚本 forfiles /p D:\BACKUP\WindowsImageBackup /s /m *.* /d -14 /c "cmd /c del @file"; ...

  5. centOS最小化安装后网络连接问题

    编辑配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0   修改此行重启后即可 ONBOOT="yes"           #修改为 ...

  6. ubuntu 的mysql 安装过程和无法远程的解决方案

    ubuntu 的mysql 安装过程和无法远程的解决方案 安装完mysql-server启动mysqlroot@ubuntu:# /etc/init.d/mysql start (如果这个命令不可以, ...

  7. ubuntu 忘记root密码

    Ubuntu14.04系统中,因为误操作导致管理员密码丢失或无效,并且忘记root密码,此时无法进行任何root/sudo权限操作.可以通过GRUB重新设置root密码,并恢复管理员账户到正常状态. ...

  8. Mybatis单个参数的if判断(针对异常:There is no getter for property..)------mybatis的内置对象

    这里有一个删除方法: int deleteByPrimaryKey(Integer id); 然后对应的sql的xml如下: <delete id="deleteByPrimaryKe ...

  9. Java基础(7)-集合类3

    list集合的特点 1)有序(存储和取出的元素一直) 2)可重复 List子类的特点 ArrayList 有序,可重复 底层数据结构是数组 查询快,增删慢 线程不安全,效率高 Vector 有序,可重 ...

  10. NovaException: Unexpected vif_type=binding_failed

    nova/virt/libvirt/vif.py: _("Unexpected vif_type=%s") % vif_type) NovaException: Unexpecte ...