题解:UVA12765 Factorial Products

· · 题解

思路

因为 3s 的时限且 n 和 m 都小于等于 1000 ,所以考虑暴力实现。

又考虑到 long long 可能会爆炸,所以使用 __int128 来存储数据。

然后直接模拟即可。

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    for(;;)
    {
        long long n,m;
        __int128 a=1,b=1;
        cin>>n>>m;
        if(n==0&&m==0)break;
        for(int j=1;j<=n;j++)
        {
            int c;
            cin>>c;
            for(int k=1;k<=c;k++)
            {
                a*=k;
            }
        }
        for(int j=1;j<=m;j++)
        {
            int c;
            cin>>c;
            for(int k=1;k<=c;k++)
            {
                b*=k;
            }
        }
        if(a==b)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}