UVa 107 - The Cat in the Hat (找规律,注意精度)
| The Cat in the Hat |
Background
(An homage to Theodore Seuss Geisel)
The Cat in the Hat is a nasty creature,
But the striped hat he is wearing has a rather nifty feature.
With one flick of his wrist he pops his top off.
Do you know what's inside that Cat's hat?
A bunch of small cats, each with its own striped hat.
Each little cat does the same as line three,
All except the littlest ones, who just say ``Why me?''
Because the littlest cats have to clean all the grime,
And they're tired of doing it time after time!
The Problem
A clever cat walks into a messy room which he needs to clean. Instead of doing the work alone, it decides to have its helper cats do the work. It keeps its (smaller) helper cats inside its hat. Each helper cat also has helper cats in its own hat, and so on. Eventually, the cats reach a smallest size. These smallest cats have no additional cats in their hats. These unfortunate smallest cats have to do the cleaning.
The number of cats inside each (non-smallest) cat's hat is a constant, N. The height of these cats-in-a-hat is
times the height of the cat whose hat they are in.
The smallest cats are of height one;
these are the cats that get the work done.
All heights are positive integers.
Given the height of the initial cat and the number of worker cats (of height one), find the number of cats that are not doing any work (cats of height greater than one) and also determine the sum of all the cats' heights (the height of a stack of all cats standing one on top of another).
The Input
The input consists of a sequence of cat-in-hat specifications. Each specification is a single line consisting of two positive integers, separated by white space. The first integer is the height of the initial cat, and the second integer is the number of worker cats.
A pair of 0's on a line indicates the end of input.
The Output
For each input line (cat-in-hat specification), print the number of cats that are not working, followed by a space, followed by the height of the stack of cats. There should be one output line for each input line other than the ``0 0'' that terminates input.
Sample Input
216 125
5764801 1679616
0 0
Sample Output
31 671
335923 30275911
解题思路:
帽子里的猫,
此题给的条件是第一支猫的高度h和最后的working cats的数量w.
下面分析猫的高度和猫的数量之间的关系:
设开始的猫的高度是 h , N 是每个帽子里面猫的数量,设一个中间变量s。
猫的高度 h h / (N + 1) h / (N + 1)2 ………… h / (N + 1)s
猫的数量 1 N N * N ………… Ns
当最后工作的猫的个数为w时:
lg(n)/lg(n+1)=lg(num)/lg(hgiht)
是用二分查找算出N即可,条件就写成了fabs(lg(n)*lg(height)-lg(num)/lg(n+1)<0.00001)
下面贴代码:
#include <bits/stdc++.h> using namespace std; int main()
{
int h,num;
while(~scanf("%d%d",&h,&num))
{
if(==h&&==num)
break;
int left=,right=,mid;
while(left)
{
mid=(left+right)/;
if(fabs(log(mid)*log(h)-log(mid+)*log(num))<=0.000001) break;
if(log(mid)*log(h)-log(mid+)*log(num)>)
right=mid;
else
left=mid;
}
int x=,y=h;
left=;
while(h>)
{
h/=(+mid);
left*=mid;
x+=left;
y+=h*left;
}
printf("%d %d\n",x-num+,y);
}
}
UVa 107 - The Cat in the Hat (找规律,注意精度)的更多相关文章
- uva 107 - The Cat in the Hat
The Cat in the Hat Background (An homage to Theodore Seuss Geisel) The Cat in the Hat is a nasty c ...
- 杂题 UVAoj 107 The Cat in the Hat
The Cat in the Hat Background (An homage to Theodore Seuss Geisel) The Cat in the Hat is a nasty c ...
- UVA - 808 Bee Breeding (建立坐标系&找规律)
题目: 输入两个格子的编号a和b(a,b≤10000),求最短距离.例如,19和30的距离为5(一条最短路是19-7-6-5-15-30). 思路: 如图建立坐标系,然后看两个点的向量如果位于二四象限 ...
- 【数论,找规律】Uva 11526 - H(n)
原来做过的题再看还是没想出来,看来当时必然没有真正理解.这次回顾感觉理解更透彻了. 网上的题解差不多都是一个版本,而且感觉有点扯.根据n=20猜出来的? 好吧哪能根据一个就猜到那么变态的公式.其实这题 ...
- GCD XOR UVA 12716 找规律 给定一个n,找多少对(a,b)满足1<=b<=a<=n,gcd(a,b)=a^b;
/** 题目:GCD XOR UVA 12716 链接:https://vjudge.net/problem/UVA-12716 题意:给定一个n,找多少对(a,b)满足1<=b<=a&l ...
- 递推+高精度+找规律 UVA 10254 The Priest Mathematician
题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...
- SGU 107 987654321 problem【找规律】
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=107 题意: 平方后几位为987654321的n位数有多少个 分析: 虽然说是水题 ...
- UVa 11774 (置换 找规律) Doom's Day
我看大多数人的博客只说了一句:找规律得答案为(n + m) / gcd(n, m) 不过神题的题解还须神人写.. We can associate at each cell a base 3-numb ...
- UVA 10254 - The Priest Mathematician (dp | 汉诺塔 | 找规律 | 大数)
本文出自 http://blog.csdn.net/shuangde800 题目点击打开链接 题意: 汉诺塔游戏请看 百度百科 正常的汉诺塔游戏是只有3个柱子,并且如果有n个圆盘,至少需要2^n- ...
随机推荐
- 【转载】Linux下编辑生成.mo文件
转载自:http://www.hackbase.com/tech/2012-02-27/65972.html 编辑生成.mo文件 我们在弄网站的时候很可能会接触到.mo和.po文件..po文件是GNU ...
- iOS-金额小写转大写
一.目的 1. 金额小写转化成大写. 如 123456.65 --> 壹拾贰万叁仟肆佰伍拾陆元陆角伍分 2. 只能处理13位数的金额,并且只能处理到小数点后两位. 二.代码 #import &q ...
- 初用DataGrip,连接后看不到自己创建的数据库的问题
1.首先,成功连接数据库服务 2.可以看到默认连接到名为"larins_qr_db"的数据库 3.这时,我想看其它的数据库怎么办? 4.OK,想要的出现了!
- 探索jdk8之ConcurrentHashMap 的实现机制
在介绍ConcurrentHashMap源码之前,很有必要复习下java并发编程中的一些基础知识,比如内存模型等. 存储模型 并发编程中的三个概念 1.原子性 2.可见性 3.重排序 对HashMap ...
- 如何找出你性能最差的SQL Server查询
我经常会被反复问到这样的问题:”我有一个性能很差的SQL Server.我如何找出最差性能的查询?“.因此在今天的文章里会给你一些让你很容易找到问题答案的信息向导. 问SQL Server! SQL ...
- 用Qt写软件系列二:QCookieViewer(浏览器Cookie查看器)
预备 继上篇<浏览器缓存查看器QCacheViewer>之后,本篇开始QCookieViewer的编写.Cookie技术作为网站收集用户隐私信息.分析用户偏好的一种手段,广泛应用于各大网站 ...
- WinFrom子窗体向父窗体传值
父窗框mainForm;子窗体childForm,利用事件进行传值 在子窗体中的操作: public event EventHandler accept;public string value; pr ...
- csharp: 百度语音合成
public string API_id = "3333"; //你的ID public string API_record = null; public string API_r ...
- ahjesus 捕获entity framework生成的sql语句
网上这方面的资料很少,找到一个可以用的 http://code.msdn.microsoft.com/EFProviderWrappers 里面有dll可以下载,有教程,不过是E文的. 在Entity ...
- Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法
Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法 Some sites could ...