Factorial Products eps1e-6

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©. 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 Input:
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.
The Output:
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.
Sample Input:
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
Sample Output:
Case #1: B
Case #2: TIE
Case #3: C

三个需要注意的点:

  1. 如果直接进行阶乘的乘法,很容易爆ll;这时对整个式子取log 那么所有的 * 即可转化为 +

  2. 注意是两个或三个列表的值均等于最大值,才是TIE;写的时候不要写成两个值相等就是TIE了(em…我这个憨憨就犯了这个低级错误 (ಥ_ಥ) )

  3. em… 还有eps的取值,一开始取的1e-8,WA了…
    之后改成1e-6…它A了!!! (到现在也没整明白 ╥﹏╥…)

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
const double eps=1e-6;
double f[2515]={0.0};
int main() {
    f[1]=0;
    for(int i=2;i<=2507;i++)f[i]=f[i-1]+log(i);
	int t,a,b,c,w;
	double x,y,z;
	scanf("%d",&t);
	for(int ca=1;ca<=t;ca++)
    {
        scanf("%d%d%d",&a,&b,&c);
        x=0,y=0,z=0;
        for(int i=1;i<=a;i++)
        {
            scanf("%d",&w);
            x+=f[w];
        }
        for(int i=1;i<=b;i++)
        {
            scanf("%d",&w);

            y+=f[w];
        }
        for(int i=1;i<=c;i++)
        {
            scanf("%d",&w);
            z+=f[w];
        }
        printf("Case #%d: ",ca);
        double maxn=max(x,max(y,z));
         int z1=0,z2=0,z3=0;
        if(abs(x-maxn)<eps)z1++;
        if(abs(y-maxn)<eps)z2++;
        if(abs(z-maxn)<eps)z3++;
        if(z1+z2+z3>1)puts("TIE");
        else
        {
            if(z1)puts("A");
            else if(z2)puts("B");
            else puts("C");
        }

    }
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值