题意:
思路:
暴力求解
#include <bits/stdc++.h>
using namespace std;
const int maxn =1e8+10;
typedef unsigned long long ll;
ll a[maxn],b[maxn];
int main()
{
ios::sync_with_stdio(false);
int t,cas=1,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int j=1; j<=n; j++)
cin>>b[j];
if(n==1)//特判n==1;
{
ll x=__gcd(a[1],b[1]);
printf("Case #%d: %llu %llu\n",cas++,b[1]/x,a[1]/x);
continue;
}
ll fz,fm;
fz=b[n]+a[n]*a[n-1];//先算最下面一项,因为比较特殊
fm=a[n];
for(int i=n-2; i>=1; i--)//保存分子分母
{
ll t=fz;
fz = fm;
fm = t;
fz = fz*b[i+1];
fz = fz+fm*a[i];
}
ll t=fm;
fm = fz;
fz = t*b[1];
ll x=__gcd(fz,fm);
fz/=x;
fm/=x;
printf("Case #%d: %llu %llu\n",cas++,fz,fm);
}
return 0;
}