F. Easy Function

F.       Easy Function

 

Please help Xiao Ming Write a Super Fast function whichresult is like below:

int cmp(int a, int b, int c) {

   SuperLong factorial = 1;

   for(int i = 1; i <= a; i ++)

       factorial *= i;

 

   SuperLong power = 1;

   for(int i = 1; i <= c; i++)

       power *= b;

  1.  

   if(factorial < power)

       return -1;

   else if(factorial == power)

       return 0;

   else

       return 1;

}

SuperLong is a type of integer with unlimited length.

Input:

Inputcontains multiple test cases. One line one case, and each case is threeintegers, a, b, c. We guarantee that 1 <= a, b, c <= 20000.

Output:

For eachcase, print the function result, and one line one case.

Sample input and output

Sample input:

1 1 1

3 2 3

5 11 2

Sample output:

0

-1

-1



题意就是比较a!和b^c的大小,由于数据范围很大,要有技巧。

log(a*b)=log(a)+log(b)

我们可以把阶乘取对数写成和的形式,相应的对b^c作出调整,于是就变成了比较下面两者的大小

1.log(1)+log(2)+...+log(a-1)+log(a)

2. c*log(b)

这两者也不会超出范围,于是就可以直接比较大小

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <cmath>
using namespace std;
double base[20005];//预处理出所有的阶乘
int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	int a, b, c;
	base[1] = log(1.0);
	for (int i = 2; i<20001; i++)base[i] = base[i - 1] + log(1.0*i);
	while (cin >> a >> b >> c){
		double temp = 1.0*c*log((double)b);
		if (temp == base[a])cout << "0" << endl;
		else if (temp > base[a])cout << "-1" << endl;
		else cout << "1" << endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值