• DFSID:埃及分数


    题目描述 
    Description

    在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数。 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的。 对于一个分数a/b,表示方法有很多种,但是哪种最好呢? 首先,加数少的比加数多的好,其次,加数个数相同的,最小的分数越大越 好。 如: 19/45=1/3 + 1/12 + 1/180 19/45=1/3 + 1/15 + 1/45 19/45=1/3 + 1/18 + 1/30, 19/45=1/4 + 1/6 + 1/180 19/45=1/5 + 1/6 + 1/18. 最好的是最后一种,因为1/18比1/180,1/45,1/30,1/180都大。 给出a,b(0<a<b<1000),编程计算最好的表达方式。

    输入描述 Input Description

    a b

    输出描述 Output Description

    若干个数,自小到大排列,依次是单位分数的分母。

    样例输入 Sample Input

    19 45

    样例输出 
    Sample Output
    5 6 18
    思路:
    对于此种题,枚举没有限制,不能用广搜,考虑使用迭代加深搜。
    迭代加深搜:迭代加深搜索,实质上就是限定下界的深度优先搜索。即首先允许深度优先搜索K层搜索树,
    若没有发现可行解,再将K+1后重复以上步骤搜索,直到搜索到可行解。

    对于这道题,依次搜索第一层,第二层,第三层,....,第k层
    判断上下界:上界: (x*b) div a
    下界: b div a
    下界理由:搜索的分数一定小于等于当前的分数
    上界理由:如果接下来所有的分数都相等,那么(x*b) div a

    如果第k层得到解,停止搜索,输出,否侧继续搜索

    判断当前的解优于暂存的解,那么if (temp[0]=init[0])and(init[init[0]]>temp[temp[0]] swap(temp,init);
    if init[0]>temp[0] swap(init,temp);

    code:
    var i:longint;
    j,k:longint;
    m,init:array[0..1000]of longint;
    a,b:longint;
    function min(x,y:longint):longint;
    begin if x>y
    then exit(y)
    else exit(x);
    end;
    function max(x,y:longint):longint;
    begin if x>y
    then exit(x)
    else exit(y);
    end;
    function gcd(x,y:longint):longint;
    begin if y=0
    then exit(x)
    else exit(gcd(y,x mod y));
    end;
    function can(x:longint):boolean;
    var i:longint;
    aa,bb,temp:longint;
             procedure dfs(deep,a,b:longint);
    var i:longint;
    aa,bb:longint;
    j,k:longint;
    begin if (deep>x)and(a=0)
    then begin if (m[0]=init[0])and(init[init[0]]<m[m[0]])
    then begin for i:=0 to init[0] do
    m[i]:=init[i];
    end;
    if (m[0]>init[0])
    then begin for i:=0 to init[0] do
    m[i]:=init[i];
    end;
    end;
    if (deep>x)
    then exit;
    if a<=0
    then exit;
    j:=max(b div a,init[deep-1]+1);
    k:=((x-deep+1)*b) div a+1;
    for i:=j to k do
    begin inc(init[0]);
    init[init[0]]:=i;
    temp:=gcd(b,i);
    bb:=max(b,i) div temp *min(b,i);
    aa:=a*(i div temp)-(b div temp);
    dfs(deep+1,aa,bb);
    dec(init[0]);
    end
    end;
             begin if x=1
    then begin i:=1;
    while (a/b<1/i) do inc(i);
    if a/b=1/i
    then begin m[0]:=1;
    m[1]:=i;
    exit(true);
    end
    else begin exit(false);
    end;
    end;
    m[0]:=maxlongint;
    fillchar(init,sizeof(Init),0);
    if x<>1
    then begin j:=(b div a);
    k:=(x*b) div a+1;
    for i:=j to k do
    begin init[0]:=1;
    init[1]:=i;
    temp:=gcd(b,i);
    bb:=max(b,i) div temp *min(b,i);
    aa:=a*(i div temp)-(b div temp);
    dfs(2,aa,bb);
    dec(init[0]);
    end;
    if m[0]=maxlongint
    then exit(false)
    else exit(true);
    end;
    end;
    begin
    readln(a,b);
    i:=3;
    while not can(i) do
    inc(i);
    for i:=1 to m[0] do
    write(m[i],' ');
    end.
  • 相关阅读:
    Android Studio:xxx is not an enclosing class 错误的解决方法
    SpringMVC验证框架Validation特殊用法
    在Spring MVC中使用注解的方式校验RequestParams
    Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
    Bean Validation 技术规范特性概述
    JSR 303
    SpringMVC学习
    javax.validation.UnexpectedTypeException: No validator could be found for constraint 'org.hibernate.validator.constraints.Length' validating type
    SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
    Spring AspectJ切入点语法详解
  • 原文地址:https://www.cnblogs.com/spiderKK/p/4905571.html
Copyright © 2020-2023  润新知