给定一个数组arr,返回不包含本位置值的累乘数组
例如,arr=[2,3,1,4],返回[12, 8, 24, 6],即除自己外,其他位置上的累乘
[要求]
时间复杂度为,额外空间复杂度为
第一行有两个整数N, P。分别表示序列长度,模数(即输出的每个数需要对此取模)
接下来一行N个整数表示数组内的数
输出N个整数表示答案
4 100000007 2 3 1 4
12 8 24 6
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int p = scanner.nextInt(); long[] arr = new long[n]; for(int i=0;i<n;i++){ arr[i] = scanner.nextInt(); } long[] lr = new long[n]; long[] rl = new long[n]; lr[0]=1; rl[n-1]=1; for(int i=1;i<n;i++){ lr[i] = lr[i-1] * arr[i-1] % p; } for(int i=n-2;i>=0;i--){ rl[i] = rl[i+1] * arr[i+1] % p; } for(int i=0;i<n;i++){ System.out.print(lr[i]*rl[i]%p+" "); } } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().trim().split(" "); int n = Integer.parseInt(params[0]); long mod = Long.parseLong(params[1]); String[] strArr = br.readLine().trim().split(" "); int[] arr = new int[n]; for(int i = 0; i < n; i++) arr[i] = Integer.parseInt(strArr[i]); long[] leftMulti = new long[n]; long[] rightMulti = new long[n]; leftMulti[0] = 1; rightMulti[n - 1] = 1; for(int i = 1; i < n; i++) leftMulti[i] = leftMulti[i - 1] * arr[i - 1] % mod; for(int i = n - 2; i >= 0; i--) rightMulti[i] = rightMulti[i + 1] * arr[i + 1] % mod; for(int i = 0; i < n; i++) System.out.print(leftMulti[i] * rightMulti[i] % mod + " "); } }
#include <bits/stdc++.h> (755)#define ll long long using namespace std; int main(){ int n, p; cin>>n>>p; int a[n]; ll l[n+1], r[n+1]; l[0] = r[n] = 1; for(int i=0;i<n;i++){ cin>>a[i]; l[i+1] = l[i]*a[i]%p; } for(int i=n;i>=1;i--) r[i-1] = r[i]*a[i-1]%p; for(int i=0;i<n;i++) cout<<(l[i]*r[i+1]%p)<<" "; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { long long n,p; cin>>n>>p; vector<int>arr(n),l(n+1),r(n+1); l[0]=1; r[n]=1; for(int i=0;i<n;i++) { cin>>arr[i]; l[i+1] = l[i]*arr[i]%p; } for(int i = n-1;i>=0;i--) r[i] = r[i+1]*arr[i]%p; for(int i=0;i<n;i++) cout<<(l[i]*r[i+1]%p)<<" "; return 0; } 不知道为啥下面的方法不对,在IDE都是对的?哪位大神解答一下! int main() { long long n, p; cin >> n >> p; vector<int>arr(n); long long sum = 1; for (int i = 0; i<n; i++) { cin >> arr[i]; sum *= arr[i]; } for (int i = 0; i<n; i++) cout << sum / arr[i] % p << " "; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int n, p; while (cin >> n >> p) { vector<int> arr(n, 0); int all = 1; int count = 0; int index = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] != 0) { all *= arr[i]; all%=p; } else { count++; index = i; } } vector<int> ret(n, 0); if (count == 0) { for (int i = 0; i < n; i++) { ret[i] = (all / arr[i]) % p; } } else if (count == 1) { ret[index] = all % p; } for (int i = 0; i < n; i++) { cout << ret[i] << " "; } cout << endl; } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int mod = sc.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } long[] res = getProducts(arr, mod); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(res[i] + " "); } System.out.println(sb.toString()); } //需要用到额外两个:lr与rl,分别代表arr[0...i]从左到右的乘积与 //arr[i+1...N-1]从右到左的乘积,这样res[i] = lr[i-1] * rl[i+1] //通过优化可以只使用一组额外数组 public static long[] getProducts(long[] arr, int mod) { long[] res = new long[arr.length]; res[0] = arr[0]; //先得到lr数组 for (int i = 1; i < arr.length; i++) { res[i] = res[i-1] * arr[i] % mod; } long tmp = 1; for (int i = arr.length-1; i > 0; i--) { res[i] = tmp * res[i-1] % mod; //每一个tmp都记录着rl数组中从右到左的数 tmp = tmp * arr[i] % mod; } res[0] = tmp % mod; return res; } }
N,P = map(int,input().split()) array = list(map(int,input().split())) l = [0 for _ in range(N+1)] r = [0 for _ in range(N+1)] l[0] = 1 r[N] = 1 for i in range(N): l[i+1] = l[i] * array[i] % P for i in range(N-1,-1,-1): r[i] = r[i+1] * array[i] % P answer = [] for i in range(N): answer.append(l[i]*r[i+1]%P) print(' '.join(map(str,answer)))
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int p = sc.nextInt(); int[] nums = new int[n]; long[] res = new long[n]; long temp = 1; for (int i = 0; i < n; i ++) { nums[i] = sc.nextInt(); res[i] = (int)temp; temp *= nums[i]; temp %= p; } temp = 1; for (int i = n - 1; i >= 0; i --) { res[i] *= temp; res[i] %= p; temp *= nums[i]; temp %= p; } for (long x: res) { System.out.print(x + " "); } } }