35 不包含本位置值的累乘数组
Table of Contents

ac

数组需要使用long类型

import java.util.Scanner;


public class Main {

    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();
        int p = sc.nextInt();
        long[] arr = new long[n];
        for(int i=0;i<n;i++){
            arr[i] = sc.nextInt();
        }
        long[] left = new long[n];
        left[0] = 1;
        for(int i=0;i<n-1;i++){
            left[i+1] = (left[i] * arr[i]) % p;
        }
        long[] right = new long[n];
        right[n-1] = 1;
        for(int i=n-2;i>=0;i--){
           right[i] = (right[i+1] * arr[i+1]) % p;
        }
        // 左 * 右 打印结果
        for(int i =0;i<n-1;i++){
            System.out.print((left[i] * right[i]) % p + " ");
        }
        System.out.println((left[n-1] * right[n-1]) % p);
    }
}
/*
4 100000007
2 3 1 4

12 8 24 6
*/