90分 最后一个点TLE(开O2能过)
  • 板块P1763 埃及分数
  • 楼主Bodhi
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/8/16 09:35
  • 上次更新2023/10/27 15:12:33
查看原帖
90分 最后一个点TLE(开O2能过)
364848
Bodhi楼主2022/8/16 09:35

参考的是第二篇题解

#include <bits/stdc++.h>
using namespace std;

#define reg register int
using ll = long long;
const int R = 1e7 + 10, INF = 0x3f3f3f3f;
int temp[R], ans[R], maxdep = 1;
inline int gcd(int x, int y)
{
	int z;
	while (y)
	{
		z = x % y;
		x = y;
		y = z;
	}
	return x;
}
struct Node
{
	int a, b;
	Node(int c, int d)
	{
		int g = gcd(c, d);
		a = c / g, b = d / g;
	}
	Node() { a = 0, b = 1; }
	bool operator<(const Node &t) const { return a * t.b < b * t.a; }
	bool operator<=(const Node &t) const { return a * t.b <= b * t.a; }
	bool operator>(const Node &t) const { return a * t.b > b * t.a; }
	bool operator>=(const Node &t) const { return a * t.b >= b * t.a; }
	bool operator==(const Node &t) const { return a * t.b == b * t.a; }
	Node operator+(const Node &t) const
	{
		int fz = (a * t.b) + (b * t.a), fm = b * t.b, g = gcd(fz, fm);
		return {fz / g, fm / g};
	}
	Node operator-(const Node &t) const
	{
		int fz = (a * t.b) - (b * t.a), fm = b * t.b, g = gcd(fz, fm);
		return {fz / g, fm / g};
	}
} x;
void dfs(int p, Node sum, int last)
{
	if (sum > x || p > maxdep)
	{
		return;
	}
	if (p == maxdep)
	{
		Node left = x - sum;
		if (left.a == 1 && left.b <= 1e7 && left.b > last)
		{
			if (left.b < ans[maxdep])
			{
				for (reg j = 1; j < maxdep; ++j)
				{
					ans[j] = temp[j];
				}
				ans[maxdep] = left.b;
			}
		}
		return;
	}
	for (reg j = last + 1; j <= ans[maxdep]; ++j)
	{
		if (sum + Node(maxdep - p + 1, j) < x)
		{
			return;
		}
		temp[p] = j;
		dfs(p + 1, sum + Node(1, j), j);
	}
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int a, b;
	cin >> a >> b;
	x = Node(a, b);
	while (true)
	{
		ans[maxdep] = INF;
		dfs(1, {0, 1}, 0);
		if (ans[maxdep] != INF)
		{
			for (reg j = 1; j <= maxdep; ++j)
			{
				cout << ans[j] << ' ';
			}
			return 0;
		}
		++maxdep;
	}
	return 0;
}

大家能帮忙看一下还有哪里能优化吗

2022/8/16 09:35
加载中...