No.740 The Phantom Tree
Problem Statement
In a certain village, there is a tree that the villagers call the Phantom Tree. When fully grown, this tree bears N leaves, but after that it sheds M leaves every month, and once it has eventually shed all of its leaves, it will never bear leaves again.
Furthermore, this village has a "windy period" lasting Q months, starting from month P every year, and during this period the Phantom Tree sheds 2×M leaves.
In this village, it is now January, and the Phantom Tree has just become fully grown. Output in which month, counting from the start, it sheds all of its leaves.
Input
N M P Q
N is the number of leaves on the Phantom Tree when it is fully grown.
M is the number of leaves shed each month.
The windy period lasts Q months starting from month P every year.
1<=N<=1000000(=10^6)
1<=M<=N
1<=P<=12
1<=Q<=13-P (the windy period never continues across a year boundary)
Output
Output in which month, counting from the start, the Phantom Tree sheds all of its leaves. Print a newline at the end.
Sample
Sample 1
Input
10 1 1 3
Output
7
During the 3 months of January, February, and March, the wind is strong, so the tree sheds 2 leaves each month. The remaining 4 leaves are shed one at a time over 4 months. Therefore, all the leaves have fallen in the 7th month.
Sample 2
Input
50 10 4 4
Output
4
In January, February, and March, the tree sheds 10 leaves each month. April, May, June, and July are windy, so the remaining 20 leaves are all shed by April. Therefore, all the leaves have fallen in the 4th month.
Sample 3
Input
100 2 5 8
Output
31
The windy period is the 8 months from May to December every year.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
gotutiyan