不要使用int类型,因为p最大达十的九次方,p与数组元素相乘可能会让int溢出,应当使用long long类型。

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>


int cmp(const void *v1, const void *v2);
int binarysearch(long long arr[], int n, long long key);
int main(void) {
    int n, max, i, j;
    long long p, *arr;
    scanf("%d %lld", &n, &p);
    arr = new long long[n];
    for (i = 0; i < n; i++) {
        scanf("%lld", arr + i);
    }
    qsort(arr, n, sizeof(long long), cmp);

    max = 0;
    for (i = 0; arr[i] * p < arr[n - 1]; i++) {
        j = binarysearch(arr, n, arr[i] * p);
        if (max < j - i) {
            max = j - i;
        }
    }
    if (arr[i] * p >= arr[n - 1] && max < n - i) {
        max = n - i;
    }
    printf("%d\n", max);
    delete[] arr;
    return 0;
}

int cmp(const void *v1, const void *v2) {
    return *((long long *)v1) - *((long long *)v2);
}

int binarysearch(long long arr[], int n, long long key) {
    int left, right, mid;
    left = 0;
    right = n - 1;
    while (left < right) {
        mid = (left + right) >> 1;
        if (arr[mid] > key) {
            right = mid;
        }
        else {
            left = mid + 1;
        }
    }
    return left;
}