Big product#9001

Big product (Анализа на решението)

Algorithm Analysis

We will assume that a \leq b.

Example

In the third example, the product looks like: (-7) * (-6) * (-5) * (-4) * (-3) * (-2) * (-1)

The product contains 7 negative factors. The numbers a = -7 and b = -1 have the same parity. Therefore, the product is negative.

Algorithm Implementation

Read the input data.

scanf("%d %d", &a, &b);

If all factors are positive, then the answer is 1.

if (a > 0) res = 1;

If there is a 0 among the factors, then the answer is 0.

else if (a <= 0 && b >= 0) res = 0;

If all factors are negative, then the answer is determined by the number of factors. If the number of factors is even ( and have different parity), then the answer is 1. Otherwise, the answer is -1.

else
{
    if ((b - a) % 2 == 0) res = -1;
    else res = 1;
}

Output the answer.

printf("%d\n", res);

Java Implementation

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        Scanner con = new Scanner(System.in);
        int a = con.nextInt();
        int b = con.nextInt();
        int res;

        if (a > 0) res = 1;
        else if (a <= 0 && b >= 0) res = 0;
        else
        {
            if ((b - a) % 2 == 0) res = -1;
            else res = 1;
        }

        System.out.println(res);
        con.close();
    }
}

Python Implementation

a, b = map(int, input().split())

if a > 0: res = 1
elif a <= 0 and b >= 0: res = 0
else:
    if (b - a) % 2 == 0: res = -1
    else: res = 1

print(res)