暴力!暴力!之”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 n, m, k, x 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.
Example Input

1 3 8 1 1

4 2 9 4 2

5 5 25 4 3

100 100 1000000000000000000 100 100

Example Output

3 2 3

2 1 1

1 1 1

101010101010101 50505050505051 50505050505051

Note

The order of asking pupils in the first test:

  1. the pupil from the first row who seats at the first table, it means it is Sergei;
  2. the pupil from the first row who seats at the second table;
  3. the pupil from the first row who seats at the third table;
  4. the pupil from the first row who seats at the first table, it means it is Sergei;
  5. the pupil from the first row who seats at the second table;
  6. the pupil from the first row who seats at the third table;
  7. the pupil from the first row who seats at the first table, it means it is Sergei;
  8. the pupil from the first row who seats at the second table;

The order of asking pupils in the second test:

  1. the pupil from the first row who seats at the first table;
  2. the pupil from the first row who seats at the second table;
  3. the pupil from the second row who seats at the first table;
  4. the pupil from the second row who seats at the second table;
  5. the pupil from the third row who seats at the first table;
  6. the pupil from the third row who seats at the second table;
  7. the pupil from the fourth row who seats at the first table;
  8. the pupil from the fourth row who seats at the second table, it means it is Sergei;
  9. the pupil from the third row who seats at the first table;


题目大意:有一个倒霉的班级,有一种有毒的点名方式,如图:

在横向总是1,2,3...n;在纵向总是1,2,3...n-1,n,n-1,n-2...2,1,2...;

有个倒霉孩子在这个班级的(x,y)上。

问你,在点了k次名字的情况下被点到次数最多和最少的次数分别是多少,这个倒霉孩子又被点了几次。

解题思路:因为k相当大,因此要对其进行优化,我们会发现一次循环是 两倍的m*n再减去两倍的m,即2*m*n - 2*m;

对其取余后在暴力涂色求解就可以了,因为教室最大仅仅有10000,一次循环不过不到20000;

AC代码:

 1 import java.util.*;
 2 
 3 public class H{
 4     public static void main(String[] args){
 5         Scanner sc = new Scanner(System.in);
 6         while(sc.hasNext()){
 7             long n = sc.nextLong();
 8             long m = sc.nextLong();
 9             long k = sc.nextLong();
10             long x = sc.nextLong();
11             long y = sc.nextLong();
12             if(n == 1){
13                 long max = 0;long min = 0;long ser = 0;
14                 if(k % m == 0){max = k / m;min = k / m;ser = k / m;}
15                 else {max = k / m + 1;min = k / m;
16                     if(k % m >= y){ser = k / m + 1;}
17                     else{ser = k / m;}
18                 }
19                 System.out.println(max + " " + min + " " + ser);
20             }
21             else if(n > 1){
22                 long max = -1;long min = 10000000000000000L;
23                 long round = 2*m*n - 2*m;
24                 long sum_r = k / round;
25                 long left = k % round;
26                 long print[][] = new long[101][101];
27                 for(int i = 1;i <= (int)n;i ++){//先涂整数圈
28                     for(int j = 1;j <= (int)m;j ++){
29                         if(i == (int)1 || i == (int)n){print[i][j] = sum_r;}
30                         else if(i < n && i > 1){print[i][j] = sum_r * 2;}
31                     }
32                 }
33                 //涂剩下的余数
34                 for(int i = 1;i <= (int)n;i ++){//正着涂 1 -- n
35                     for(int j = 1;j <= (int)m;j ++){
36                         if(left > 0){print[i][j] ++;left --;}
37                     }
38                 }
39                 for(int i = (int)(n - 1);i >= 2;i --){//反着涂 n-1 -- 2
40                     for(int j = 1;j <= (int)m;j ++){
41                         if(left > 0){print[i][j] ++;left --;}
42                     }
43                 }
44                 //再暴力求最大最小
45                 for(int i = 1;i <= (int)n;i ++){
46                     for(int j = 1;j <= (int)m;j ++){
47                         if(max < print[i][j]){max = print[i][j];}
48                         if(min > print[i][j]){min = print[i][j];}
49                     }
50                 }
51                 System.out.println(max + " " + min + " " + print[(int)x][(int)y]);
52             }
53         }
54     }
55 }
原文地址:https://www.cnblogs.com/love-fromAtoZ/p/7245193.html