zoukankan      html  css  js  c++  java
  • [HNOI 2006]鬼谷子的钱袋

    Description

    鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政。有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会,其中有一件宝物引起了他极大的兴趣,那就是无字天书。但是,他的行程安排得很满,他他已经买好了去邯郸的长途马车标,不巧的是出发时间是在拍卖会快要结束的时候。于是,他决定事先做好准备,将自己的金币数好并用一个个的小钱袋装好,以便在他现有金币的支付能力下,任何数目的金币他都能用这些封闭好的小钱的组合来付账。鬼谷子也是一个非常节俭的人,他想方设法使自己在满足上述要求的前提下,所用的钱袋数最少,并且不有两个钱袋装有相同的大于1的金币数。假设他有m个金币,你能猜到他会用多少个钱袋,并且每个钱袋装多少个金币吗?

    Input

    包含一个整数,表示鬼谷子现有的总的金币数目m。其中,1≤m ≤1000000000。

    Output

    只有一个整数h,表示所用钱袋个数

    Sample Input

    3

    Sample Output

    2

    题目大意

    求最少的数的个数,使它们能组成$[1,m]$内的任意数字。不能出现大于一重复的数字。

    题解

    显然就是二进制,我们倍增来求,最后剩下的单独成一组。若剩下的数和之前相同,那么一个$+1$,一个$-1$即可。

    容易发现,答案就是${log_2 m}+1$。

    原题还是要输出方案的,我们开个数组记录下即可。

     1 //It is made by Awson on 2017.10.20
     2 #include <set>
     3 #include <map>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <string>
    10 #include <cstdio>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Min(a, b) ((a) < (b) ? (a) : (b))
    17 #define Max(a, b) ((a) > (b) ? (a) : (b))
    18 #define Abs(x) ((x) < 0 ? (-(x)) : (x))
    19 using namespace std;
    20 
    21 LL m, st = 1;
    22 LL cnt, a[105];
    23 
    24 void work() {
    25     scanf("%lld", &m);
    26     while (m-st > 0) m -= st, a[++cnt] = st, st <<= 1;
    27     if (m) a[++cnt] = m;
    28     printf("%lld
    ", cnt);
    29     sort(a+1, a+1+cnt);
    30     for (int i = 1; i <= cnt; i++) if (a[i] != 1 && a[i] == a[i+1]) {
    31         printf("%d ", a[i]-1); a[i+1]++;
    32     }else printf("%d ", a[i]);
    33 }
    34 int main() {
    35     work();
    36     return 0;
    37 }
  • 相关阅读:
    Javascript 运动中Offset的bug——逐行分析代码,让你轻松了解运动的原理
    Javascript 多物体运动——逐行分析代码,让你轻松了解运动的原理
    thymeleaf设置网页脚本里面的值
    win10系统瞬间黑屏问题
    Spring boot启动端口设置
    idea打包jar包
    java socket使用例子
    java使用socket读取网页
    java读取网页
    Error:Unexpected lock protocol found in lock file. Expected 3, found 0
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7701776.html
Copyright © 2011-2022 走看看