Factorial Products

Description

点击查看题面

Factorial is just a game of multiplications. Formally, it can be defined as a recurrence relation:

Fact (0) = 1

Fact (n) = n * Fact(n-1), for all integers n > 0

This problem is all about multiplications, more and more multiplications. It is a game of multiplications of factorials.

The Problem:
You will be given three lists of numbers: A, B and C. You have to take the factorials of all the numbers in each list and multiply them to get ProFact(A), ProFact(B), ProFact(C). Then report which product is the largest.

For example, consider the lists A = {2, 4, 7}, B = {0, 1, 9} and C = {2, 3, 5, 5}. Then,

ProFact(A) = 2! 4! 7! = 241, 920
ProFact(B) = 0! 1! 9! = 362, 880
ProFact(C) = 2! 3! 5! * 5! = 172,800

So, the largest product for this example is ProFact(B).

输入描述:

The first input line contains a positive integer, n, indicating the number of test cases. Each test case consists of four input lines. The first line consists of three positive integers providing, respectively, the size for the lists A, B and C. The last three lines contain, respectively, the elements (non-negative integers) in lists A, B and C.

All the values in the input file will be less than 2,501.

输出描述:

For each test case, output “Case #t: h” in a line, where t is the case number (starting with 1) and h is the list name with the highest product. If two or three lists are tied for the highest product, print “TIE”. Follow the format illustrated in Sample Output. Assume that, if the pairwise product values differ, then the relative difference of these products will differ by at least 0.01% of the largest product.

样例输入
3
3 3 4
2 4 7
0 1 9
2 3 5 5
2 2 2
2 3
3 2
2 2
3 3 3
1 3 5
2 4 6
1 4 7
样例输出
Case #1: B
Case #2: TIE
Case #3: C

Probelm solving

这道题的意思很好理解,就是给你三组数,让你比较哪组数里面所有数的阶乘和最大。保证数据小于2500。
阶乘是一个很大的数,如果直接暴力计算肯定是存不下的。所以我们取一个对数,因为我们只需要比较大小,不需要进一步计算。
因为,并且数据最大也就2500,所以我们首先可以通过递推预处理出每一个值的阶乘所对应的对数。然后后面的相乘也直接对数相加即可。
因为精度问题,题目中也说到了,所以我们比较大小的时候不能直接用等于判断(WA死我了),看两个数是否相等我们只需要看他们的差和1e-6的大小关系即可~

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
const int maxn = 3e3+10;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double ohuo = 1e-6;
int d[4][2]={1,0,0,1,-1,0,0,-1};
long double fac[maxn];
int main() {
    ios::sync_with_stdio(0);
    #ifdef Uncle_drew
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #else
    #endif
    fac[1]=0;
    for(int i=2;i<=2510;i++){
        fac[i]=fac[i-1]+log(i);
        // cout<<fac[i]<<endl;
    }//预处理
    int t,a,b,c,mid;
    cin>>t;
    for(int ca=1;ca<=t;ca++){
        cin>>a>>b>>c;
        double x=0,y=0,z=0;
        while(a--){
            cin>>mid;
            x+=fac[mid];
        }
        while(b--){
            cin>>mid;
            y+=fac[mid];
        }
        while(c--){
            cin>>mid;
            z+=fac[mid];
        }
        cout<<"Case #"<<ca<<": ";
        if(x-y>ohuo&&x-z>ohuo){
            cout<<"A";
        }
        else if(y-x>ohuo&&y-z>ohuo){
            cout<<"B";
        }
        else if(z-y>ohuo&&z-x>ohuo){
            cout<<"C";
        }
        else{
            cout<<"TIE";
        }
        cout<<endl;
    }
    return 0;
}