请大家帮个忙呗
查看原帖
请大家帮个忙呗
183603
SUNCHAOYI废物生成器楼主2022/5/26 18:56
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define init(x) memset (x,0,sizeof (x))
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 205;
const int MOD = 1e9 + 7;
inline int read ();
ll a,b,g,dep,t,k;
ll ans[MAX],p[MAX];
bool ok,vis[1005];
ll gcd (ll x,ll y);
void dfs (ll step,ll x,ll y);
bool check ();
int main ()
{
	//freopen (".in","r",stdin);
	//freopen (".out","w",stdout);
	t = read ();
	for (int i = 1;i <= t;++i)
	{
		init (vis);init (p);init (ans);
		a = read ();b = read ();k = read ();
		for (int j = 1;j <= k;++j) vis[read ()] = 1;
		g = gcd (a,b);
		printf ("Case %d: %d/%d=",i,a,b);
		a /= g;b /= g;
		p[0] = 1;dep = ok = 0;
		while (++dep)
		{
			dfs (1,a,b);
			if (ok)
			{	
				for (int j = 1;j < dep;++j) printf ("1/%lld+",ans[j]);
				printf ("1/%lld\n",ans[dep]);
				break;
			}
		}
	}
	return 0;
}
inline int read ()
{
    int s = 0;int f = 1;
    char ch = getchar ();
    while ((ch < '0' || ch > '9') && ch != EOF)
	{
        if (ch == '-') f = -1;
        ch = getchar ();
    }
    while (ch >= '0' && ch <= '9')
	{
        s = s * 10 + ch - '0';
        ch = getchar ();
    }
    return s * f;
}
ll gcd (ll x,ll y)
{
	return !y ? x : gcd (y,x % y);
}
void dfs (ll step,ll x,ll y)
{
	if (step > dep) return ;//超过层数 
	if (x == 1 && y > p[step - 1])//ans + (1 / y)
	{
		if (y <= 1000)//不能使用的数字
			if (vis[y]) return ;
		p[step] = y;
		if (!ok || check ())//更新最优解 
			for (int i = 1;i <= dep;++i) ans[i] = p[i];
		ok = 1;
		return ;
	}
	ll l = max (y / x,p[step - 1] + 1),r = (dep - step + 1) * y / x;//边界处理 
	if (ok) r = min (r,ans[dep] - 1);//右边界的进一步明确 
	r = min (r,(ll)1e7);
	for (ll i = l;i <= r;++i)
	{
		if (i <= 1000)
			if (vis[i]) continue;
		p[step] = i;
		ll dx = 1ll * x * i - y,dy = 1ll * y * i;//dfs搜索处理 
		g = gcd (dx,dy);
		dx /= g;dy /= g;
		dfs (step + 1,dx,dy);
	}
}
bool check ()//按照题意保留最佳的答案
{
	for (ll i = dep;i;--i)
	{
		if (ans[i] == p[i]) continue;
		if (ans[i] < p[i]) return 0;
		else return 1;
	}
	return 0;
}

提交了好多遍都是WA,和别的程序拍也没有查出来,因此希望有大佬能帮个忙看看,谢谢了!!!

2022/5/26 18:56
加载中...