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;
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;
}