Codeforces #710 - Strange Table

 /*

    Solving concept:


    I have first found the position of x in "by columns":

        This is note that the position is started from 1. So,

        if(x % n == 0) then, row = n, col = x / n;

        else row = x % n, col = (x / n) + 1;

        This is an easy technique.

        Mind that: you can not run any loop to find the position because of the large number of input and test cases.


    Now, find, what will be value for that position (row, col) in "by rows":

        Look, in "by rows" method - a number will be increased sequentially in side of a row. For example:

        consider 3x5 matrix -

        1 2 3 4 5

        6 7 8 9 10

        11 12 13 14 15

        there should have exact 15 numbers/values. So, what will be value of (row, col)?


    In "by columns" method 11 (given sample - Case 1 - test 3) would be found at position

    (row = 11 % 3 = 2, col = 11 / 3 = 3 + 1 = 4)

    I have 3x5 matrix. So, we might get exact 10 from a 2x5 matrix, right?

    Okay, this is the tricks!


    Now just find how much would we go behind from the position of (2, 5) in "by rows" method:

    that is 2 x 5 = 10 - (5 - 4) = 9



    Ta Da!!!!




*/



#include<bits/stdc++.h>

using namespace std;


#define sf scanf

#define pf printf

#define LL long long


int const high = 1e3+1;


int main()

{

    int test;

    LL n, m, x, div=0, quo=0, row=0, col=0, diff=0, ans=0;


    sf("%d", &test);


    while(test--)

    {

        sf("%lld %lld %lld", &n,  &m, &x);


        div = x/n;

        quo = x%n;


        if(quo == 0)

        {

            row = quo+n;

            col = div;

        }


        else

        {

            row = quo;

            col = div+1;

        }


        diff = m - col;


        ans = (row*m) - diff;


        pf("%lld\n", ans);


    }


    return 0;

}


Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number