[Codevs 1228]埃及分数
Egyptian Fraction
第一道迭代加深搜索,《算法竞赛入门经典》上的例题。
注意分母这个数字可能很大,所以要用long long
在寻找满足 \(1/c \leq a/b\) 的最小的 \(c\) 时,可以知道\(c=\lceil a/b \rceil\),注意要将\(a/b\)转换成double
类型.
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXD=1000+1;
ll ans[MAXD],v[MAXD];
inline ll get_first(ll a,ll b) {
for(ll c=1;;c++) {
if(a*c>=b) return c;
}
}
inline bool better_ans(int maxd) {
if(!ans[maxd] || v[maxd]!=ans[maxd]) return !ans[maxd] || v[maxd]