您的位置:首页 > 其它

Problem E: Balance (华中农业大学预赛)

2016-05-14 16:32 176 查看

Problem E: Balance

Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 183 Solved: 57

[Submit][Status][Web
Board]

Description

Every night Diao Ze is dreaming about the gold medal in the ACM-ICPC World Final. Not only does it represent the honor, but also Diao Ze likes the gold, and we know it is blingbling. Whenever Diao Ze gets a gold medal in his dream, he would weigh it
again and again. Gosh, so tightfisted he is. For fear of losing any gold, he wants to know the certain weight of this gold medal. So he asks you for help in his dream.



Now you have a counter balance, like the picture above (you can put weight counters on both of the pans of the scale). Diao Ze wants to get the weight of the medal accurately, and he knows that the medal could not be heavier thanN grams. If
it is possible for Diao Ze to make the scale balanced with some certain different counter (any weight of the counter which has integer weight of grams is available for you, and the amount is infinite). Diao Ze could get the exact weight of the medal from the
nominal weight of the counter. Pay attention, the counter with the same weight cannot be used more than once!

Input

The first line contains an integer T, indicating the total number of test cases. In each test case there is only one line contains an integer

, indicating
the maximum possible weight of the gold medal.

Output

For each test case, output the answer indicating the minimum number of counters, in that condition, you can make the scale balanced if the medal isx grams, no matter what
x is, but satisfies

.

Sample Input

3345

Sample Output

223

HINT

In the second sample, N=4, we can use 2 counter with nominal weight 1 grams and 3 grams. If the medal is 1 gram, just use the counter with 1 gram. If the medal is 2 grams, use the counter with 3 grams and 1 gram on two pans, the medal is on the same
pan with counter 1. That means 2=3-1. If the medal is 3 grams, just use the counter with 3 grams. And if the medal is 4 grams, use the counter with 3 grams and 1 gram on the same pan, the medal is on the other pan. That means 4=3+1. So all the possible weight
between 1 and 4 can be got.

[Submit][Status]

题意:


给一个数n,请问最少需要多少种不同的数字就可以构成从1−n的所有数字

例如:我们都知道人民币的面值是1、2、5、10,为什么是这个数值呢,我们分析了下发现,从1−10的每个数字都可以由每种面值选出至多一张通过加

法和减法(找钱)来构成,(比如:1+2=3,5−1=4,5+1=6,5+2=7,1+2+5=8,10−1=9)

但是实际上,我们只需要1、2、7三种面值就可以组成1−10的每一个数字了

(1+2=3,7−1−2=4,7−2=5,7−1=6,7+1=8,7+2=9,7+1+2=10)

那么现在问题来了,给一个数n,请问最少需要多少种不同的面值就可以构成从1−n的所有数字,注意在构成每一个数字时同种面值不能超过1张。


有一架天平,要用它称出1~N克之间所有重量为整数克的物体,至少用多少个砝码?

例如要用它称出1~40克之间所有重量为整数克的物体,至少用多少个砝码?每个砝码的重量是多少?答案是4个砝码,这4个砝码的重量分别是1,3,9,27。再推至极限情况,给出任意一个范围,1~N,是否有一套公式,根据N的数值,可以算出需要至少多少砝码,并分别计算出这些砝码的重量呢?

借鉴的,,数学不好,真心累~

作者:周欣宇
链接:http://www.zhihu.com/question/30164499/answer/47003542
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

一、结论:
假设使用n+1个砝码,
(复制的公式 有点问题,想看的 直接点上面的链接~)
对于每个n,可以称出的N的范围在
此处为~( [ (3^n+1) / 2 ,  (3^(n+1) -1) / 2 ]);~~
多于这个范围需要n=n+1,少于这个范围则只需要到n=n-1即可
对于题主提出的情况即为n=3,

二、应用:
对于任意N,根据(一)中闭区间的范围求出n值,即可得到所需求的砝码个数(n+1)。
假如N=6546587,求出log(6546587, base=3)~=14.2857, 即需要15个砝码,从1,3,9,27一直到3^14=4782969;由这15个砝码可以最多称出1-7174453中任何一个数字。

证明:
数学归纳法:
(1)对n=0,1,2,口算成立

(2)假设有k个砝码,可以称出不大于

的所有组合。

(3)那么加入第k+1个砝码:
我们可以看到

也即恰好为

的中间值,离两个端点的距离均为
。
而这个值正是(2)中k个砝码可以完美覆盖的数值范围。

很久没写证明了可能语言不太好...如有疑问请提出
------

来一个直观图。
我们知道最大克数和最远距离其实是同样的问题。

假设在第k步,所有的砝码总共可以测这么长
<img src="https://pic3.zhimg.com/942c30b45d78801a1130c56f786ac292_b.jpg" data-rawwidth="636" data-rawheight="152" class="origin_image zh-lightbox-thumb" width="636" data-original="https://pic3.zhimg.com/942c30b45d78801a1130c56f786ac292_r.jpg">那么在第k+1步,为了让砝码能够最大限度的被利用,即测出最远距离,那么我们要这么安排:

那么在第k+1步,为了让砝码能够最大限度的被利用,即测出最远距离,那么我们要这么安排:
<img src="https://pic3.zhimg.com/5c6b9c5f5d300d85b36d67fb80a69a56_b.jpg" data-rawwidth="1962" data-rawheight="355" class="origin_image zh-lightbox-thumb" width="1962" data-original="https://pic3.zhimg.com/5c6b9c5f5d300d85b36d67fb80a69a56_r.jpg">所以第k+1步中能测出的最远距离

所以第k+1步中能测出的最远距离的递推公式即为

因为,可以算出最后的通项,即

-----

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<stdio.h>
#include<map>
#include<vector>
#define min(a,b)(a<b?a:b)
#define max(a,b)(a>b?a:b)
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int main()
{
int T,n,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;;i++)
{
if(1.0*(pow(3,i)+1)/2<=n && (1.0*(pow(3,i+1)-1)/2)>=n)
{
printf("%d\n",i+1);
break;
}
}
}
return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: