public static void Run(Input input, Output output) { var pu = new PrimesUtils(1000000); var primes = pu.GetPrimes(); var T = int.Parse(input.ReadLine()); for (int t0 = 0; t0 < T; t0++) { var N = int.Parse(input.ReadLine()); var countOfRepresentations = 0; int i = 1; // 2 is the only even prime and it is impossible to represent odd number // as sum 2 and double quare while (primes[i] <= N) { var squareCandidate = (N - primes[i]) / 2; var sqrt = (int)Math.Floor(Math.Sqrt(squareCandidate)); if (sqrt * sqrt == squareCandidate) { countOfRepresentations++; } i++; } output.WriteLine(countOfRepresentations.ToString()); } }
public static void Run(Input input, Output output) { var metaStr = input.ReadLine().Split(' '); var N = int.Parse(metaStr[0]); var K = int.Parse(metaStr[1]); var pu = new PrimesUtils(5000000); var primesByPermutations = new Dictionary <string, List <int> >(); var primes = pu.GetPrimes(); for (int i = 0; i < primes.Count; i++) { var currPrime = primes[i]; if (primes[i] < 1000) { continue; } var permutationKey = GetPermutationKey(currPrime); if (!primesByPermutations.ContainsKey(permutationKey)) { primesByPermutations.Add(permutationKey, new List <int>() { currPrime }); } else { primesByPermutations[permutationKey].Add(currPrime); } } var result = new List <int[]>(); foreach (var primesByPermutationsKvp in primesByPermutations) { GetArithmeticSequences(primesByPermutationsKvp.Value, K, N, result); } result.Sort(new Comparison <int[]>(((a, b) => { int i = 0; while (i < a.Length && a[i].CompareTo(b[i]) == 0) { i++; } if (i < a.Length) { return(a[i].CompareTo(b[i])); } return(0); } ) )); for (int i = 0; i < result.Count; i++) { output.WriteLine(string.Join(string.Empty, result[i])); //output.WriteLine(string.Join(" ", result[i])); } }
public static void Run(Input input, Output output) { var pu = new PrimesUtils(9999999); var primes = pu.GetPrimes(); var meta = input.ReadLine().Split(' '); var N = int.Parse(meta[0]); var K = int.Parse(meta[1]); var L = int.Parse(meta[2]); var leftBoudary = _boundaries[N]; var currPrimeIndex = 0; while (primes[currPrimeIndex] < leftBoudary) { currPrimeIndex++; } currPrimeIndex--; var replacementsMasks = GetReplacementsMasks(N, K); while (true) // It is guaranteed that solution does exist { currPrimeIndex++; var currPrime = primes[currPrimeIndex]; var generatedPrimes = GenerateDigitReplacements(currPrime, K, L - 1, replacementsMasks, pu); if (generatedPrimes != null) { output.WriteLine(currPrime.ToString() + " " + string.Join(" ", generatedPrimes)); break; } } }