static int MultSymbolsFromPosToPos(int[] seq, int startPos, int endPos) { int mult = 1; for (var i = startPos; i <= endPos; i++) { mult = Symb.Mult(mult, seq[i]); } return(mult); }
static IEnumerable <int> FindAllKPos(int[] seq) { int mult = 1; for (var i = seq.Length - 1; i >= 0; i--) { mult = Symb.Mult(seq[i], mult); if (mult == Symb.k) { yield return(i); } } }
static IEnumerable <int> FindAllIPos(int[] seq) { int mult = 1; for (var i = 0; i < seq.Length; i++) { mult = Symb.Mult(mult, seq[i]); if (mult == Symb.i) { yield return(i); } } }
static string Solve(Tuple <long, int[]> d) { long r = d.Item1; int[] seq = d.Item2; if (r == 1) { var possibleIPos = FindAllIPos(seq).ToList(); var possibleKPos = FindAllKPos(seq).Reverse().ToList(); if (!possibleIPos.Any() || !possibleKPos.Any()) { return("NO"); } //possibleKPos.Reverse(); //possibleKPos = possibleKPos.Where(kp => kp > (possibleIPos[0] + 1)).ToList(); Dictionary <int, int> nextKPosMult = new Dictionary <int, int>(); for (var i = 1; i < possibleKPos.Count; i++) { var cur = possibleKPos[i]; var prev = possibleKPos[i - 1]; nextKPosMult.Add(cur, MultSymbolsFromPosToPos(seq, prev, cur - 1)); } foreach (var pI in possibleIPos) { possibleKPos = possibleKPos.Where(pK => pK > (pI + 1)).ToList(); if (!possibleKPos.Any()) { break; } int prevPK = possibleKPos[0]; int prevMult = MultSymbolsFromPosToPos(seq, pI + 1, prevPK - 1); if (prevMult == Symb.j) { return("YES"); } foreach (var pK in possibleKPos.Skip(1).ToList()) { prevMult = Symb.Mult(prevMult, nextKPosMult[pK]); if (prevMult == Symb.j) { return("YES"); } } } return("NO"); } else if (r > 1) { int cf = MultSymbolsFromPosToPos(seq, 0, seq.Length - 1); //long newR = r % 4; long newR = Math.Min((r % 4) + 16, r); //for (var newR = (int)(r % 4); newR <= r && newR <= 100; newR += 4) //{ for (var i = 0; i < newR; i++) { int cfpi = Symb.Power(cf, i); for (var k = i; k < newR; k++) { int cfpk = Symb.Power(cf, (int)(newR - 1 - k)); int cfpj = Symb.Power(cf, k - i - 1); if (Solve(new Tuple <long, int[]>(1, Merge(seq, cfpi, cfpk, cfpj, k == i))) == "YES") { return("YES"); } } } //} return("NO"); } else { throw new NotImplementedException(); } }