标签:
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define long long LL bool dfs(int d,int from,LL aa, LL bb){ if(d == maxd){ if(bb%aa) return false; v[d] = bb/aa; if(better(d)) memccpy(ans, v, sizeof(LL)*(d+1)); } bool ok =false; from=max(from, get_fitst(aa,bb)); for(int i=from;;i++){ ///减枝:如果剩下的maxd+1-d个分数全部是1/i,加起来仍然不超过aa/bb,则无解 if(bb * (maxd+1-d) <= i*aa) break; v[d]=i; ///计算aa/bb - 1/i,设结果为a2/b2 LL b2 = bb*i; LL a2 = aa*i - bb; LL g = gcd(a2,b2); if(dfs(d+1,i+1,a2/g,b2/g)) ok =true; } return ok; } int main() { int ok=0; for(int maxd=1;;maxd++){ memset(ans,-1,sizeof(ans)); if(dfs(0,get_first(a,b), a, b)) { ok=1;break; } ///其中get_first(a,b)是满足1/c <= a/b的最小c } return 0; }
标签:
原文地址:http://blog.csdn.net/a197p/article/details/45309467