简体   繁体   English

找到多种生成数字的方法

[英]Find a Number of ways to Generate a Number

I have given Fibonacci digits only , i have to find out the numbers of ways to generate a number using K Fibonacci numbers only.我只给出了Fibonacci digits only ,我必须找出仅使用K斐波那契数字生成数字的方法。

Constraints:约束:

1<=K<=10
1<=N<=10^9

For Example:例如:

N=14 and K=3

There are two ways:有两种方式:

(8,5,1) and (8,3,3) 

Here is my recursive solution:这是我的递归解决方案:

public static void num_gen(int i ,long val ,int used){

      if(used==0){

          if(val==n) ans++;
          return ;
      }

      if(i==Fib.length) return ;

      for(int j=0;j<=used;j++){

          long x = j*Fib[i];
          if(x+val<=n){
              num_gen(i+1,x+val, used-j);
          }
      }

}

This solution will timeout for large value of N and K=10.对于较大的 N 值和 K=10,此解决方案将超时。 Can you provide me algorithm with better complexity.你能给我提供更复杂的算法吗?

This can be expressed as multiplying polynomials where exponents are Fibonacci numbers.这可以表示为乘法多项式,其中指数是斐波那契数。

Number of factors is K.因子数为 K。

The result is a coefficient of the member of the result polynomial whose exponent equals N.结果是指数等于 N 的结果多项式成员的系数。

Example: What is the number of ways to compose number 7 from 3 numbers where each of these 3 numbers can be 1,2 or 3.示例:从 3 个数字组成数字 7 的方法数是多少,其中这 3 个数字中的每一个都可以是 1,2 或 3。

(x + x² + x³)³ = x⁹ + 3x⁸ +6x⁷ + 7x⁶ + 6x⁵ + 3x⁴ + x³ (x + x² + x³)³ = x⁹ + 3x⁸ +6x⁷ + 7x⁷ + 6x⁵ + 3x⁴ + x³

Result is 6 since it is the coefficient of the x⁷ member of the result polynomial.结果是 6,因为它是结果多项式的 x⁷ 成员的系数。

I'd like to give you a solution that works in another language, and I hope that helps you learn in the process of translating it to Java.我想给你一个适用于另一种语言的解决方案,我希望能帮助你在将它翻译成 Java 的过程中学习。 Because I'm unclear on how to otherwise help you fix the recursive solution you're working on, as I believe that no recursion is required.因为我不清楚如何以其他方式帮助您修复您正在处理的递归解决方案,因为我认为不需要递归。 Also no preset array of Fibonacci numbers is needed.也不需要预设的斐波那契数列。

This is in Perl, it worked for:这是在 Perl 中,它适用于:

$ perl fibber.pl 3 14
8,5,1
8,3,3
2 matches

But I can't guarantee it's perfectly right.但我不能保证它是完全正确的。

#!/usr/bin/perl
use bigint;
use List::Util qw(sum);

my ($digits, $goal) = @ARGV;
if (!($digits > 0) || !($goal > 0)) {
    die "Missing 2 arguments: the number count to sum, the value they sum to.";
}

sub fib {
    my ($a, $b) = @_;
    return sub {
        if (0 == scalar @_) {
            (my $r, $a, $b) = ($a, $b, $a+$b);
            return $r;
        } else {
            ($a, $b) = @_;
            (my $r, $a, $b) = ($b, $a+$b, $a+$b+$b);
            return $r;
        }
    }
}

my @f = (0) x $digits;
   @f = map {fib(1,2)} @f;
my @d = map {$_->()}   @f;
my $count = 0;
while ($d[0] < $goal) {
    if ($goal == sum @d) {
        $count++;
        print(join(",", @d)."\n");
    }
    my ($i, $a, $b) = (0, $d[$i], $f[$i]->());
    $d[$i] = $b;
    while ($goal <= $d[$i]) {
        $i++;
        if ($i == $digits) {
            print "$count matches\n";
            exit 0;
        }
        ($a, $b) = ($d[$i], $f[$i]->());
        $d[$i] = $b;
    }
    while ($i > 0) {
        $i--;
        $d[$i] = $f[$i]->($a, $b);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM