Uva 107 帽子里的猫

再次出现奇怪问题怎么也AC不了的。大致算法思路没有问题,udebug也完全一致,但是提交后总是T。

经过几次测试发现,可能是在某个输入值非常大的样例挂掉的。按常理(且参照其他已经AC人的说法验证)应该不会有次幂非常大的,所以怀疑是错过了正确值。


题:

The number of cats inside each (non-smallest) cat’s hat is aconstant, N. Theheight of these cats-in-a-hatis 1/(N+1) times the height of the cat whose hat they are in.

The smallest cats are of height one; these are the catsthat get the work done.

All heights are positive integers.

Given the height of the initial cat and the number ofworker cats (of height one), find the number of cats that are not doing any work (catsof height greater than one) and also determine the sum of all the cats’ heights (the height ofa stack of all cats standing one on top of another)

input

The input consists of a sequence ofcat-in-hat specifications. Each specification is a single line consisting of two positive integers, separatedby white space. The first integer is the height of the initial cat, and thesecond integer is the number of worker cats.

A pair of ‘0’s on a line indicates the end of input.

Output

For each input line (cat-in-hatspecification), print the number of cats that are not working, followedby a space, followed by the height of the stack of cats. There should beone output line for each input line other than the ‘0 0’ that terminates input.


思路

LittleCat =1/(N+1)cat

h*n/(n+1)

/*

* 解题思路:

*     题意:输入最开始的猫的高度和最后干活猫的数量求有多少只猫不用干活和所有猫的高度和是多少

* 把题意理清就好( 设 H , M为输入的两个值 , K为产生了多少代 , N为每代产生多少只猫 )不知道没有关系,可以先设未知数解 6*6*6 3^3*2^3 n=5 k=3 m=125

*     N ^ k = M   ,   H * ( 1/(N+1 ) ) ^K = 1;

*     N=pow(M,1/k)一定是整数 H=(N+1)^k  pow(H,1/k)=N+1=pow(M,1/k)+1   开k次方pow函数,第二个参数使用小数表示

*       largeNum+=N^k(0~k-1)sum+=个数*高度 N^k*H*( 1/( N+1 ) ) ^k(0~k)

*       K = log( M ) / log( N ) = log( H ) / log(N+1 );

*     N从1开始遍历,直到上面两个等式相减绝对值小于1e-10 则找到N值(这一步一样都是暴力求解)

*     然后即可求出K值,最后做些相加运算即可

*/

debug过程解决的问题:

小猫数不加高1的,但是总高度要

超过时间限制

浮点数要用fabs而不是abs

莫名其妙一直T,OJ样例没有按题目要求的结束符号,任然需要判断scanf是否遇到文件结束符

有可能次幂极大,或错过了正确值

//16 1
#include<stdio.h>
#include<math.h> 
//#define LOCAL

int main(){
#ifdef LOCAL
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	long long initH,workNum,largeNum,sumH,n,k,num,h,i;
	long double temp,temp1;
	while(scanf("%I64d %I64d",&initH,&workNum)==2){
		if(!initH&&!workNum)break;
		else if(initH==1){//特殊情况 
			largeNum=0;
			sumH=workNum;
		}else{
			largeNum=0;//不工作的小猫数量 
			sumH=0;//总高度 
			num=1;//当前一层的小猫数量 
			h=initH;//当前小猫高度 
//			for(k=1;k<=workNum*initH;k++){
			for(k=1;k<=initH*workNum;k++){//解出小猫有多少代 
//				(int)floor(0.5+pow(h,1.0/k));
				temp=pow((float)workNum,1.0/k)+1.0;
				temp1=pow((float)initH,1.0/k);
//				printf("a:%lf b:%lf rest:%lf\n",pow((float)initH,1.0/k),temp,temp-pow((float)initH,1.0/k));
//				if(fabs(temp-pow((float)initH,1.0/k))<0.0001){
				if(fabs(temp-temp1)<0.001){
//					printf("equel\n");
					break;
				}
			}
			n=(int)floor(0.5+temp)-1;//代入得每个帽子小猫数量 
//			printf("n:%d k:%d\n",n,k);
			for(i=0;i<k;i++){//模拟摘帽过程 
				sumH+=num*h;
				largeNum+=num;
				num*=n;
				h=h/(n+1);
//				temp=floor(pow((float)n,(float)k)+0.5);
//				largeNum+=(int)temp;
//				sumH+=(int)temp*initH*pow(1.0/(n+1),(float)k);
			}
			sumH+=workNum;

		}

		printf("%I64d %I64d\n",largeNum,sumH);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lagoon_lala/article/details/80376100
107