📜  执行N步后,范围[min,max]中x的每个值的奇数和偶数结果数

📅  最后修改于: 2021-04-24 22:44:03             🧑  作者: Mango

给定数字N以及最小和最大范围。分别给定ab的N个值。任务是在执行如下所述的N次操作后计算偶数/奇数结果的数量。
在每一步中,计算:

解释:

  • 步骤1:y 1 = a 1 x + b 1
  • 步骤2:y 2 = a 2 y 1 + b 2 => y 2 = a 2 a 1 x + a 2 b 1 + b 2
  • 步骤3:y 3 = a 3 y 2 + b 3 => y 3 = a 3 a 2 a 1 x + a 3 a 2 b 1 + a 3 b 2 + b 3
  • 步骤n:y n = a n y n-1 + b n

为了获得最终结果,请将y 0的值用作[min,mix]范围内的每个值。为简单起见,我们假设y 0的值为x,并用最终方程中[min,max]范围内的所有可能值替换x以计算结果。

例子:

Input: n = 2,  min = 1, max = 4 
       a = 1, b = 2 
       a = 3, b = 4 
Output: even = 2, odd = 2. 

Step1: y = 1x + 2 = x+2 
Step2: y =  3(x+2) + 4 = 3x + 10 
Putting all values of in range [1, 4], 
2 odd values and 2 even values are obtained. 

Input: n = 1, min = 4, max = 60
       a= 1, b = 2

Output: even = 29, odd = 28

天真的方法是将a和b的值存储在数组中,并计算指定范围内每个数字的最终结果。如果结果为偶数,则偶数计数增加。否则,奇数计数将增加。

这里使用的一种有效方法是一个基本概念,即两个数字的乘积即使两个数字中的任何一个为偶数也为偶数,否则为奇数,并且两个数字的总和仅在两个数字均为偶数时才为偶数。在这里,可以看到在每个步骤中,一个数字乘以x,然后将另一个常数添加到乘积中。任务是检查结果是偶数还是奇数。在计算的最后一步,检查a 1 a 2 a 3 …a n是否为偶数/奇数,a 2 a 3 …a n b 1 + a 3 a 4 …a n b 2 +…+ b n是否为偶/奇。检查a 1 a 2 a 3 …a n是否为偶数/奇数:
如果任何一个i是偶数,则乘积将始终是偶数,否则它将是奇数。检查a 2 a 3 …a n b 1 + a 3 a 4 …a n b 2 +…+ b n是否为偶数/奇数:

下表说明了系数的所有各种可能性:

a2a3…ai-1b1 + a3a4…ai-1b2 + … + bi-1 ai bi a2a3…aib1 + a3a4…aib2 + … + bi
odd odd odd even
odd odd even odd
odd even odd odd
odd even even even
even odd odd odd
even odd even even
even even odd odd
even even even even

下表说明了y = ax + b的所有各种可能性:

x a b y
odd odd odd even
odd odd even odd
odd even odd odd
odd even even even
even odd odd odd
even odd even even
even even odd odd
even even even even

不必遍历范围[min,max]中的所有数字,而是将其分为两部分来检查范围中的数字是偶数还是奇数,因为所有偶数输入的结果相同,而所有奇数输入的结果均为同样的结果。因此,请检查一种情况,并将其乘以范围中的偶数和奇数。

执行上述计算,并检查最后一步的x系数。

  • 如果是偶数,则aeven为true,否则为false。
  • 如果常数为偶数,则beven为true,否则为false。
  • x的系数是任意一层中a的任意一个值的偶数。
  • 最后一层之后的常数项(如果执行)将通过beven的值以及当前的a和b进行检查。

借助于上面给出的表(第一个),测试了每一层的常数,并且相应地更新了beven的值。

假设x为偶数,则偶数和奇数的值被初始化。

  1. 如果x为偶数,则无论a为多少,ax都将始终为偶数。因此,根据常数项的值,结果将是偶数或奇数。
  2. 如果常数为偶数,则结果为偶数,因此,在给定范围内(max / 2 –(min-1)/ 2),用奇偶数初始化,而用零初始化奇数。
  3. 如果常数为奇数,则结果为奇数,因此奇数由给定范围内的偶数初始化(max / 2 –(min-1)/ 2),偶数由零初始化。

假设x为奇数,则更新偶数和奇数的值。

  1. 如果a为奇数,则ax为奇数。如果a是偶数,则ax是偶数。
  2. 如果ax和常数都为奇数,或者ax和常数都为偶数,则结果为偶数,因此在给定范围内,奇数增加(奇数–最小+ 1 –偶数)
  3. 如果ax为偶数且常数为奇数,或ax为奇数且常数为奇数,则结果为奇数,因此在给定范围内(最大–最小+ 1 –偶数) ,奇数增加了奇数。

下面是上述方法的实现:

C++
// C++ program to print
// Number of odd/even results for
// every value of x in range [min, end]
// after performing N steps
#include 
using namespace std;
 
// Function that prints the
// number of odd and even results
void count_even_odd(int min, int max, int steps[][2])
{
    int a, b, even, odd;
 
    // If constant at layer i is even, beven is true,
    // otherwise false. If the coefficient of x at
    // layer i is even, aeven is true, otherwise false.
    bool beven = true, aeven = false;
    int n = 2;
    for (int i = 0; i < n; i++) {
 
        a = steps[i][0], b = steps[i][1];
 
        // If any of the coefficients at any layer is found
        // to be even, then the product of all the
        // coefficients will always be even.
 
        if (!(aeven || a & 1))
            aeven = true;
 
        // Checking whether the constant added after all
        // layers is even or odd.
 
        if (beven) {
            if (b & 1)
                beven = false;
        }
        else if (!(a & 1)) {
            if (!(b & 1))
                beven = true;
        }
        else {
            if (b & 1)
                beven = true;
        }
    }
 
    // Counting the number of even and odd.
 
    // Assuming input x is even.
    if (beven) {
        even = (int)max / 2 - (int)(min - 1) / 2;
        odd = 0;
    }
    else {
        even = (int)max / 2 - (int)(min - 1) / 2;
        odd = 0;
    }
 
    // Assuming input x is odd.
    if (!(beven ^ aeven))
        even += max - min + 1 - (int)max / 2
                + (int)(min - 1) / 2;
    else
        odd += max - min + 1 - (int)max / 2
               + (int)(min - 1) / 2;
 
    // Displaying the counts.
    cout << "even = " << even << ", odd = " << odd << endl;
}
 
// Driver Code
int main()
{
    int min = 1, max = 4;
    int steps[][2] = { { 1, 2 },
                       { 3, 4 } };
 
    count_even_odd(min, max, steps);
    return 0;
}


Java
// Java program to print
// Number of odd/even
// results for every value
// of x in range [min, end]
// after performing N steps
import java.io.*;
 
class GFG
{
 
// Function that prints
// the number of odd and
// even results
static void count_even_odd(int min,
                           int max,
                           int steps[][])
{
    int a, b, even, odd;
 
    // If constant at layer i
    // is even, beven is true,
    // otherwise false. If the
    // coefficient of x at layer
    // i is even, aeven is true,
    // otherwise false.
    boolean beven = true,
            aeven = false;
    int n = 2;
    for (int i = 0; i < n; i++)
    {
 
        a = steps[i][0];
        b = steps[i][1];
 
        // If any of the coefficients
        // at any layer is found to be
        // even, then the product of
        // all the coefficients will
        // always be even.
        if (!(aeven || (a & 1) > 0))
            aeven = true;
 
        // Checking whether the
        // constant added after all
        // layers is even or odd.
        if (beven)
        {
            if ((b & 1) > 0)
                beven = false;
        }
        else if (!((a & 1) > 0))
        {
            if (!((b & 1) > 0))
                beven = true;
        }
        else
        {
            if ((b & 1) > 0)
                beven = true;
        }
    }
 
    // Counting the number
    // of even and odd.
 
    // Assuming input x is even.
    if (beven)
    {
        even = (int)max / 2 -
               (int)(min - 1) / 2;
        odd = 0;
    }
    else
    {
        even = (int)max / 2 -
               (int)(min - 1) / 2;
        odd = 0;
    }
 
    // Assuming input x is odd.
    if (!(beven ^ aeven))
        even += max - min + 1 -
                (int)max / 2 +
                (int)(min - 1) / 2;
    else
        odd += max - min + 1 -
               (int)max / 2 +
               (int)(min - 1) / 2;
 
    // Displaying the counts.
    System.out.print("even = " + even +
                     ", odd = " + odd);
}
 
// Driver Code
public static void main (String[] args)
{
    int min = 1, max = 4;
    int steps[][] = {{1, 2},
                     {3, 4}};
 
    count_even_odd(min, max, steps);
}
}
 
// This code is contributed
// by anuj_67.


Python3
# Python3 program to print
# Number of odd/even results
# for every value of x in
# range [min, end] after
# performing N steps
 
# Function that prints
# the number of odd
# and even results
def count_even_odd(min, max, steps):
  
    # If constant at layer i
    # is even, beven is True,
    # otherwise False. If
    # the coefficient of x at
    # layer i is even, aeven
    # is True, otherwise False.
    beven = True
    aeven = False
    n = 2
    for i in range(0, n) :
        a = steps[i][0]
        b = steps[i][1]
  
        # If any of the coefficients
        # at any layer is found to
        # be even, then the product
        # of all the coefficients
        # will always be even.
        if (not(aeven or a & 1)):
            aeven = True
  
        # Checking whether the
        # constant added after all
        # layers is even or odd.
        if (beven) :
            if (b & 1):
                beven = False
          
        elif (not(a & 1)) :
            if (not(b & 1)):
                beven = True
          
        else :
            if (b & 1):
                beven = True
          
    # Counting the number
    # of even and odd.
  
    # Assuming input x is even.
    if (beven):
        even = (int(max / 2) -
                int((min - 1) / 2))
        odd = 0
      
    else :
        even = (int(max / 2) -
                int((min - 1) / 2))
        odd = 0
  
    # Assuming input x is odd.
    if (not(beven ^ aeven)):
        even += (max - min + 1 -
             int(max / 2) + int((min - 1) / 2))
    else:
        odd += (max - min + 1 -
            int(max / 2) + int((min - 1) / 2))
  
    # Displaying the counts.
    print("even = " , even ,
          ", odd = " , odd, sep = "")
  
# Driver Code
min = 1
max = 4
steps = [[1, 2],[3, 4]]
count_even_odd(min, max, steps)
 
# This code is contributed
# by Smitha


C#
// C# program to print
// Number of odd/even
// results for every value
// of x in range [min, end]
// after performing N steps
using System;
 
class GFG
{
 
// Function that prints
// the number of odd and
// even results
static void count_even_odd(int min,
                           int max,
                           int [,]steps)
{
    int a, b, even, odd;
 
    // If constant at layer i
    // is even, beven is true,
    // otherwise false. If the
    // coefficient of x at layer
    // i is even, aeven is true,
    // otherwise false.
    bool beven = true,
         aeven = false;
    int n = 2;
    for (int i = 0; i < n; i++)
    {
 
        a = steps[i, 0];
        b = steps[i, 1];
 
        // If any of the coefficients
        // at any layer is found
        // to be even, then the
        // product of all the
        // coefficients will always
        // be even.
        if (!(aeven || (a & 1) > 0))
            aeven = true;
 
        // Checking whether the
        // constant added after all
        // layers is even or odd.
        if (beven)
        {
            if ((b & 1) > 0)
                beven = false;
        }
        else if (!((a & 1) > 0))
        {
            if (!((b & 1) > 0))
                beven = true;
        }
        else
        {
            if ((b & 1) > 0)
                beven = true;
        }
    }
 
    // Counting the number
    // of even and odd.
 
    // Assuming input
    // x is even.
    if (beven)
    {
        even = (int)max / 2 -
               (int)(min - 1) / 2;
        odd = 0;
    }
    else
    {
        even = (int)max / 2 -
            (int)(min - 1) / 2;
        odd = 0;
    }
 
    // Assuming input
    // x is odd.
    if (!(beven ^ aeven))
        even += max - min + 1 -
                 (int)max / 2 +
                (int)(min - 1) / 2;
    else
        odd += max - min + 1 -
            (int)max / 2 +
            (int)(min - 1) / 2;
 
    // Displaying the counts.
    Console.Write("even = " + even +
                  ", odd = " + odd);
}
 
// Driver Code
public static void Main ()
{
    int min = 1, max = 4;
    int [,]steps = {{1, 2},
                    {3, 4}};
 
    count_even_odd(min, max, steps);
}
}
 
// This code is contributed
// by anuj_67.


PHP


Javascript


输出:
even = 2, odd = 2