先上题目:

Problem 2062 Suneast & Yayamao

Accept: 146    Submit: 319
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Yayamao is so cute that people loves it so much.

Everyone wants to buy Yayamao from Suneast (a business man who sells Yayamao).

Suneast is a strange business man. He sells Yayamao in a random price from 1, 2, 3, 4, 5…, n.

Suneast is also a lazy business man. He never looks for a change. But people can’t but Yayamao with a lower price, that say people must pay exact money for Yayamao.

Now, we want to know how many pieces of money people should bring with to buy a Yayamao with the exactly price.

 Input

There are multiple test cases. Each test case has an integer n(1<=n<=2147483647) in a single line.

 Output

For each case, output a single integer in a line indicate the number of pieces of money people should bring with to buy a Yayamao whose price is random from 1 to n.

 Sample Input

1
2
5

 Sample Output

1
2
3

 Hint

In test case 1: people can bring 1 piece of money: 1

In test case 2: people can bring 2 pieces of money: (1, 1) or (1, 2)

In test case 3: people can bring 3 pieces of money: (1, 1, 3) or (1, 2, 2) ….

  题意:给你一个数n,问你至少用多少个数,只用这些数就可以表示1~n的所有数。

  比如5:1=1

       2=1+1

       3=1+2

       4=2+2

         5=1+1+3

  当然可能还有很多的方法来表示1~5的数,不过一定需要三个。

  其实我们可以这样分析,将这个数n化成二进制,假如它有l位,那么对于一个l位的二进制数,我们需要用l位二进制才可以将它表示出来,同时比它小的数,我们可以用不超过l位的数来保存表示它们,换言之,我们可以用2^0,2^1,2^2···2^k···这些数来表示从1~n的数,至于k等于多少,那就需要看n的二进制有多少位了。

  这也是背包问题里面的多重背包的基础。

上代码:

 #include <cstdio>
#include <cstring>
#define LL long long
#define MAX 2147483647
using namespace std; int main()
{
int n;
int tot;
//freopen("data.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
tot=;
while(n){
tot++;
n=n>>;
}
printf("%d\n",tot);
}
return ;
}

2062

    

FZU - 2062 - Suneast & Yayamao的更多相关文章

  1. 【二进制】FZU 2062 Suneast & Yayamao

    题意:给出n,问最少需要多少个数字,这些数字能组成1~n的所有数: 分析:n=1; 1; 1个 n=2; 1,1;  2个 1 = 1; 2 = 1+1;   n=3; 1,2; 2个 1 = 1; ...

  2. Problem 2062 Suneast & Yayamao 二进制(多重背包的理解基础)

                                          Problem 2062 Suneast & Yayamao Accept: 143    Submit: 313T ...

  3. FZU Problem 2062 Suneast & Yayamao

    http://acm.fzu.edu.cn/problem.php?pid=2062 题目大意: 给你一个数n,要求求出用多少个数字可以表示1~n的所有数. 思路: 分解为二进制. 对于一个数n,看它 ...

  4. FZU Problem 2062 Suneast &amp; Yayamao

    http://acm.fzu.edu.cn/problem.php?pid=2062 标题效果: 给你一个数n,要求求出用多少个数字能够表示1~n的全部数. 思路: 分解为二进制. 对于一个数n.看它 ...

  5. FZU 2240 Daxia & Suneast's problem

    博弈,$SG$函数,规律,线段树. 这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢.修改操作和区间查询的话可以用线段树维护一下区间异或和. 数据那么大 ...

  6. FZU 2137 奇异字符串 后缀树组+RMQ

    题目连接:http://acm.fzu.edu.cn/problem.php?pid=2137 题解: 枚举x位置,向左右延伸计算答案 如何计算答案:对字符串建立SA,那么对于想双延伸的长度L,假如有 ...

  7. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  8. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  9. [日常训练]yayamao的神题

    Description $yayamao$是数学神犇,一天他在纸上计算起了$1/P$, 我们知道按照模拟除法可以得到准确解,例如$1/7=0.(142857),1/10=0.1(0)$.$yayama ...

随机推荐

  1. codeforce1046 Bubble Cup 11 - Finals 题解

    比赛的时候开G开了3h结果rose说一句那唯一一个AC的是羊的心态就崩了.. 这套题感觉质量挺好然后就back了下 A: AI robots 有三个限制条件:相互能够看见和智商的差.使用主席树,可以维 ...

  2. B2321 [BeiJing2011集训]星器 数学&&物理

    这个题貌似特别奇怪,根本什么算法都想不出来,然而...看完题解之后,竟然用了能量守恒?惊了! 这里有一个题解: https://blog.csdn.net/Mima_Reincarnation/art ...

  3. javascript--给你的JS代码添加单元测试

    通过测试框架为JavaScript应用添加测试,从而保证代码的高质量.这里的笔记例子应用在jaywcjlove/validator.js中. 安装 用到三个工具chai(断言工具),mocha(测试框 ...

  4. C# Event.ClickCount 解决垃圾鼠标带来的烦恼

    今天调试遇到个Bug,百思不得其解的是在自己的设备上重来不重现,在测试机上百分百重现,如下: 问题:点击一次Button执行两次Click操作 分析:看Log的确是执行了两次,就像真的点击了两次But ...

  5. Python 30 单例模式

    单例模式 多次实例化的结果指向同一个实例 单例模式实现方式 import settings #方式一: class MySQL: __instance=None def __init__(self,i ...

  6. Element-UI 笔记

    1.表单验证 官官方地址 :     https://element.eleme.cn/#/zh-CN/component/form 使用rules进行表单字段验证  https://blog.csd ...

  7. POJ 2286 The Rotation Game IDA*

    (再一次感谢学长幻灯片) ID A* 随便自己yy了一下. 额嗯 思路什么的都没有问题 就是改不对.. 无奈地删代码...边删边交. 删啊删 哎呦 AC了 ... ... ... 找删的那一段 . o ...

  8. Hibernate基础(二)

    Hibernate中的实体规则 创建实体类注意事项: 1.持久化类提供无参构造函数 2.成员变量私有 提供共有get set 方法 3.属性应尽量使用包装类 Integer.Float.Double等 ...

  9. 5.17从零开始搭建springboot-dubbo的例子

    https://www.cnblogs.com/baijinqiang/p/10848259.html

  10. python爬虫:找房助手V1.0-爬取58同城租房信息

    1.用于爬取58上的租房信息,限成都,其他地方的,可以把网址改改: 2.这个爬虫有一点问题,就是没用多线程,因为我用了之后总是会报: 'module' object has no attribute ...