uva 107 The Cat in the Hat

来源:互联网 发布:结对编程搞笑图片 编辑:程序博客网 时间:2024/06/05 02:38

原题:
A clever cat walks in to 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) help er cats inside its hat. Each helper cat also has help er 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 constan t, N . The heigh t 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 heigh ts are p ositiv e in tegers.
Given the heigh t of the initial cat and the number of worker cats (of height one), and 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’ heigh ts (the heig t of a stack of all cats standing one on top of another).
翻译:
(来自lucky 猫)
一隻神奇聰明貓走進了一間亂七八糟的房間,他不想自己動手收拾,他決定要找幫手來工作。於是他從他的帽子中變出了N隻小貓來幫他(變出來的貓,高度為原來貓的 1/(N+1) )。這些小貓也有帽子,所以每一隻小貓又從他的帽子中變出N隻小小貓來幫他。如此一直下去,直到這些小小小….貓小到不能再小(高度=1),他們的帽子無法再變出更小的貓來幫忙,而這些最小的貓只得動手打掃房間。注意:所有貓的高度都是正整數。
在這個問題中,給你一開始那隻貓的高度,以及最後動手工作的貓的數目(也就是高度為1的貓的數目)。要請你求出有多少隻貓是沒有在工作的,以及所有貓的高度的總和。

Input

每組測試資料一列,有2個正整數分別代表一開始那隻貓的高度,以及最後動手工作的貓的數目。0 0代表輸入結束。

Output

每組測試資料輸出一列,包含2個正整數分別代表有多少隻貓是沒有在工作的,以及所有貓的高度的總和。

Sample Input

216 125
5764801 1679616
64 1
0 0
Sample Output

31 671
335923 30275911
6 127

#include<bits/stdc++.h>using namespace std;//fstream in,out;int main(){    ios::sync_with_stdio(false);//  in.open("data.txt");//  out.open("input.txt");    int tall,team;    int x,tot,n,remain;    while(cin>>tall>>team,tall+team)    {        if(tall==team&&tall==1)        {            cout<<"0 1"<<endl;            continue;        }        int tmp;        x=1;        while(true)        {            tmp=static_cast<int>(pow(1.0+pow(team,1.0/x),x)+0.1);            if(tall==tmp)                break;            x++;        }        n=static_cast<int>(pow(team,1.0/x)+0.1);        tot=0;        for(int i=0;i<=x;i++)            tot+=static_cast<int>(tall*pow(1.0*n/(n+1.0),i*1.0)+0.1);        if(n==1)            remain=x;        else            remain=int((pow(n*1.0,x+1)-1)/(n-1.0)+0.1)-team;        cout<<remain<<" "<<tot<<endl;    }//  in.close();//  out.close();    return 0;}

解答:
=_= ! 我居然被这水题卡了半天,c++的语法不过关。

根据题意列出公式
设n为每次变出几只猫,x为变了几次,tall为开始猫的高度,team为最后工作猫的数量。
则有公式

tall=(n+1)x

nx=team

把下面的那个公式变成
n=team1x

然后代入到上面那个公式中,因为整数范围最多2^64 所以枚举x即可。然后求出n最后用基本的数学公式就可以了。

这里注意一个强制转换的知识

我的编译器是mingw32-g++.exe -std=c++11 a=int(c)和a=(int)(c)的功能貌似不一样,至少我的输出结果有问题。不过百度了一下,好像又没什么区别。这里推荐用C++语言中的static_cast。会造成输出结果的误差!所以

http://bbs.csdn.net/topics/390531221 关于两个形式的讨论

0 0
原创粉丝点击