Strange Table Solution Codeforces Round #710 Div. 3

Strange Table solution Codeforces Round #710 Div. 3

Polycarp found a rectangular table consisting of nn rows and mm columns. He noticed that each cell of the table has its number, obtained by the following algorithm “by columns”:

  • cells are numbered starting from one;
  • cells are numbered from left to right by columns, and inside each column from top to bottom;
  • number of each cell is an integer one greater than in the previous cell.

For example, if n=3n=3 and m=5m=5, the table will be numbered as follows:123456789101112131415147101325811143691215

However, Polycarp considers such numbering inconvenient. He likes the numbering “by rows”:

  • cells are numbered starting from one;
  • cells are numbered from top to bottom by rows, and inside each row from left to right;
  • number of each cell is an integer one greater than the number of the previous cell.

For example, if n=3n=3 and m=5m=5, then Polycarp likes the following table numbering:161127123813491451015123456789101112131415

Polycarp doesn’t have much time, so he asks you to find out what would be the cell number in the numbering “by rows”, if in the numbering “by columns” the cell has the number xx?Input

The first line contains a single integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

Each test case consists of a single line containing three integers nn, mm, xx (1≤n,m≤1061≤n,m≤106, 1≤x≤n⋅m1≤x≤n⋅m), where nn and mm are the number of rows and columns in the table, and xx is the cell number.

Note that the numbers in some test cases do not fit into the 3232-bit integer type, so you must use at least the 6464-bit integer type of your programming language.Output

For each test case, output the cell number in the numbering “by rows”.ExampleinputCopy

5
1 1 1
2 2 3
3 5 11
100 100 7312
1000000 1000000 1000000000000

outputCopy

1
2
9
1174
1000000000000

Solution :

#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define mod 1000000007

#define yes cout<<“YES\n”

#define no cout<<“NO\n”

int main()

{

ios_base::sync_with_stdio(0);

cin.tie(0); cout.tie(0);

ll int t;

cin>>t;

while(t–)

{

unsigned ll int n,i,j,a[100002],count=0,flag=0,m,x,h,ans;

string s;

vector<ll int> vect;

cin>>n>>m>>x;

h=x%n;

if(h==0)

h=n;

ans=(h-1)*m;

count=h;

for(i=h;;i+=n)

{

ans++;

if(count==x)

break;

count+=n;

}

cout<<ans<<endl;

}

return 0;

}

Leave a Comment