Start: Jun, 28, 2019 13:00:00
2019年度暑期短学期第四天-助教场
End: Jun, 30, 2019 23:59:00
Time elapsed:
Time remaining:

Problem_ID: I
Result: Accepted
Time: 3ms
Memory: 1756kB
Author: 2018212212064
In contest: 1289

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
char fac[10];
int read(char s[])
{
	int p = 0, f = 1, ans = 0;
	for (; !isdigit(s[p]) && s[p] != '-' && s[p]; ++p);
	if (s[p] == '-')
	{
		f = -1;
		++p;
	}
	for (; s[p]; ++p)
		ans = ans * 10 + s[p] - '0';
	return ans * f;
}

long long gcd(long long a, long long b)
{
	return b ? gcd(b, a % b) : a;
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n, m;
		long long a = 1, b = 1, k = 0;
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; ++i)
		{
			scanf("%s", fac);
			if (fac[0] == 'x' && fac[1] == '^')
				k += read(fac);
			else if (strcmp(fac, "x") == 0 || strcmp(fac, "sinx") == 0 || strcmp(fac, "arcsinx") == 0 || strcmp(fac, "tanx") == 0 || strcmp(fac, "arctanx") == 0 || strcmp(fac, "e^x-1") == 0 || strcmp(fac, "ln(x+1)") == 0)++k;
			else if (strcmp(fac, "1-cosx") == 0)k += 2, b *= 2;
			else a *= read(fac);
		}
		for (int i = 0; i < m; ++i)
		{
			scanf("%s", fac);
			if (fac[0] == 'x' && fac[1] == '^')
				k -= read(fac);
			else if (strcmp(fac, "x") == 0 || strcmp(fac, "sinx") == 0 || strcmp(fac, "arcsinx") == 0 || strcmp(fac, "tanx") == 0 || strcmp(fac, "arctanx") == 0 || strcmp(fac, "e^x-1") == 0 || strcmp(fac, "ln(x+1)") == 0)--k;
			else if (strcmp(fac, "1-cosx") == 0)k -= 2, a *= 2;
			else b *= read(fac);
		}
		if (k > 0)
			puts("0");
		else if (k < 0)
			puts("INF");
		else
		{
			int flag = (a > 0 && b > 0) || (a < 0 && b < 0);
			a = abs(a);
			b = abs(b);
			long long c = gcd(a, b);
			if (flag == 0)
				putchar('-');
			if (c == b)
				printf("%lld\n", a / b);
			else
				printf("%lld/%lld\n", a / c, b / c);
		}
	}
//	system("pause");
}