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- ...
随机推荐
- left join on
问题: select * from A left join f on e.cust=f.account_id where f.status='0' 与 select * from A left jo ...
- Mysql 修改密码及重置密码方法
修改密码: //选择数据库 use mysql; //修改密码 update user set password=password('新密码') where user='root'; //立即生效 f ...
- Android 学习笔记之如何实现简单相机功能
PS:看来算法和数据结构还是非常有用的,以后每天都练习两道算法题目...这次忘了对代码进行折叠了..导致篇幅过长... 学习内容: 1.Android如何实现相机功能... 2.如何实现音频的录制.. ...
- python编码声明的位置很重要
python在3.x版本之前,编码一直是一个很头痛的问题.在代码中如果要使用中文,通常都要在文件的头部注明# -*- coding:utf-8 -*- 这样IDE或者解释器才会智能的转换编码. 这其中 ...
- SystemTap了解
SystemTrap是监控和跟踪运行中的Linux内核操作的动态方法. http://www.ibm.com/developerworks/cn/linux/l-systemtap/ 使用System ...
- C#对象与XMl文件之间的相互转换
C#提供三种序列化方式,分别为: 1.是使用BinaryFormatter进行串行化: 2.使用SoapFormatter进行串行化: 3.使用XmlSerializer进行串行化.其中对于Binar ...
- MySQL+Keepalived实现双机HA
host1与host3互为主从,即host1为host3的主机,同时也为host3的从机 host1 192.168.203.131 host2 192.168.203.132 host3 192 ...
- [阅读]个人阅读作业week7
People-oriented in Agile People-oriented in Agile One Leader Prepare Good ideas from users People-or ...
- log4net日志记录
这里是接着上一篇来优化的,上篇:ASP.NET MVC中错误日志信息记录 log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件,数据库,EventLog等),日志就是程序的黑 ...
- 购买SSL证书到部署网站遇到的若干问题
作为一个菜鸟,对于SSL证书,我了解不多,只知道用了它网站更安全,所以这次使用SSL证书途中遇到了各方面的各种问题,到今天为止终于全部解决. 一.证书格式 前两天在那什么云上面买了个SSL证书,是Wo ...