给定正整数N。任务是找到将N表示为1s和2s之和的方式数量。
例子:
Input : N = 3
Output : 3
3 can be represented as (1+1+1), (2+1), (1+2).
Input : N = 5
Output : 8
对于N = 1,答案为1。
对于N =2。(1 +1),(2),答案为2。
对于N = 3(1 + 1 +1),(2 +1),(1 + 2),答案为3。
对于N = 4.(1 +1 +1 +1),(2 +1 +1),(1 +1 +1),(1 +1 +1),(2 +1)的答案是5。
等等。
可以看出,它形成了斐波那契数列。因此,将N表示为1s和2s之和的方式数是第(N +1)个斐波那契数。
如何?
我们可以轻松地看到递归函数与斐波那契数完全相同。要获得N的总和,我们可以在N – 1上加1。此外,我们可以在N – 2上加2。只允许1和2构成总和N。因此,使用1s和2s获得总和N。 ,总共的方法是:获取方法的数量(N – 1)+获取方法的数量(N – 2)。
我们可以在O(Log n)时间中找到第N个斐波那契数。请参考这篇文章的方法5。
下面是此方法的实现:
C++
// C++ program to find number of ways to representing
// a number as a sum of 1's and 2's
#include
using namespace std;
// Function to multiply matrix.
void multiply(int F[2][2], int M[2][2])
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
// Power function in log n
void power(int F[2][2], int n)
{
if( n == 0 || n == 1)
return;
int M[2][2] = {{1,1},{1,0}};
power(F, n/2);
multiply(F, F);
if (n%2 != 0)
multiply(F, M);
}
/* function that returns (n+1)th Fibonacci number
Or number of ways to represent n as sum of 1's
2's */
int countWays(int n)
{
int F[2][2] = {{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n);
return F[0][0];
}
// Driver program
int main()
{
int n = 5;
cout << countWays(n) << endl;
return 0;
}
Java
// Java program to find number of
// ways to representing a number
// as a sum of 1's and 2's
class GFG
{
// Function to multiply matrix.
static void multiply(int F[][], int M[][])
{
int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
// Power function in log n
static void power(int F[][], int n)
{
if (n == 0 || n == 1)
{
return;
}
int M[][] = {{1, 1}, {1, 0}};
power(F, n / 2);
multiply(F, F);
if (n % 2 != 0)
{
multiply(F, M);
}
}
/* function that returns (n+1)th Fibonacci number
Or number of ways to represent n as sum of 1's
2's */
static int countWays(int n)
{
int F[][] = {{1, 1}, {1, 0}};
if (n == 0)
{
return 0;
}
power(F, n);
return F[0][0];
}
// Driver program
public static void main(String[] args)
{
int n = 5;
System.out.println(countWays(n));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to find number of ways to
# representing a number as a sum of 1's and 2's
# Function to multiply matrix.
def multiply(F, M):
x = F[0][0] * M[0][0] + F[0][1] * M[1][0]
y = F[0][0] * M[0][1] + F[0][1] * M[1][1]
z = F[1][0] * M[0][0] + F[1][1] * M[1][0]
w = F[1][0] * M[0][1] + F[1][1] * M[1][1]
F[0][0] = x
F[0][1] = y
F[1][0] = z
F[1][1] = w
# Power function in log n
def power(F, n):
if( n == 0 or n == 1):
return
M = [[1, 1],[1, 0]]
power(F, n // 2)
multiply(F, F)
if (n % 2 != 0):
multiply(F, M)
#/* function that returns (n+1)th Fibonacci number
# Or number of ways to represent n as sum of 1's
# 2's */
def countWays(n):
F = [[1, 1], [1, 0]]
if (n == 0):
return 0
power(F, n)
return F[0][0]
# Driver Code
n = 5
print(countWays(n))
# This code is contributed by mohit kumar
C#
// C# program to find number of
// ways to representing a number
// as a sum of 1's and 2's
class GFG
{
// Function to multiply matrix.
static void multiply(int [,]F, int [,]M)
{
int x = F[0,0] * M[0,0] + F[0,1] * M[1,0];
int y = F[0,0] * M[0,1] + F[0,1] * M[1,1];
int z = F[1,0] * M[0,0] + F[1,1] * M[1,0];
int w = F[1,0] * M[0,1] + F[1,1] * M[1,1];
F[0,0] = x;
F[0,1] = y;
F[1,0] = z;
F[1,1] = w;
}
// Power function in log n
static void power(int [,]F, int n)
{
if (n == 0 || n == 1)
{
return;
}
int [,]M = {{1, 1}, {1, 0}};
power(F, n / 2);
multiply(F, F);
if (n % 2 != 0)
{
multiply(F, M);
}
}
/* function that returns (n+1)th Fibonacci number
Or number of ways to represent n as sum of 1's
2's */
static int countWays(int n)
{
int [,]F = {{1, 1}, {1, 0}};
if (n == 0)
{
return 0;
}
power(F, n);
return F[0,0];
}
// Driver program
public static void Main()
{
int n = 5;
System.Console.WriteLine(countWays(n));
}
}
// This code contributed by mits
Javascript
输出:
8