📜  计算将偶数“ n”表示为偶数和的方法

📅  最后修改于: 2021-04-29 08:10:32             🧑  作者: Mango

给定正整数“ n”。计算将“ n”表示为偶数正整数之和的总数。以模10 9 + 7输出答案

例子:

Input: 6
Output: 4

Explanation
There are only four ways to write 6
as sum of even integers:
1. 2 + 2 + 2
2. 2 + 4
3. 4 + 2
4. 6
Input: 8
Output: 8

方法是找到可能的模式或递归函数。该方法将与“将n表示为奇数整数之和的计数方法”中已经讨论的方法相同。这里给定的数字是偶数,这意味着偶数和只能通过将第(n-2)数字加两次来获得。我们可以注意到(通过举例)将2加到数字上会使计数加倍。令写“ n”的方式总数为ways(n)。 ‘ways(n)’的值可以用以下公式编写:

ways(n) = ways(n-2) + ways(n-2)
ways(n) = 2 * ways(n-2)

ways(2) = 1 = 20
ways(4) = 2 = 21
ways(6) = 4 = 22
ways(8) = 8 = 23
''
''
''
ways(2 * n) = 2n-1

Replace n by (m / 2)
=> ways(m) = 2m/2 - 1
C++
// C++ program to count ways to write
// number as sum of even integers
#include
using namespace std;
  
// Initialize mod variable as constant
const int MOD = 1e9 + 7;
  
/* Iterative Function to calculate (x^y)%p in O(log y) */
int power(int x, unsigned int y, int p)
{
    int res = 1;      // Initialize result
  
    x = x % p;  // Update x if it is more than or
                // equal to p
  
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (1LL * res * x) % p;
  
        // y must be even now
        y = y>>1; // y = y/2
        x = (1LL * x * x) % p;
    }
    return res;
}
  
// Return number of ways to write 'n'
// as sum of even integers
int countEvenWays(int n)
{
  return power(2, n/2 - 1, MOD);
}
  
// Driver code
int main()
{
    int n = 6;
    cout << countEvenWays(n) << "\n";
  
    n = 8;
    cout << countEvenWays(n);
   return 0;
}


Java
// JAVA program to count ways to write
// number as sum of even integers
  
class GFG {
      
    // Initialize mod variable as constant
    static int MOD = 1000000007;
       
    /* Iterative Function to calculate 
    (x^y)%p in O(log y) */
    static int power(int x, int y, int p)
    {   
        // Initialize result
        int res = 1;      
          
        // Update x if it is more
        // than or equal to p
        x = x % p;  
       
        while (y > 0)
        {
            // If y is odd, multiply x 
            // with result
            if (y % 2 == 1)
                res = (1 * res * x) % p;
       
            // y must be even now
            y = y >> 1; // y = y/2
            x = (1 * x * x) % p;
        }
        return res;
    }
       
    // Return number of ways to write
    // 'n' as sum of even integers
    static int countEvenWays(int n)
    {
      return power(2, n/2 - 1, MOD);
    }
       
    // Driver code
    public static void main(String args[])
    {
        int n = 6;
        System.out.println(countEvenWays(n));
        n = 8;
        System.out.println(countEvenWays(n));
    }
}
  
/* This code is contributed by Nikita Tiwari. */


Python
# PYTHON program to count ways to write
# number as sum of even integers
  
# Initialize mod variable as constant
MOD = 1e9 + 7
  
# Iterative Function to calculate 
# (x^y)%p in O(log y) 
def power(x, y, p) :
    res = 1      # Initialize result
   
    x = x % p  # Update x if it is more 
               # than or equal to p
   
    while (y > 0) :
          
        # If y is odd, multiply x
        # with result
        if (y & 1) :
            res = (1 * res * x) % p
          
        # y must be even now
        y = y >> 1  # y = y/2
        x = (1 * x * x) % p
          
          
    return res
  
   
# Return number of ways to write 'n'
# as sum of even integers
def countEvenWays(n) :
    return power(2, n/2 - 1, MOD)
  
# Driver code
n = 6
print (int(countEvenWays(n)))
n = 8
print (int(countEvenWays(n)))
  
# This code is contributed by Nikita Tiwari.


C#
// C# program to count ways to write
// number as sum of even integers
using System;
  
class GFG {
      
    // Initialize mod variable as constant
    static int MOD = 1000000007;
      
    /* Iterative Function to calculate 
    (x^y)%p in O(log y) */
    static int power(int x, int y, int p)
    { 
          
        // Initialize result
        int res = 1;     
          
        // Update x if it is more
        // than or equal to p
        x = x % p; 
      
        while (y > 0)
        {
              
            // If y is odd, multiply x 
            // with result
            if (y % 2 == 1)
                res = (1 * res * x) % p;
      
            // y must be even now
            y = y >> 1; // y = y/2
            x = (1 * x * x) % p;
        }
          
        return res;
    }
      
    // Return number of ways to write
    // 'n' as sum of even integers
    static int countEvenWays(int n)
    {
        return power(2, n/2 - 1, MOD);
    }
      
    // Driver code
    public static void Main()
    {
        int n = 6;
        Console.WriteLine(countEvenWays(n));
          
        n = 8;
        Console.WriteLine(countEvenWays(n));
    }
}
  
// This code is contributed by vt_m.


PHP
 0)
    {
        // If y is odd, multiply 
        // x with result
        if ($y & 1)
            $res = (1 * $res *
                        $x) % $p;
  
        // y must be even now
        $y = $y >> 1; // y = y/2
        $x = (1 * $x *
                  $x) % $p;
    }
    return $res;
}
  
// Return number of ways 
// to write 'n' as sum of
// even integers
function countEvenWays($n)
{
    global $MOD;
    return power(2, $n / 
                 2 - 1, $MOD);
}
  
// Driver code
$n = 6;
echo countEvenWays($n), "\n";
  
$n = 8;
echo countEvenWays($n);
  
// This code is contributed
// by ajit
?>


输出:

4
8

时间复杂度: O(Log(n))
辅助空间: O(1)