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:
- the maximum number of questions a particular pupil is asked,
- the minimum number of questions a particular pupil is asked,
- 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.
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).
Print three integers:
- the maximum number of questions a particular pupil is asked,
- the minimum number of questions a particular pupil is asked,
- how many times the teacher asked Sergei.
1 3 8 1 1
3 2 3
4 2 9 4 2
2 1 1
5 5 25 4 3
1 1 1
100 100 1000000000000000000 100 100
101010101010101 50505050505051 50505050505051
The order of asking pupils in the first test:
- the pupil from the first row who seats at the first table, it means it is Sergei;
- the pupil from the first row who seats at the second table;
- the pupil from the first row who seats at the third table;
- the pupil from the first row who seats at the first table, it means it is Sergei;
- the pupil from the first row who seats at the second table;
- the pupil from the first row who seats at the third table;
- the pupil from the first row who seats at the first table, it means it is Sergei;
- the pupil from the first row who seats at the second table;
The order of asking pupils in the second test:
- the pupil from the first row who seats at the first table;
- the pupil from the first row who seats at the second table;
- the pupil from the second row who seats at the first table;
- the pupil from the second row who seats at the second table;
- the pupil from the third row who seats at the first table;
- the pupil from the third row who seats at the second table;
- the pupil from the fourth row who seats at the first table;
- the pupil from the fourth row who seats at the second table, it means it is Sergei;
- the pupil from the third row who seats at the first table;
题意:教室里有n行m列,老师上课点名从第一行第一列开始点,直到点到第一行第m列,就从第二行第一列开始点,当点完第n行的名之后,接着点第n-1行的名。以此类推,就是从行上来看的话:1,2,3,4,……,n,n-1,n-2,……,1 ,2,……。这样的顺序点名。老师上课总共点k次名,问该课堂最多可以点同一个同学多少次,最少可以点同一个同学多少次,点了位置为(x,y)的同学多少次名。
解题思路:从行来看一个周期是(n+n-2),因为当n = 1的时候这条式子等于0,所以要特判n = 1的情况。如果n = 1的话,那么每次都是点这一行了,最少的人是算了k / m次,剩下的用k % m从第一行去模拟加上,直到那个余数变成0为止。如果n > 1的话,那么每个周期点第1行和第n行的次数是只有一次,而点2~n-1列的次数是有两次,那么可以点的周期数 = k / m / (n+n-2),然后剩下的用k % (m * (n+n-2))去模拟。因为初始是从第一行开始的,所以从第一行开始扫起。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define LL long long
const LL INF=0x7FFFFFFFFFFFFFFF;
int main()
{
LL n,m,k,x,y;
LL a[105][105];
while(~scanf("%lld %lld %lld %lld %lld",&n,&m,&k,&x,&y))
{
memset(a,0,sizeof a);
LL mi=INF,ma=-1;
if(n==1)
{
LL kk=k/(n*m),kkk=k%(n*m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[i][j]+=kk;
if(kkk) a[i][j]++,kkk--;
}
}
}
else
{
LL kk=k/(n*2*m-2*m),kkk=k%(n*m*2-2*m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[i][j]+=kk;
if(kkk) a[i][j]++,kkk--;
}
}
for(int i=n-1;i>1;i--)
{
for(int j=1;j<=m;j++)
{
a[i][j]+=kk;
if(kkk) a[i][j]++,kkk--;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ma=max(ma,a[i][j]);
mi=min(mi,a[i][j]);
}
}
printf("%lld %lld %lld\n",ma,mi,a[x][y]);
}
return 0;
}