写一个分数类模拟一下就行了。。。。很多坑
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 505 #define maxm 10005 #define eps 1e-12 #define mod 998244353 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair //#define ls o<<1 //#define rs o<<1 | 1 //#define lson o<<1, L, mid //#define rson o<<1 | 1, mid+1, R //#pragma comment(linker, "/STACK:102400000,102400000") #define pii pair<int, int> typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} // head struct Fra { LL e, d; Fra(LL e = 0, LL d = 0) : e(e), d(d) {} void inv() { swap(e, d); if(d < 0) e = -e, d = -d; } void to() { LL g = __gcd(e, d); e /= g, d /= g; if(d < 0) e = -e, d = -d; } Fra operator + (const Fra& b) const { Fra res = Fra(e * b.d + b.e * d, d * b.d); res.to(); return res; } Fra operator - (const Fra& b) const { Fra res = Fra(e * b.d - b.e * d, d * b.d); res.to(); return res; } Fra operator * (const Fra& b) const { Fra res = Fra(e * b.e, d * b.d); res.to(); return res; } Fra operator / (const Fra& b) const { Fra res = Fra(e * b.d, d * b.e); res.to(); return res; } }r1, r2; LL a[maxn]; int n1, n2; void solve(Fra x) { int flag = 0; if(x.e == 0) printf("0"); while(x.d && x.e) { LL t = x.e / x.d; if(x.e < 0 && x.e % x.d) t--; if(flag == 0) flag = 1, printf("%lld", t); else printf(" %lld", t); x.e -= x.d * t; x.to(); x.inv(); } printf("\n"); } void work() { for(int i = 1; i <= n1; i++) scanf("%lld", &a[i]); r1 = Fra(a[n1], 1LL); for(int i = n1-1; i >= 1; i--) { r1.inv(); r1 = r1 + Fra(a[i], 1LL); } for(int i = 1; i <= n2; i++) scanf("%lld", &a[i]); r2 = Fra(a[n2], 1LL); for(int i = n2-1; i >= 1; i--) { r2.inv(); r2 = r2 + Fra(a[i], 1LL); } solve(r1 + r2); solve(r1 - r2); solve(r1 * r2); solve(r1 / r2); } int main() { int _ = 0; while(scanf("%d%d", &n1, &n2), n1 || n2) { printf("Case %d:\n", ++_); work(); } return 0; }