Comments

Problem A

  • since
$$$ {lcm(a,b)}= {ab \over gcd(a,b)} $$$
  • given
$$$ {lcm(a,b) \over gcd(a,b)} \lt = 3 $$$
  • then
$$$ {{ab} \over (gcd(a,b))^2 } \le 3 $$$
  • since
$$$ 1 \lt =a \lt =b \lt =n $$$

gcd(a,b) will be in range [1, n], therefore if gcd(a,b)==1 then a=1 and b is multiple of 1 , if gcd(a,b)==2 then b should be multiple of 2 , for gcd(a,b)==3 (b=3*a) ...etc.

  • thus
$$$ b= m a $$$

where m is an integer.

$$${ab \over {(gcd(a,b))^2}}= {{a*a*m \over (gcd(a,am))^2}}= {a*a*m \over a^2}= m$$$
  • hence
$$$m \le 3$$$
  • b=a*m , where m={1,2,3}
  • now when m=1 therefore b=a , since 1<=a<=n there will be n solutions
    • example : (1,1), (2,2) ,(3,3)....(n,n)
  • for m=2 b=2*a there will be 2* [n/2] solutions
    • example : (1,2) , (2,1), (2,4), (4,2)....([n/2],2*[n/2]),(2*[n/2],[n/2])
  • for m=3 b=3*a there will be 2* [n/3] solutions
    • example : (1,3),(3,1), (2,6),(6,2)....([n/3],3*[n/3]),(3*[n/3],[n/3])
  • so by proof solution is: n+ 2*[n/3]+2*[n/2]

thanks for negative feedback.

codeforces round 812! (problem E)

  • diagonal elements are to be untouched as they play no role in swap.

  • Every non diagonal element can have two relative positions A[i][j] and A[j][i] only if A[i][j]!= A[j][i].

  • Then for certain k ( 1<=k<=n) row can be swapped with it's corresponding column to make matrix lexicographically smallest. Here( for upper half of diagonal) we have to choose some i where i<j and check if A[i][j]> A[j][i] or A[i][j]< A[j][i]. Then why is it we require DSU .Here initially DSU represents k different sets where each number ( 1<=k<=n) is it's own parent.

  • Then through union and find operations the set's have to be segregated into two forms that the operation can be performed for kth node or not.

  • ( find using path compression optimization. This concept can be find here
int n;
int arr[2000],p[1000];
int find(int u){
  if(u==p[u])return u;
  return p[u]=find(p[u]);
}
//simple union 
void Union(int a,int b){
  a=find(a);
  b=find(b);
  if(a!=b)
      p[a]=b;
}

void solve(){
   cin>>n;
   int mat[n][n];
   //matrix input
   for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>mat[i][j];
    }
   }
   /* let's consider 2n nodes , where first half of the nodes 
      depict the group that have same states whereas another group depicts 
      elements to have different states. 
       example matrix
         0 1 2 3
       0  a b c d
       1  e f g h
       2  i j k l
       3  m n o p

        suppose we compare for b and e , and b>e then we have two options either to do operation for
        k=0 or k=1. so we put (0 + n )and (1) in same group and (1 + n ) and 0 in same group.
        Here one more condition can occur if b<e then we put 0 and 1 in same group ( why? because if we perform k=0 operation 
        and k=1 operation then it does not affect the b's positon).therefore  i+n and j+n is done and i union j is done.

   */
   for(int i=0;i<2*n;i++)
    p[i]=i;
   /* Now to be lexicographically smallest matrix 
   we counter with those elements which are upper half of the diagonal of 
   the matrix*/

   for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
      if(mat[i][j]<mat[j][i]){
      /*if previously it was calculated that i and j  have to be in different 
      groups so be dont mind it because we have to find lexicographically smaller one*/
        if(find(i)==find(j+n)||find(i+n)==find(j)){
           continue;
        }
        else{
          /*otherwise just form same groups*/
           Union(i,j);
           Union(i+n,j+n);
        }
      }
      else if(mat[i][j]>mat[j][i]){
        /*if they are already in same groups don't dipatch them as already it is 
        leading to answer*/
        if(find(i)==find(j)||find(i+n)==find(j+n))
          continue;
        else{
          /*if not then form groups */
          Union(i+n,j);
          Union(i,j+n);
        }
      }
    }
   }
   /* remember diagnol elements doesn't change */

 for(int i=0;i<n;i++){
  for(int j=i+1;j<n;j++){
    /*if i and j+n and j and i+n belong to same group  then just swap relative 
    elements*/
   if(find(i)==find(j+n)&&find(j)==find(i+n)){
    swap(mat[i][j],mat[j][i]);
   }
  }
 }  
 for(int i=0;i<n;i++){
  for(int j=0;j<n;j++){
    cout<<mat[i][j]<<" "; 
  }
  cout<<endl;
 }
}
int main() {
  int t;
 cin>>t;
  while(t--)
  solve();
}

thanks