Nhân tất cả phần tử mảng, chia cho n, lấy phần dư. Ví dụ:
Input: arr[] = {12, 35, 69, 74, 165, 54}; N = 47 Output: 14
Giải thích
Tích = 19107673200. 19107673200 % 47 = 14. Cách tốt: nhân từng phần lấy modulo n ngay để tránh tràn số (overflow).
Ví dụ
#include <stdio.h>
int main() {
int arr[] = { 12, 35, 69, 74, 165, 54};
int len = 6;
int n = 47;
int mul = 1;
for (int i = 0; i < len; i++)
mul = (mul * (arr[i] % n)) % n;
printf("the remainder is %d", (mul%n));
return 0;
}
Kết quả
the remainder is 14