Codesignal

<Codesignal> Rectangle Rotation

우현짱짱 2020. 4. 18. 16:24

Medium

A rectangle with sides equal to even integers a and b is drawn on the Cartesian plane. Its center (the intersection point of its diagonals) coincides with the point (0, 0), but the sides of the rectangle are not parallel to the axes; instead, they are forming 45 degree angles with the axes.

How many points with integer coordinates are located inside the given rectangle (including on its sides)?

Example

For a = 6 and b = 4, the output should be
rectangleRotation(a, b) = 23.

The following picture illustrates the example, and the 23 points are marked green.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer a

    A positive even integer.

    Guaranteed constraints:
    2 ≤ a ≤ 50.

  • [input] integer b

    A positive even integer.

    Guaranteed constraints:
    2 ≤ b ≤ 50.

  • [output] integer

    • The number of inner points with integer coordinates.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int rectangleRotation(int a, int b)
{
	a/=sqrt(2);
	b/=sqrt(2);

	return (2*a*b+a+b)|1;
}
728x90