• 【CodeVS 1288】埃及分数


    http://codevs.cn/problem/1288/
    loli秘制面向高一的搜索,好难啊QAQ
    我本来想按照分母从大到小搜,因为这样分母从小到大枚举到的第一个可行方案就是最优方案。
    但貌似会T。。。
    所以按照分母从小往大搜,分母得有个上界。
    设分母为(num),则(frac{step}{num}geqfrac{a}{b},num leq frac{b·step}a),且(frac ab-frac 1{num}=frac{a·num-b}{b·num})
    (step)为IDA*枚举的步数。

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    
    int step;
    ll g, ans[1003], num[1003];
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    
    void dfs(ll a, ll b, int down, int tmp) {
    	if (a < 0) return;
    	
    	g = gcd(a, b); a /= g; b /= g;
    	if (tmp == step) {
    		if (a != 1 || num[tmp - 1] >= b) return;
    		num[tmp] = b;
    		if (!ans[1] || num[tmp] < ans[tmp])
    			memcpy(ans, num, sizeof(ll) * (step + 1));
    		num[tmp] = 0;
    		return;
    	}
    	
    	int up = (int) floor(1.0 * b * step / a) + 1;
    	for (int i = down; i <= up; ++i) {
    		num[tmp] = i;
    		dfs(a * i - b, b * i, i + 1, tmp + 1);
    	}
    	num[tmp] = 0;
    }
    
    int main() {
    	ll a, b; int up;
    	scanf("%lld%lld", &a, &b);
    	if (b % a == 0) {
    		printf("%lld
    ", b / gcd(a, b));
    		return 0;
    	}
    	
    	for (step = 2; ; ++step) {
    		up = (int) floor(1.0 * b * step / a);
    		for (int i = 2; i <= up; ++i) {
    			num[1] = i;
    			dfs(a * i - b, b * i, i + 1, 2);
    		}
    		num[1] = 0;
    		if (ans[1]) break;
    	}
    	
    	for (int i = 1; i <= step; ++i) {
            printf("%lld", ans[i]);
            if (i != step) putchar(' ');
            else putchar('
    ');
    	}
    	return 0;
    }
    
  • 相关阅读:
    1.两数之和
    [Udemy] ES 7 and Elastic Stack
    [Udemy] ES 7 and Elastic Stack
    Common Linux Commands 日常工作常用Linux命令
    ELK 学习
    web 3d 技术预研及数据可视化技术
    AWS Cloud Practioner 官方课程笔记
    怎么用 pytorch 查看 GPU 信息
    ECG 项目预研
    怎么查看keras 或者 tensorflow 正在使用的GPU
  • 原文地址:https://www.cnblogs.com/abclzr/p/6071021.html
Copyright © 2020-2023  润新知