比赛 202110省实验桐柏一中普及组联赛 评测结果 AAAAAAAAAA
题目名称 分数运算 最终得分 100
用户昵称 PYD1 运行时间 0.094 s
代码语言 C++ 内存使用 1.87 MiB
提交时间 2021-10-18 19:00:55
显示代码纯文本
#define MAXN 4000

#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

using namespace std;

inline int max(int a,int b) {return a > b ? a : b;}

struct bign{
	int len,a[MAXN + 1];
	bign () {memset(a,0,sizeof(a)),len = 1;}
	bign (int num){
		char s[MAXN + 1];
		sprintf(s,"%d",num);
		*this = s;
	}
	bign (char * num){
		if (num[0] == '0'){
			len = 1,memset(a,0,sizeof(a));
		}else{
			for (int i = 0;num[i] == 48;num++) ;
			len = strlen(num);
			for (int i = 0;i < len;i++) a[i] = (num[len - i - 1] ^ 48);
		}
	}
	bign operator = (int num){
		char s[MAXN + 1];
		sprintf(s,"%d",num);
		*this = s;
		return *this;
	}
	bign operator = (char * num){
		if (num[0] == '0'){
			len = 1,memset(a,0,sizeof(a));
		}else{
			for (int i = 0;num[i] == 48;num++) ;
			len = strlen(num);
			for (int i = 0;i < len;i++) a[i] = (num[len - i - 1] ^ 48);
		}
		return *this;
	}
	bign operator + (const bign & b){
		bign ans;
		ans.len = 0;
		for (int i = 0,j = 0;j || i < max(len,b.len);i++){
			if (i < len) j += a[i];
			if (i < b.len) j += b.a[i];
			ans.a[ans.len++] = j % 10;
			j /= 10;
		}
		return ans;
	}
	bign operator += (bign & b) {return *this = *this + b;}
	void clean(){
		while (len > 1 && !a[len - 1]) --len;
		if (len == 0) len = 1,a[0] = 0;
	}
	bign operator * (const bign & b){
		bign ans;
		ans.len = len + b.len;
		for (int i = 0;i < len;i++){
			for (int j = 0;j < b.len;j++) ans.a[i + j] += a[i] * b.a[j];
		}
		for (int i = 0;i < ans.len;i++) ans.a[i + 1] += ans.a[i] / 10,ans.a[i] %= 10;
		ans.clean();
		return ans;
	}
	bign operator *= (bign & b) {return *this = (*this) * b;}
	bign operator - (bign & b){//without negative result or numbers
		bign ans;
		ans.len = 0;
		for (int i = 0,j = 0;i < len;i++){
			int x = a[i] - j;
			if (i < b.len) x -= b.a[i];
			if (x >= 0) j = 0;
			else j = 1,x += 10;
			ans.a[ans.len++] = x;
		}
		ans.clean();
		return ans;
	}
	bign operator -= (bign & b) {return *this = *this - b;}
	bign operator / (bign & b){
		bign ans,f,ten = 10;
		for (int i = len - 1;i >= 0;i--){
			f *= ten;
			f.a[0] = a[i];
			while (f >= b) f -= b,ans.a[i]++;
		}
		ans.len = len,ans.clean();
		return ans;
	}
	bign operator /= (bign & b) {return *this = *this / b;}
	bign operator % (bign & b){
		bign t = *this / b * b;
		t = *this - t;
		t.clean();
		return t;
	}
	bign operator %= (bign & b) {return *this = *this % b;}
	bool operator < (bign & b){
		if (len != b.len) return len < b.len;
		for (int i = len - 1;i >= 0;i--){
			if (a[i] != b.a[i]) return a[i] < b.a[i];
		}
		return 0;
	}
	bool operator > (bign & b){
		if (len != b.len) return len > b.len;
		for (int i = len - 1;i >= 0;i--){
			if (a[i] != b.a[i]) return a[i] > b.a[i];
		}
		return 0;
	}
	bool operator == (bign & b){
		if (len != b.len) return 0;
		for (int i = len - 1;i >= 0;i--){
			if (a[i] != b.a[i]) return 0;
		}
		return 1;
	}
	bool operator != (bign & b) {return !(*this == b);}
	bool operator >= (bign & b) {return *this > b || *this == b;}
	bool operator <= (bign & b) {return *this < b || *this == b;}
};

inline int read(){
	int t = 0,f = 1;
	register char c = getchar();
	while (c < 48 || c > 57) f = (c == '-') ? (-1) : (f),c = getchar();
	while (c >= 48 && c <= 57) t = (t << 1) + (t << 3) + (c ^ 48),c = getchar();
	return f * t;
}

int n,m,a,b;

bign ans1,ans2,zero(0),one(1);

bign gcd(bign x,bign y){
	if (y == zero) return x;
	return gcd(y,x % y);
}

void update(){
	bign tmp = gcd(ans2,ans1);
	ans2 /= tmp,ans1 /= tmp;
}

int main(){
	freopen("fenshu.in","r",stdin);
	freopen("fenshu.out","w",stdout);
	n = read(),m = read(),a = read(),b = read();
	ans1 = bign(a),ans2 = bign(b);
	for (int i = 2;i <= n;i++){
		a = read(),b = read();
		ans1 = ans1 * bign(b) + ans2 * bign(a);
		ans2 = ans2 * b;
		update();
	}
	for (int i = 1;i <= m;i++){
		a = read(),ans2 = ans2 * a,update();
	}
	for (int i = ans1.len - 1;i >= 0;i--) printf("%d",ans1.a[i]);
	if (ans2 != one){
		putchar(' ');
		for (int i = ans2.len - 1;i >= 0;i--) printf("%d",ans2.a[i]);
	}
	return 0;
}