#P15528. [ROIR 2015 Day 2] forest 伐木

[ROIR 2015 Day 2] forest 伐木

Problem Description

Farmer Nikolai hired two lumberjacks, Dmitry and Fyodor, to cut down a forest so that he can plant corn there. There are XX trees in the forest.

Dmitry cuts down AA trees per day, but every KK days he takes a day off and cuts nothing. Therefore, Dmitry rests on days KK, 2K2K, 3K3K, and so on.

Fyodor cuts down BB trees per day, but every MM days he takes a day off and cuts nothing. Therefore, Fyodor rests on days MM, 2M2M, 3M3M, and so on.

The two lumberjacks work in parallel. Therefore, on days when neither rests, they cut down A+BA + B trees in total; on days when only Fyodor rests, they cut down AA trees; on days when only Dmitry rests, they cut down BB trees; and on days when both rest, they cut down nothing.

Farmer Nikolai wants to know how many days it will take the lumberjacks to cut down all the trees, so that he can start sowing corn.

Task: Write a program that, given integers AA, KK, BB, MM, and XX, computes the number of days required for all the trees to be cut down.

Input Format

The input file contains five integers separated by spaces: AA, KK, BB, MM, and XX (1≤A,B≤1091 \leq A, B \leq 10^9, 2≤K,M≤10182 \leq K, M \leq 10^{18}, 1≤X≤10181 \leq X \leq 10^{18}).

Output Format

The output file should contain one integer — the number of days required to cut down all the trees.

2 4 3 3 25
7

Hint

Example Explanation

In this example, the lumberjacks cut down 2525 trees in 77 days, as follows:

  • Day 11: Dmitry cut down 22 trees, Fyodor cut down 33 trees, for a total of 55 trees.
  • Day 22: Dmitry cut down 22 trees, Fyodor cut down 33 trees, for a total of 1010 trees.
  • Day 33: Dmitry cut down 22 trees, Fyodor rested, for a total of 1212 trees.
  • Day 44: Dmitry rested, Fyodor cut down 33 trees, for a total of 1515 trees.
  • Day 55: Dmitry cut down 22 trees, Fyodor cut down 33 trees, for a total of 2020 trees.
  • Day 66: Dmitry cut down 22 trees, Fyodor rested, for a total of 2222 trees.
  • Day 77: Dmitry cut down 22 trees, Fyodor cut down the remaining 11 tree, for a total of 2525 trees cut down.

Scoring System and Subtasks Description

Subtask 1 (32 points)

  • 1≤X≤10001 \leq X \leq 1000, 1≤A,B≤10001 \leq A, B \leq 1000, 2≤K,M≤10002 \leq K, M \leq 1000.
  • You can score only if all tests are passed.

Subtask 2 (10 points)

  • 1≤X≤10181 \leq X \leq 10^{18}.
  • X<KX < K.
  • X<MX < M.
  • When solving this subtask, you may assume the lumberjacks do not rest.
  • You can score only if all tests are passed.

Subtask 3 (10 points)

  • 1≤X≤10181 \leq X \leq 10^{18}.
  • An additional condition is K=MK = M.
  • You can score only if all tests are passed.

Subtask 4 (48 points)

  • 1≤X≤10181 \leq X \leq 10^{18}, 1≤A,B≤1091 \leq A, B \leq 10^9, 2≤K,M≤10182 \leq K, M \leq 10^{18}.
  • This subtask has 1616 tests. Each test is worth 33 points, and each test is scored independently.

Translation source: GPT 5.2.

Translated by ChatGPT 5