My Java solution by counting GCD
\n// if n1 * n2 % k == 0, then gcd(n1, k) * gcd(n2, k) % k == 0.\nclass Solution {\n    public long countPairs(int[] nums, int k) {\n        if (nums == null ||
```\n// if n1 * n2 % k == 0, then gcd(n1, k) * gcd(n2, k) % k == 0.\nclass Solution {\n    public long countPairs(int[] nums, int k) {\n        if (nums == null || nums.length == 0) {\n            return 0;\n        }\n        GCD gcd = new GCD();\n        long pairCount = 0;\n        Map<Integer, Integer> map = new HashMap<>();\n        for (int number : nums) {\n            int currentGCD = gcd.findGCD(number, k);\n            for (int num : map.keySet()) {\n                if ((long)currentGCD * num % k == 0) {\n                    pairCount += map.get(num);\n                }\n            }\n            map.put(currentGCD, map.getOrDefault(currentGCD, 0) + 1);\n        }\n        return pairCount;\n    }\n}\n\nclass GCD {\n    public int findGCD(int n1, int n2) {\n        if (n2 == 0) {\n            return n1;\n        }\n        return findGCD(n2, n1 % n2);\n    }\n}\n```
['Array', 'Java']