Simple solution using gcd
\n//The condition given to us is (a*b % k==0)\n// So we can rewrite the above condition that if any factor of k is present in a and any other factor of k is pre
```\n//The condition given to us is (a*b % k==0)\n// So we can rewrite the above condition that if any factor of k is present in a and any other factor of k is present in b then their multiplication will be divisble by k\n\n// so gcd(a,k) * gcd(b,k) % k==0 \n\n\nclass Solution {\n    public long countPairs(int[] nums, int k) {\n        long ans=0;\n        HashMap<Integer,Integer> hm=new HashMap<>();\n        for(int val:nums){\n            int gcd1=gcd(val,k);\n            \n            for(int gcd2:hm.keySet()){\n                if((long)gcd1*gcd2 % k==0){\n                    ans+=hm.get(gcd2);\n                }\n            }\n            \n            hm.put(gcd1,hm.getOrDefault(gcd1,0)+1);\n        }\n        \n        return ans;\n    }\n    \n    //function to calculate gcd \n   \n    public int gcd(int n1,int n2)\n    {\n        while(n1%n2!=0){\n            int rem=n1%n2;\n            n1=n2;\n            n2=rem;\n        }\n        return n2;\n    }\n}\n```
['Java']