首页 > 代码库 > CF758C Unfair Poll

CF758C Unfair Poll

题意:

On the Literature lesson Sergei noticed an awful injustice, it seems that some students are asked more often than others.

Seating in the class looks like a rectangle, where n rows with m pupils in each.

The teacher asks pupils in the following order: at first, she asks all pupils from the first row in the order of their seating, then she continues to ask pupils from the next row. If the teacher asked the last row, then the direction of the poll changes, it means that she asks the previous row. The order of asking the rows looks as follows: the 1-st row, the 2-nd row, ..., the n?-?1-st row, the n-th row, the n?-?1-st row, ..., the 2-nd row, the 1-st row, the 2-nd row, ...

The order of asking of pupils on the same row is always the same: the 1-st pupil, the 2-nd pupil, ..., the m-th pupil.

During the lesson the teacher managed to ask exactly k questions from pupils in order described above. Sergei seats on the x-th row, on the y-th place in the row. Sergei decided to prove to the teacher that pupils are asked irregularly, help him count three values:

  1. the maximum number of questions a particular pupil is asked,
  2. the minimum number of questions a particular pupil is asked,
  3. how many times the teacher asked Sergei.

If there is only one row in the class, then the teacher always asks children from this row.

Input

The first and the only line contains five integers nmkx and y (1?≤?n,?m?≤?100,?1?≤?k?≤?1018,?1?≤?x?≤?n,?1?≤?y?≤?m).

Output

Print three integers:

  1. the maximum number of questions a particular pupil is asked,
  2. the minimum number of questions a particular pupil is asked,
  3. how many times the teacher asked Sergei.
Examples
input
1 3 8 1 1
output
3 2 3
input
4 2 9 4 2
output
2 1 1
input
5 5 25 4 3
output
1 1 1
input
100 100 1000000000000000000 100 100
output
101010101010101 50505050505051 50505050505051

思路:

模拟。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long ll;
 5 ll n, m, k, x, y, maxn, minn, t;
 6 int main()
 7 {
 8     cin >> n >> m >> k >> x >> y;
 9     if (n == 1)
10     {
11         maxn = k / m + (k % m ? 1 : 0);
12         minn = k / m;
13         t = y > k % m ? minn : maxn;
14         cout << maxn << " " << minn << " " << t << endl;
15     }
16     else
17     {
18         ll t = (2 * n - 2) * m;
19         ll tmp = k / t;
20         ll rem = k % t;
21         maxn = n > 2 ? 2 * tmp : tmp, minn = tmp;
22         t = (x == 1 || x == n) ? minn : maxn;
23         if (rem)
24         {
25             if (rem > n * m)
26             {
27                 maxn += 2;
28                 minn++;
29                 ll p = (rem - n * m) / m;
30                 ll q = (rem - n * m) % m;
31                 ll nx = n - 1 - p;
32                 ll ny = q;
33                 if (ny == 0)
34                 {
35                     nx++;
36                     ny = m;
37                 }
38                 if (x < nx || x == nx && y > ny || x == n)
39                 {
40                     t++;
41                 }
42                 else
43                 {
44                     t += 2;
45                 }
46             }
47             else if (rem == n * m)
48             {
49                 maxn++;
50                 minn++;
51                 t++;
52             }
53             else
54             {
55                 if (rem > m)
56                     maxn++;
57                 else if (maxn == minn)
58                     maxn++;
59                 ll nx = rem / m;
60                 ll ny = rem % m;
61                 if (ny)
62                 {
63                     nx++;
64                 }
65                 else
66                 {
67                     ny = m;
68                 }
69                 if (!(x > nx || x == nx && y > ny))
70                 {
71                     t++;
72                 }
73             }
74         }
75         cout << maxn << " " << minn << " " << t << endl;
76     }
77     return 0;
78 }

 

CF758C Unfair Poll