UVALive 6176 Faulhaber's Triangle C++大数模拟

题目链接:点击打开链接

大数模拟

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 450;
struct node {
	ll x, y;
	void print() {
		if (y == 1 || x == 0)
			printf("%lld", x);
		else
			printf("%lld/%lld", x, y);
		puts("");
	}
	void repair() {
		if (x != 0) {
			ll c = __gcd(x, y);
			x /= c;
			y /= c;
		}
		if (x == 0)
			y = 1;
		if (y < 0) {
			x = -x;
			y = -y;
		}
	}
	node() {
		x = 0;
		y = 1;
	}
};
node a[N][N], one, sum[N];

node sub(node i, node j) {
	node re;
	ll c = i.y / __gcd(i.y, j.y) * j.y;
	i.x = c / i.y * i.x;
	j.x = c / j.y * j.x;
	re.x = i.x - j.x;
	re.y = c;
	re.repair();
	return re;
}
node add(node i, node j) {
	node re;
	ll c = i.y / __gcd(i.y, j.y) * j.y;
	i.x = c / i.y * i.x;
	j.x = c / j.y * j.x;
	re.x = i.x + j.x;
	re.y = c;
	re.repair();
	return re;
}
int main() {
	one.x = 1;
	for (int i = 0; i <= 400; ++i) {
		a[i][1] = sub(one, sum[i]);
		int k = 1;
		for (int j = i + 1; j <= 410; ++j) {
			++ k;
			a[j][k] = a[j - 1][k - 1];
			a[j][k].x *= j;
			a[j][k].y *= k;
			a[j][k].repair();
			sum[j] = add(sum[j], a[j][k]);
		}
	}
			
	int cas, n, m;
	scanf("%d", &cas);
	while (cas -- > 0) {
		scanf("%d", &n);
		printf("%d ", n);
		
		scanf("%d%d", &n, &m);
		a[n][m].print();
	}
	return 0;
}


你可能感兴趣的:(UVALive 6176 Faulhaber's Triangle C++大数模拟)