您的位置:首页 > 产品设计 > UI/UE

CSUOJ 1638 Continued Fraction

2015-06-04 23:06 585 查看

1638: Continued Fraction

Time Limit: 1 Sec Memory Limit: 128 MB

Description

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m,a1[20],a2[20];
LL gcd(LL x,LL y){
return y?gcd(y,x%y):x;
}
void dfs(LL &A,LL &B,int *arr,int cur,int dep){
if(cur == dep-1){
A = arr[cur];
B = 1;
}
if(cur >= dep-1) return;
LL tmpA,tmpB;
dfs(tmpA,tmpB,arr,cur+1,dep);
LL theGCD = gcd(A = arr[cur]*tmpA + tmpB,B = tmpA);
A /= theGCD;
B /= theGCD;
}
void print(LL x,LL y){
LL GCD = gcd(x,y);
LL tmp = (x /= GCD)/(y /= GCD),p = x - tmp*y;
printf("%lld%c",tmp,p?' ':'\n');
if(p) print(y,p);
}
int main(){
while(~scanf("%d %d",&n,&m)){
for(int i = 0; i < n; ++i) scanf("%d",a1+i);
for(int i = 0; i < m; ++i) scanf("%d",a2+i);
LL A1 = 0,B1 = 1,A2 = 0,B2 = 1;
dfs(B1,A1,a1,1,n);
dfs(B2,A2,a2,1,m);
A1 += a1[0]*B1;
A2 += a2[0]*B2;
print(A1*B2 + A2*B1,B1*B2);
print(A1*B2 - A2*B1,B1*B2);
print(A1*A2,B1*B2);
print(A1*B2,A2*B1);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: