1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 LL tmpans[100000],ans[100000]; 5 int maxd; 6 /** 7 *学习:1.memcpy:复制结构体时加&, 8 数组时不加,数组名即地址 9 2.迭代加深搜索,剪枝条 10 * */ 11 int get_maxb(LL a,LL b){ 12 // 13 //for(int i = 1 ; ; i ++){ 14 // if(a*i >= b) return i; 15 //} 16 return b/a+1; 17 } 18 bool is_better(int d){ 19 for(int i = d ; i >= 0 ; i --) if(tmpans[i] != ans[i]){ 20 return ans[i] == -1 || tmpans[i] < ans[i]; 21 } 22 return false; 23 } 24 25 bool dfs(int d,int from,LL a,LL b){ 26 if(d == maxd){ 27 if(b%a) return false; 28 tmpans[d] = b / a; 29 if(is_better(d)) memcpy(ans,tmpans,sizeof(LL) * (d+1)); 30 return true; 31 } 32 // 33 bool ok = false; 34 from = max(from,get_maxb(a,b)); 35 for(int i = from ; ; i ++){ 36 // 剪枝 37 if(a*i >= (maxd-d+1) * b) break; 38 tmpans[d] = i; 39 // 剩下的分数 40 LL bb = b*i; 41 LL aa = a*i - b; 42 LL g = __gcd(aa,bb); 43 if(dfs(d+1,i+1,aa/g,bb/g)) ok = true; 44 } 45 return ok; 46 } 47 48 int main(){ 49 LL a,b; 50 int kase = 1; 51 while(scanf("%lld%lld",&a,&b) != EOF && (a || b)){ 52 bool ok = false; 53 memset(ans,-1,sizeof(ans)); 54 for(maxd = 1 ; ; maxd ++){ 55 if(dfs(0,get_maxb(a,b),a,b)) { ok = true; break; } 56 } 57 printf("Case %d:%lld/%lld=",kase++,a,b); 58 for(int i = 0 ; i < maxd ; i ++) printf("%d/%lld+",1,ans[i]); 59 printf("%d/%lld\n",1,ans[maxd]); 60 } 61 return 0; 62 }
时间: 2024-11-08 23:54:47