public static int productExceptSelf2(int[] nums, int m) { System.Numerics.BigInteger[] result = new System.Numerics.BigInteger[nums.Length]; System.Numerics.BigInteger tmp = 1; for (int i = 0; i < nums.Length; i++) { result[i] = tmp; tmp *= nums[i]; } tmp = 1; for (int i = nums.Length - 1; i >= 0; i--) { result[i] = (result[i] * tmp) % m; tmp *= nums[i]; } System.Numerics.BigInteger finalResult = 0; result.ToList().ForEach(x => finalResult = finalResult + x); return(Convert.ToInt32((finalResult % m).ToString())); }
public static int productExceptSelf3(int[] nums, int m) { System.Numerics.BigInteger[] result = new System.Numerics.BigInteger[nums.Length]; result[nums.Length - 1] = 1; for (int i = nums.Length - 2; i >= 0; i--) { result[i] = result[i + 1] * nums[i + 1]; } System.Numerics.BigInteger left = 1; for (int i = 0; i < nums.Length; i++) { result[i] = result[i] * left; left = left * nums[i]; } System.Numerics.BigInteger y = 0; result.ToList().ForEach(x => y = y + x); return(Convert.ToInt32((y % m).ToString())); }
public static int productExceptSelf(int[] nums, int m) { System.Numerics.BigInteger[] fResults = new System.Numerics.BigInteger[nums.Length]; System.Numerics.BigInteger products = new System.Numerics.BigInteger(1); foreach (var item in nums) { if (item != 1) { products = products * item; } } for (int i = 0; i < nums.Length; i++) { System.Numerics.BigInteger tempProd = products; fResults[i] = (tempProd / nums[i]) % m; } System.Numerics.BigInteger result = 0; fResults.ToList().ForEach(x => result = result + x); return(Convert.ToInt32((result % m).ToString())); }