先上题目:

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. Android4.4 wpa_supplicant深入分析之wpa_supplicant初始化流程续

    下面我们将接上一篇文章继续分析main中第二个关键函数wpa_supplicant_add_iface. wpa_supplicant_add_iface用于向wpa_supplicant添加接口设备 ...

  2. P1052 过河(状态压缩)

    P1052 过河(状态压缩) 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把 ...

  3. leetcode排列组合相关

    目录 78/90子集 39/40组合总和 77组合 46/47全排序,同颜色球不相邻的排序方法 78/90子集 输入: [1,2,2] 78输出: [[], [1], [2], [1 2], [2], ...

  4. Android单选中listview中的一项

    public class LipsListAdapter extends BaseAdapter { private Context context; private List<Lips> ...

  5. PCB 线路板人生

    由此开端,增加PCB人生分类栏,后续在此分享PCB 非工作方面所思所想,由于文笔不好,请指正.

  6. selenium3 + python - table定位

    前言 在web页面中经常会遇到table表格,特别是后台操作页面比较常见.本篇详细讲解table表格如何定位. 一.认识table 1.首先看下table长什么样,如下图,这种网状表格的都是table ...

  7. GStreamer系列 - 基本介绍

    什么是Gstreamer? Gstreamer是一个支持Windows,Linux,Android, iOS的跨平台的多媒体框架,应用程序可以通过管道(Pipeline)的方式,将多媒体处理的各个步骤 ...

  8. A - Team

    Problem description One day three best friends Petya, Vasya and Tonya decided to form a team and tak ...

  9. javascript基础(完整)

    一.什么是javascript? 是一种基于对象和事件驱动(以事件驱动的方式直接对客户端的输入做出响应,无需经过服务器端)并具有安全性能的解释型脚本语言,在web应用中得到非常广泛地应用.它不需要编译 ...

  10. android 国际化 横屏(land) 竖屏(port)margin外边距和padding内边距

    android 国际化 横屏(land) 竖屏(port) 边距又分为内边距和外边距,即margin和padding.