洛谷 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 Xcode 8 快捷键 (注释 失效 处理)
在升级后,好用的VVDocumment 插件不能用了.(但是苹果这次内置了好多好用的插件,也有自己的注释功能了 AddDocumentation) 上网上有查到 传播很广泛的一条信息 "这个 ...
- P4299 首都
题目 P4299 首都 做法 这题是动态维护树的重心,连边后找到两棵树的重心拉一条链(性质:新重心在链上),然后暴力爬 要注意: 1.是找重心的过程中要先把旋转标记放下来,因为\(Splay(x)\) ...
- php的正则表达式
这篇文章介绍的内容是关于php的正则表达式 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下. 正则表达式是一种描述字符串结果的语法规则,是一个特定的格式化模式,可以匹配.替换.截取匹配 ...
- Java中系统时间的获取_currentTimeMillis()函数应用解读
快速解读 System.currentTimeMillis()+time*1000) 的含义 一.时间的单位转换 1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微 ...
- Java Hibernate 5.3.x
SchemeExport Hibernate根据实体类和实体类映射文件自动生成表结构. 示例代码: <?xml version='1.0' encoding='utf-8'?> <! ...
- 吴恩达深度学习笔记(十一)—— dropout正则化
主要内容: 一.dropout正则化的思想 二.dropout算法流程 三.dropout的优缺点 一.dropout正则化的思想 在神经网络中,dropout是一种“玄学”的正则化方法,以减少过拟合 ...
- centos iscsi 配置
首先是服务器的设置:[root@localhost 桌面]# yum install scsi-target-* -y 安装服务 配置yum的方法太简单了,我就不写了[root@local ...
- 安装rackspace private cloud --5 Deployment configuration
运行Ansible playbooks之前,需要配置taget host Prerequisites . cp -r /opt/openstack-ansible/etc/openstack_depl ...
- Hadoop- Namenode经常挂掉 IPC's epoch 9 is less than the last promised epoch 10
如题出现Namenode经常挂掉 IPC's epoch 9 is less than the last promised epoch 10, 2019-01-03 05:36:14,774 INFO ...
- Eureka、Ribbon、Feign常见问题及解决
1.Eureka常见问 1.1.Eureka Enviroment 的配置 eureka.enviroment=product 参考 https://github.com/Netflix/eureka ...