Chef will start asking from students from 1st row to nth row and then from (n1)th row to 2nd row. This constitutes 1 time period lets say. Thus, after every time period, chef will ask 1st row 1st student. In one time period except 1st and last row students, other students are asked 2 questions while former have been asked 1 question.Thus no. of questions asked in 1 period is T=nm+(n2)m. Left out question is k%T. Now remaining questions are asked starting from 1st row 1st student. Now let f(x,y) denotes no. of questions asked to xth row yth column student no of periods,nt=k/T remaining = k%T f(1,y)=f(n,y)=nt f(x,y)=2*nt x>1&&x<n for (i = 0; k > 0 && i < n; i++) for (j = 0; j < m && k > 0; j++) { f(i,j)++; k; } Now simply iterate over all possible n,m and find max,min and questions asked to Sergei Complexity O(n*m) Hope it helps. In case of any doubt, you can comment. answered 25 Feb '17, 01:08

this is my solution: http://codeforces.com/contest/758/submission/25147009 answered 02 Mar '17, 17:37
