洛谷 P3048 [USACO12FEB]牛的IDCow IDs
题目描述
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.
输出格式:
输入输出样例
7 3
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的更多相关文章
- 洛谷P3048 [USACO12FEB]牛的IDCow IDs
P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交 讨论 题解 ...
- LUOGU P3048 [USACO12FEB]牛的IDCow IDs(组合数)
传送门 解题思路 组合数学.首先肯定是要先枚举位数,假如枚举到第\(i\)位.我们可以把第一位固定,然后那么后面的随意放\(1\),个数就为\(C_{i-1}^{k-1}\).然后每次枚举时如果方案\ ...
- 洛谷P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...
- [USACO12FEB]牛的IDCow IDs
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...
- [USACO12FEB]牛的IDCow IDs 一题多解(求二进制中有k个1 ,第n大的数)
题目: FJ给他的奶牛用二进制进行编号,每个编号恰好包含K 个"1" (1 <= K <= 10),且必须是1开头.FJ按升序编号,第一个编号是由K个"1&q ...
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
- 洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
随机推荐
- iOS 系统认知 debug distribution release 和 #ifdef DEBUG
debug:调试模式 有调试信息 线下 release: 无调试信息 经过了编译优化 发布 给用户使用的 线上模式 一般 工程项目 都是自带 上述两种配置结构 还有出现 distribution: ...
- json教程系列(3)-JSONObject的过滤设置
我们通常对一个json串和java对象进行互转时,经常会有选择性的过滤掉一些属性值.例如下面的类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- 给二维码(图片)添加文字(水印),让生成的二维码中间带logo
<?php //生成二维码 require_once IA_ROOT . '/framework/library/qrcode/phpqrcode.php'; QRcode::png($url, ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- CentOS 5下freeswitch中集成使用ekho实现TTS功能二
三:以上Festival安装完成以后回到ekho安装目录: 执行./configure --enable-festival 前 更改configure 1:替换 #AC_DEFINE(ENABLE_F ...
- jQuery自动轮播图片焦点图
在线演示 本地下载
- 20145210姚思羽 《网络对抗技术》 Web安全基础实践
20145210姚思羽 <网络对抗技术> Web安全基础实践 实验后回答问题 1.SQL注入攻击原理,如何防御 ·SQL攻击的原理很简单,就是在用户名输入框里输入SQL语句,来欺骗数据库服 ...
- Swift_初识Swift
初识Swift语言 Swift结合了C和OC的优点并且不受C兼容性的限制.Swift采用安全的编程模式并添加了很多新特性,这将是编程更简单,更灵活也更有趣,Swift是基于成熟而且倍受喜爱的Cocoa ...
- NSFetchedResultController与UITableView
1 #import "AppDelegate.h" #import "Book.h" @interface AppDelegate () @end @imple ...
- RabbitMQ解决分布式事务
案例:经典案例,以目前流行点外卖的案例,用户下单后,调用订单服务,让后订单服务调用派单系统通知送外卖人员送单,这时候订单系统与派单系统采用MQ异步通讯. RabbitMQ解决分布式事务原理: 采用最终 ...