using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Threading; using static System.Math; namespace CpLibrary.Contest { public class SolverB : SolverBase { Scanner sr; bool isMultipleTestcases = false; public override void Solve() { var (n, m, p) = sr.ReadValue(); var a = sr.ReadLongArray(n); var c = Enumerable.Repeat(1, n).ToArray(); for (int i = 0; i < n; i++) { while (a[i] % p == 0) { c[i]++; a[i] /= p; } } var ans = long.MaxValue; for (int i = 0; i < n; i++) { if (a[i] == 1) continue; var count = (int)Log(m, a[i]) - 1; var t = Pow(a[i], count); while (t <= m) { t *= a[i]; count++; } ans.Chmin(count * c[i] - (c[i] - 1)); } Console.WriteLine(ans == long.MaxValue ? -1 : ans); } public static long Pow(long x, long pow) { long ret = 1; for (int i = 1; i <= pow; i *= 2, x *= x) { if ((pow / i) % 2 == 1) ret *= x; } return ret; } public SolverB(Scanner sr) => this.sr = sr; public override void Run() { var _t = 1; if (isMultipleTestcases) _t = sr.ReadInt(); while (_t-- > 0) Solve(); } } public static class ProgramB { public static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); var sr = new Scanner(new StreamReader(Console.OpenStandardInput())); var solver = new SolverB(sr); var thread = new Thread(new ThreadStart(() => solver.Run()), 1 << 27); thread.Start(); thread.Join(); Console.Out.Flush(); } public static void Expand() => SourceExpander.Expander.Expand(); } } #region Expanded by https://github.com/naminodarie/SourceExpander namespace CpLibrary { public static class Extention { public static string Join(this IEnumerable x, string separator = "") => string.Join(separator, x); public static string Join(this IEnumerable x, char separator) => string.Join(separator, x); public static int UpperBound(this IList list, T value) => list.BinarySearch(value, true, 0, list.Count, Comparer.Default); public static int LowerBound(this IList list, T value) => list.BinarySearch(value, false, 0, list.Count, Comparer.Default); public static int BinarySearch(this IList list, T value, bool isUpperBound, int index, int length, Comparer comparer) { var ng = index - 1; var ok = index + length; while (ok - ng > 1) { var mid = ng + (ok - ng) / 2; var res = comparer.Compare(list[mid], value); if (res < 0 || (isUpperBound && res == 0)) ng = mid; else ok = mid; } return ok; } public static bool Chmax(ref this T a, T b) where T : struct, IComparable { if (a.CompareTo(b) >= 0) return false; a = b; return true; } public static bool Chmin(ref this T a, T b) where T : struct, IComparable { if (a.CompareTo(b) <= 0) return false; a = b; return true; } } } namespace CpLibrary { public class Scanner { public StreamReader sr { get; private set; } string[] str; int index; char[] separators; public Scanner(StreamReader sr, char[] separators) { this.sr = sr; this.separators = separators; str = new string[0]; index = 0; } public Scanner(StreamReader sr): this(sr, new char[]{' '}) { } public Scanner(): this(new StreamReader(Console.OpenStandardInput()), new char[]{' '}) { } public string Read() { if (index < str.Length) return str[index++]; string s; do s = sr.ReadLine(); while (s == ""); str = s.Split(separators, StringSplitOptions.RemoveEmptyEntries); index = 0; return str[index++]; } public string ReadString() => Read(); public string[] ReadStringArray(int n) { var arr = new string[n]; for (int i = 0; i < n; i++) { arr[i] = ReadString(); } return arr; } public int ReadInt() => int.Parse(ReadString()); public int[] ReadIntArray(int n) => ReadValueArray(n); public long ReadLong() => long.Parse(ReadString()); public long[] ReadLongArray(int n) => ReadValueArray(n); public double ReadDouble() => double.Parse(ReadString()); public double[] ReadDoubleArray(int n) => ReadValueArray(n); public BigInteger ReadBigInteger() => BigInteger.Parse(ReadString()); public T1 ReadValue() => (T1)Convert.ChangeType(ReadString(), typeof(T1)); public T1[] ReadValueArray(int n) { var arr = new T1[n]; for (int i = 0; i < n; i++) { arr[i] = ReadValue(); } return arr; } public (T1, T2) ReadValue() => (ReadValue(), ReadValue()); public (T1, T2, T3) ReadValue() => (ReadValue(), ReadValue(), ReadValue()); public (T1, T2, T3, T4) ReadValue() => (ReadValue(), ReadValue(), ReadValue(), ReadValue()); public (T1, T2, T3, T4, T5) ReadValue() => (ReadValue(), ReadValue(), ReadValue(), ReadValue(), ReadValue()); public (T1, T2, T3, T4, T5, T6) ReadValue() => (ReadValue(), ReadValue(), ReadValue(), ReadValue(), ReadValue(), ReadValue()); public (T1, T2, T3, T4, T5, T6, T7) ReadValue() => (ReadValue(), ReadValue(), ReadValue(), ReadValue(), ReadValue(), ReadValue(), ReadValue()); public (T1[], T2[]) ReadValueArray(int n) { var(v1, v2) = (new T1[n], new T2[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i]) = ReadValue(); } return (v1, v2); } public (T1[], T2[], T3[]) ReadValueArray(int n) { var(v1, v2, v3) = (new T1[n], new T2[n], new T3[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i]) = ReadValue(); } return (v1, v2, v3); } public (T1[], T2[], T3[], T4[]) ReadValueArray(int n) { var(v1, v2, v3, v4) = (new T1[n], new T2[n], new T3[n], new T4[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i]) = ReadValue(); } return (v1, v2, v3, v4); } public (T1[], T2[], T3[], T4[], T5[]) ReadValueArray(int n) { var(v1, v2, v3, v4, v5) = (new T1[n], new T2[n], new T3[n], new T4[n], new T5[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i], v5[i]) = ReadValue(); } return (v1, v2, v3, v4, v5); } public (T1[], T2[], T3[], T4[], T5[], T6[]) ReadValueArray(int n) { var(v1, v2, v3, v4, v5, v6) = (new T1[n], new T2[n], new T3[n], new T4[n], new T5[n], new T6[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i], v5[i], v6[i]) = ReadValue(); } return (v1, v2, v3, v4, v5, v6); } public (T1[], T2[], T3[], T4[], T5[], T6[], T7[]) ReadValueArray(int n) { var(v1, v2, v3, v4, v5, v6, v7) = (new T1[n], new T2[n], new T3[n], new T4[n], new T5[n], new T6[n], new T7[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i], v5[i], v6[i], v7[i]) = ReadValue(); } return (v1, v2, v3, v4, v5, v6, v7); } } } namespace CpLibrary { public interface ISolver { public void Solve(); public void Run(); } public abstract class SolverBase : ISolver { public abstract void Solve(); public abstract void Run(); public bool YesNo(bool condition) { Console.WriteLine(condition ? "Yes" : "No"); return condition; } public bool YESNO(bool condition) { Console.WriteLine(condition ? "YES" : "NO"); return condition; } public bool yesno(bool condition) { Console.WriteLine(condition ? "yes" : "no"); return condition; } } } namespace SourceExpander{public class Expander{[Conditional("EXP")]public static void Expand(string inputFilePath=null,string outputFilePath=null,bool ignoreAnyError=true){}public static string ExpandString(string inputFilePath=null,bool ignoreAnyError=true){return "";}}} #endregion Expanded by https://github.com/naminodarie/SourceExpander