Time limit: 3.000 seconds
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 1 / (N + 1) 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
Analysis
算法并不复杂,主要在于浮点数精度问题的处理上。用log取对数再取整时,要注意4舍5入。从干活的猫的数量入手,因为必然是平方数,遍例所有可能的因子就能找到N的值。通过实验发现,OJ给的数据中,第一只猫的高度必然是一个等比数列之和(包括1),因此如果能够根据等比数列公式直接解出公比,即N + 1,那就非常好办了。但目前我还没有找到比较好的解法,只能先按老办法算。
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <iostream> #include <math.h> using namespace std; int main( void ) { int nFirstHeight, nWorking; while (cin >> nFirstHeight >> nWorking) { if (nFirstHeight == 0) { break ; } int nCatCnt = 0, nTotalHeight = 0; if (nWorking == 1) { for ( int k = nFirstHeight; k >= 1; k /= 2, ++nCatCnt) { nTotalHeight += k; } cout << nCatCnt - 1<< ' ' << nTotalHeight << endl; continue ; } float fLog = log (( float )nWorking), fLevel; int nSqrt = ( int )( sqrt (( float )nWorking) + 0.5); int nLevel = 0, nHeight = nFirstHeight, nBranch = 2; for (; nBranch <= nSqrt; ++nBranch) { fLevel = fLog / log (( float )nBranch); int nTempLevel = ( int )(fLevel + 0.5); if ( fabs ((fLevel - nTempLevel)) < 1e-4f) { nLevel = nTempLevel + 1; if (( int )( pow (( float )(nBranch + 1), nTempLevel) + 0.5) == nFirstHeight) { break ; } } } if (nLevel == 0) { nLevel = 2; nBranch = nWorking; } for ( int k = 0, nCats = 1; k < nLevel - 1; ++k) { nCatCnt += nCats; nTotalHeight += nCats * nHeight; nCats *= nBranch; nHeight /= (nBranch + 1); } cout << nCatCnt << ' ' << nTotalHeight + nWorking << endl; } return 0; } |
![]() |
作者:王雨濛;新浪微博:@吉祥村码农;来源:《程序控》博客 -- http://www.cnblogs.com/devymex/ 此文章版权归作者所有(有特别声明的除外),转载必须注明作者及来源。您不能用于商业目的也不能修改原文内容。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库