迭代加深dfs 。数据要用long long巨坑
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<stack>
#include<sstream>
using namespace std;
int maxn, ok, a, b, k;
long long c[100000], ans[100000];
set<long long> ban;
long long gcb(long long a, long long b) {
return b == 0 ? a : gcb(b, a%b);
}
bool better(int cur)
{
for (int i = cur; i >= 0; i--) {
if (ans[i] != -1 && c[i] != ans[i])
{
return c[i] < ans[i];
}
}
return true;
}
void dfs(int cur, long long start, long long a, long long b) {
if (cur == maxn - 1) {
int m = gcb(a, b);
a /= m;
b /= m;
c[cur] = b;
if (a == 1 && !ban.count(b))
{
ok = 1;
if (better(cur))
memcpy(ans, c, sizeof(long long)*(cur + 1));
}
return;
}
start = max(start, b / a + 1);
for (long long i = start;; i++) {
if (ban.count(i)) continue;
if ((maxn - cur)*b <= a * i)
return;
long long c1 = a * i - b;
long long d1 = b * i;
int m = gcb(c1, d1);
c1 /= m;
d1 /= m;
c[cur] = i;
dfs(cur + 1, i + 1, c1, d1);
}
}
int main() {
int t, kase = 0;
cin >> t;
while (t--) {
cin >> a >> b >> k;
ban.clear();
ok = 0;
while (k--) {
long long temp;
cin >> temp;
ban.insert(temp);
}
for (maxn = 2;; maxn++) {
memset(ans, -1, sizeof(ans));
dfs(0, b / a + 1, a, b);
if (ok)
break;
}
printf("Case %d: %d/%d=", ++kase, a, b);
for (int i = 0; i < maxn; i++)
{
printf("1/%lld", ans[i]);
if (i != maxn - 1) printf("+");
else
{
printf("\n");
}
}
}
return 0;
}
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<stack>
#include<sstream>
//#include <Eigen/Dense>
//#include <Eigen/Geometry>
//using namespace Eigen;
using namespace std;
typedef long long LL;
int a, b, k, maxd, ok;
const int maxn = 1e5 + 10;
LL v[maxn], ans[maxn];//v为dfs时的中间
set<LL> ban;
LL gcd(LL a, LL b) //辗转相除法,求最大公因子
{
return b == 0 ? a : gcd(b, a%b);
}
// 返回满足1/c <= a/b的最小c
LL get_first(LL a, LL b)
{
return b / a + 1;
}
// 如果当前解v比目前最优解ans更优,更新ans
bool better(int d) //前d个,这时d只能是maxd,
{
for (int i = d; i >= 0; i--)
if (v[i] != ans[i])
return ans[i] == -1 || v[i] < ans[i];//没被访问过,或者是当前序列更优(因为题目要求数量相同时,最小分数越大越好)
return false;
}
// 当前深度为d,分母(注意是分母)不能小于from,分数之和恰好为aa/bb
void dfs(int cur, LL from, LL aa, LL bb)
{
if (cur == maxd)
{
if (bb % aa || ban.count(bb/aa))
return; // aa/bb必须是埃及分数(任何数mod1都等于0,可以起到一部分剪枝的作用)
v[cur] = bb / aa;
if (better(cur))
{
memcpy(ans, v, sizeof(LL)*(cur + 1));
ok = 1;
}
return;
}
from = max(from, get_first(aa, bb));
for (int i = from; ; i++) //进行枚举
{
// 剪枝:如果剩下的maxd+1-d个分数全部都是1/i,加起来仍然不超过aa/bb,则无解
if (aa*i >= bb * (maxd - cur + 1)) return;//从第0层开始算的,总共有maxn+1个
if (ban.count(i))
continue;
v[cur] = i;
// 计算aa/bb - 1/i,设结果为a2/b2
LL a1 = aa * i - bb;
LL b1 = bb * i;
LL g = gcd(a1, b1); // 以便约分
//除以最大公因数,防止下次计算aa*i和 bb * (maxd - cur + 1)溢出。此题数据实际不会溢出
dfs(cur + 1, i + 1, a1 / g, b1 / g);
//dfs(cur + 1, i + 1, a1, b1);
}
return;//注意这些返回条件
}
int main()
{
int kase = 0;
int cnt;
cin >> cnt;
while (cnt--)
{
ban.clear();
cin >> a >> b >> k;
while (k--)
{
LL temp;
cin >> temp;
ban.insert(temp);
}
ok = 0;
for (maxd = 1; ; maxd++) //从小到大,枚举深度上限
{
memset(ans, -1, sizeof(ans));
dfs(0, get_first(a, b), a, b);
if (ok)
break;
}
cout << "Case " << ++kase << ": ";
cout << a << "/" << b << "=";
for (int i = 0; i < maxd; i++)
cout << "1/" << ans[i] << "+";
cout << "1/" << ans[maxd] << "\n";
}
return 0;
}