using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Numerics;
namespace ConsoleApp1
{
class Program
{
static long Num_0(bool[] A)
{
if (A.Length == 1)
{
return 1;
}
else
{
long res = 0;
for (int i = 0; i < A.Length - 1; i++)
{
bool[] B = new bool[A.Length - 1];
for (int j = 0; j < i; j++)
B[j] = A[j];
for (int j = i + 1; j < A.Length-1; j++)
B[j] = A[j+1];
if (A[i] && A[i + 1])
{
B[i] = true;
res += Num_0(B);
}
B[i] = false;
res += 6 * Num_0(B);
}
return res;
}
}
static void Test_0(int range = 9, bool wait = true)
{
Console.WriteLine("Test 0 for [1.." + Convert.ToString(range) + "]");
F.WriteLine("Test 0 for [1.." + Convert.ToString(range) + "]");
bool[] A = new bool[range];
for (int i = 0; i < range; i++)
A[i] = true;
string s = "Eq count: " + Convert.ToString(Num_0(A));
Console.WriteLine(s);
F.WriteLine(s);
if (wait) Console.ReadLine();
else
Console.WriteLine();
F.WriteLine();
}
public struct NumEx
{
public NumEx(BigInteger x, BigInteger y, int opcode)
{
X = x;
Y = y;
OpCode = opcode;
Eq = X.ToString();
}
public BigInteger X { get; set; }
public BigInteger Y { get; set; }
public int OpCode { get; set; }
public string Eq { get; set; }
public override string ToString() => $"({X}/{Y}) [{Eq}]";
}
static long Num_1(NumEx[] A, int pos = -1)
{
if (A.Length == 1)
return 1;
else
{
long res = 0;
for (int i = 0; i < A.Length - 1; i++)
if ((A[i + 1].OpCode != 0) || (pos == -1) || (pos >= i))
{
NumEx[] B = new NumEx[A.Length - 1];
for (int j = 0; j < i; j++)
B[j] = A[j];
for (int j = i + 1; j < A.Length - 1; j++)
B[j] = A[j + 1];
if ((A[i].OpCode < 2) && (A[i + 1].OpCode == 0))
{
B[i].OpCode = 1;
res += Num_1(B, i);
}
if (A[i + 1].OpCode != 2)
{
B[i].OpCode = 2;
res += 2 * Num_1(B, i);
}
if (A[i + 1].OpCode != 3)
{
B[i].OpCode = 3;
res += 2 * Num_1(B, i);
}
{
B[i].OpCode = 4;
if (A[i].OpCode == 4)
res += Num_1(B, i);
else
res += 2 * Num_1(B, i);
}
}
return res;
}
}
static void Test_1(int range = 9, bool wait = true)
{
Console.WriteLine("Test 1 for [1.." + Convert.ToString(range) + "]");
F.WriteLine("Test 1 for [1.." + Convert.ToString(range) + "]");
NumEx[] A = new NumEx[range];
for (int i = 0; i < range; i++)
A[i] = new NumEx(i + 1, 1, 0);
string s = "Eq count: " + Convert.ToString(Num_1(A));
Console.WriteLine(s);
F.WriteLine(s);
if (wait) Console.ReadLine();
else
Console.WriteLine();
F.WriteLine();
}
static long PowLimit = 27;
static long SolLimit = 400;
static long SolCount = 0;
static long Num_2(NumEx[] A, long val, int pos = -1)
{
if (A.Length == 1)
{
if ((A[0].X % A[0].Y) == 0)
{
BigInteger B = BigInteger.Divide(A[0].X, A[0].Y);
if (B == new BigInteger(val))
{
SolCount++;
if (SolCount <= SolLimit)
{
Console.WriteLine(Convert.ToString(val) + " = " + A[0].Eq);
F.WriteLine(Convert.ToString(val) + " = " + A[0].Eq);
}
}
}
return 1;
}
else
{
long res = 0;
for (int i = 0; i < A.Length - 1; i++)
if ((A[i + 1].OpCode != 0) || (pos == -1) || (pos >= i))
{
NumEx[] B = new NumEx[A.Length - 1];
for (int j = 0; j < i; j++)
B[j] = A[j];
for (int j = i + 1; j < A.Length - 1; j++)
B[j] = A[j + 1];
if ((A[i].OpCode < 2) && (A[i + 1].OpCode == 0))
{
int p = A[i + 1].X.ToString().Length;
B[i].X = BigInteger.Add(A[i + 1].X, BigInteger.Multiply(A[i].X,
BigInteger.Pow(new BigInteger(10), p)));
B[i].Y = 1;
B[i].OpCode = 1;
B[i].Eq = A[i].Eq + A[i + 1].Eq;
res += Num_2(B, val, i);
}
if ((A[i + 1].X % A[i + 1].Y) == 0)
{
BigInteger pow = BigInteger.Divide(A[i + 1].X, A[i + 1].Y);
if (pow <= PowLimit)
{
if (pow.IsZero)
{
B[i].X = 1;
B[i].Y = 1;
B[i].OpCode = 4;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if (A[i+1].OpCode > 1)
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "^" + s2;
res += Num_2(B, val, i);
}
else
{
int p = (int)pow;
BigInteger val1 = BigInteger.Pow(A[i].X, p);
BigInteger val2 = BigInteger.Pow(A[i].Y, p);
B[i].X = val1;
B[i].Y = val2;
B[i].OpCode = 4;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if (A[i + 1].OpCode > 1)
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "^" + s2;
res += Num_2(B, val, i);
if ((!val1.IsZero) && (A[i].OpCode != 4))
{
B[i].X = val2;
B[i].Y = val1;
B[i].OpCode = 4;
s1 = A[i].Eq;
s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if (A[i + 1].OpCode > 1)
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "^-" + s2;
res += Num_2(B, val, i);
}
}
}
}
if ((A[i + 1].X != 0) && (A[i + 1].OpCode != 3))
{
B[i].X = BigInteger.Multiply(A[i].X, A[i + 1].Y);
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].X);
B[i].OpCode = 3;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if ((A[i + 1].OpCode > 1)||(s2.ToArray()[0]=='-'))
s2 = "(" + s2 + ")";
B[i].Eq = s1+ "/" + s2;
res += Num_2(B, val, i);
}
if (A[i + 1].OpCode != 3)
{
B[i].X = BigInteger.Multiply(A[i].X, A[i + 1].X);
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].Y);
B[i].OpCode = 3;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if ((A[i + 1].OpCode > 1) || (s2.ToArray()[0] == '-'))
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "*" + s2;
res += Num_2(B, val, i);
}
if (A[i + 1].OpCode != 2)
{
B[i].X = BigInteger.Add(BigInteger.Multiply(A[i].X, A[i + 1].Y),
BigInteger.Multiply(A[i].Y, A[i + 1].X));
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].Y);
B[i].OpCode = 2;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (s2.ToArray()[0] == '-')
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "+" + s2;
res += Num_2(B, val, i);
B[i].X =
BigInteger.Subtract(BigInteger.Multiply(A[i].X, A[i + 1].Y),
BigInteger.Multiply(A[i].Y, A[i + 1].X));
bool neg = B[i].X < BigInteger.Zero;
if (neg) B[i].X = BigInteger.Abs(B[i].X);
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].Y);
B[i].OpCode = 2;
s1 = A[i].Eq;
s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if ((A[i + 1].OpCode > 1) || (s2.ToArray()[0] == '-'))
s2 = "(" + s2 + ")";
if (neg)
B[i].Eq = "(-" + s1 + ")+" + s2;
else
B[i].Eq = s1 + "-" + s2;
res += Num_2(B, val, i);
}
}
return res;
}
}
static void Test_2(long val, int range = 9, bool wait = true, long out_lim = 400, long pow_lim = 27)
{
Console.WriteLine("Test 2 for [1.." + Convert.ToString(range) + "]");
F.WriteLine("Test 2 for [1.." + Convert.ToString(range) + "]");
NumEx[] A = new NumEx[range];
for (int i = 0; i < range; i++)
A[i] = new NumEx(i + 1, 1, 0);
SolCount = 0;
SolLimit = out_lim;
PowLimit = pow_lim;
Console.WriteLine("Max power: " + Convert.ToString(PowLimit) + ", output limit: "
+ Convert.ToString(SolLimit));
F.WriteLine("Max power: " + Convert.ToString(PowLimit) + ", output limit: "
+ Convert.ToString(SolLimit));
string s = "Eq count: " + Convert.ToString(Num_2(A, val));
Console.WriteLine(s);
F.WriteLine(s);
Console.WriteLine("Total solutions count for " + Convert.ToString(val) + ": "
+ Convert.ToString(SolCount));
F.WriteLine("Total solutions count for " + Convert.ToString(val) + ": "
+ Convert.ToString(SolCount));
if (wait) Console.ReadLine();
else
Console.WriteLine();
F.WriteLine();
}
static long GhostCount = 0;
static long GhostLimit = (int)Math.Pow(10, 6);
static long ShadowCount = 0;
static long ShadowLimit = (int)Math.Pow(10, 6);
static long Num_3(NumEx[] A, long val, int pos = -1)
{
if (A.Length == 1)
{
return 1;
}
else
{
for (int i = 0; i < A.Length; i++)
if (A[i].X != BigInteger.Zero)
{
if ((A[i].X % A[i].Y == 0) && (A[i].X / A[i].Y == new BigInteger(val)))
{
GhostCount++;
if (GhostCount <= GhostLimit)
{
string s = "";
for (int j = 0; j < A.Length; j++)
{
s += "[" + A[j].Eq + "]";
if (j == i)
s += "<-[Ghost]";
s += ";";
}
Console.WriteLine(Convert.ToString(val) + "[Ghost]: " + s);
F.WriteLine(Convert.ToString(val) + "[Ghost]: " + s);
}
}
else
{
bool b1 = A[i].X % new BigInteger(val) == 0;
bool b2 = A[i].Y % new BigInteger(val) == 0;
if (!(!b1 && !b2))
{
ShadowCount++;
if (ShadowCount <= ShadowLimit)
{
string s = "";
for (int j = 0; j < A.Length; j++)
{
s += "[" + A[j].Eq + "]";
if (j == i)
s += "<-[Shadow]";
s += ";";
}
Console.WriteLine(Convert.ToString(val) + "[Shadow]: " + s);
F.WriteLine(Convert.ToString(val) + "[Shadow]: " + s);
}
}
}
}
long res = 0;
for (int i = 0; i < A.Length - 1; i++)
if ((A[i + 1].OpCode != 0) || (pos == -1) || (pos >= i))
{
NumEx[] B = new NumEx[A.Length - 1];
for (int j = 0; j < i; j++)
B[j] = A[j];
for (int j = i + 1; j < A.Length - 1; j++)
B[j] = A[j + 1];
if ((A[i].OpCode < 2) && (A[i + 1].OpCode == 0))
{
int p = A[i + 1].X.ToString().Length;
B[i].X = BigInteger.Add(A[i + 1].X, BigInteger.Multiply(A[i].X,
BigInteger.Pow(new BigInteger(10), p)));
B[i].Y = 1;
B[i].OpCode = 1;
B[i].Eq = A[i].Eq + A[i + 1].Eq;
res += Num_3(B, val, i);
}
if ((A[i + 1].X % A[i + 1].Y) == 0)
{
BigInteger pow = BigInteger.Divide(A[i + 1].X, A[i + 1].Y);
if (pow <= PowLimit)
{
if (pow.IsZero)
{
B[i].X = 1;
B[i].Y = 1;
B[i].OpCode = 4;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if (A[i + 1].OpCode > 1)
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "^" + s2;
res += Num_3(B, val, i);
}
else
{
int p = (int)pow;
BigInteger val1 = BigInteger.Pow(A[i].X, p);
BigInteger val2 = BigInteger.Pow(A[i].Y, p);
B[i].X = val1;
B[i].Y = val2;
B[i].OpCode = 4;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if (A[i + 1].OpCode > 1)
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "^" + s2;
res += Num_3(B, val, i);
if ((!val1.IsZero) && (A[i].OpCode != 4))
{
B[i].X = val2;
B[i].Y = val1;
B[i].OpCode = 4;
s1 = A[i].Eq;
s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if (A[i + 1].OpCode > 1)
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "^-" + s2;
res += Num_3(B, val, i);
}
}
}
}
if ((A[i + 1].X != 0) && (A[i + 1].OpCode != 3))
{
B[i].X = BigInteger.Multiply(A[i].X, A[i + 1].Y);
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].X);
B[i].OpCode = 3;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if ((A[i + 1].OpCode > 1) || (s2.ToArray()[0] == '-'))
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "/" + s2;
res += Num_3(B, val, i);
}
if (A[i + 1].OpCode != 3)
{
B[i].X = BigInteger.Multiply(A[i].X, A[i + 1].X);
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].Y);
B[i].OpCode = 3;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if ((A[i + 1].OpCode > 1) || (s2.ToArray()[0] == '-'))
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "*" + s2;
res += Num_3(B, val, i);
}
if (A[i + 1].OpCode != 2)
{
B[i].X = BigInteger.Add(BigInteger.Multiply(A[i].X, A[i + 1].Y),
BigInteger.Multiply(A[i].Y, A[i + 1].X));
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].Y);
B[i].OpCode = 2;
string s1 = A[i].Eq;
string s2 = A[i + 1].Eq;
if (s2.ToArray()[0] == '-')
s2 = "(" + s2 + ")";
B[i].Eq = s1 + "+" + s2;
res += Num_3(B, val, i);
B[i].X =
BigInteger.Subtract(BigInteger.Multiply(A[i].X, A[i + 1].Y),
BigInteger.Multiply(A[i].Y, A[i + 1].X));
bool neg = B[i].X < BigInteger.Zero;
if (neg) B[i].X = BigInteger.Abs(B[i].X);
B[i].Y = BigInteger.Multiply(A[i].Y, A[i + 1].Y);
B[i].OpCode = 2;
s1 = A[i].Eq;
s2 = A[i + 1].Eq;
if (A[i].OpCode > 1)
s1 = "(" + s1 + ")";
if ((A[i + 1].OpCode > 1) || (s2.ToArray()[0] == '-'))
s2 = "(" + s2 + ")";
if (neg)
B[i].Eq = "(-" + s1 + ")+" + s2;
else
B[i].Eq = s1 + "-" + s2;
res += Num_3(B, val, i);
}
}
return res;
}
}
static void Test_3(long val, int range = 9, bool wait = true, long out_lim = 1000000, long pow_lim = 27)
{
Console.WriteLine("Test 3 for [1.." + Convert.ToString(range) + "]");
F.WriteLine("Test 3 for [1.." + Convert.ToString(range) + "]");
NumEx[] A = new NumEx[range];
for (int i = 0; i < range; i++)
A[i] = new NumEx(i + 1, 1, 0);
GhostLimit = out_lim;
ShadowLimit = out_lim;
PowLimit = pow_lim;
Console.WriteLine("Max power: " + Convert.ToString(PowLimit) + ", output limit: "
+ Convert.ToString(SolLimit));
F.WriteLine("Max power: " + Convert.ToString(PowLimit) + ", output limit: "
+ Convert.ToString(SolLimit));
string s = "Eq count: " + Convert.ToString(Num_3(A, val));
Console.WriteLine(s);
F.WriteLine(s);
Console.WriteLine("Total ghost count for " + Convert.ToString(val) + ": "
+ Convert.ToString(GhostCount));
F.WriteLine("Total ghost count for " + Convert.ToString(val) + ": "
+ Convert.ToString(GhostCount));
Console.WriteLine("Total shadow count for " + Convert.ToString(val) + ": "
+ Convert.ToString(ShadowCount));
F.WriteLine("Total shadow count for " + Convert.ToString(val) + ": "
+ Convert.ToString(ShadowCount));
if (wait) Console.ReadLine();
else
Console.WriteLine();
F.WriteLine();
}
static StreamWriter F;
static void Init(string sFilename)
{
F = new StreamWriter(sFilename);
}
static void Close()
{
F.Close();
}
static void Main(string[] args)
{
Init("Tests_0_-_3.txt");
for (int i = 2; i <= 9; i++)
{
Test_0(i, false);
Test_1(i, false);
Test_2(10958, i, false);
Test_2(10958 / 2, i, false);
Test_3(10958 / 2, i, false);
}
Close();
while (true)
Console.ReadLine();
}
}
}