#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
// Rationnels exacts et multiplication dans Q[sqrt(2)].
public readonly struct Rat15 : IEquatable<Rat15>
{
public BigInteger N { get; }
public BigInteger D { get; }
public Rat15(BigInteger n, BigInteger d)
{
if (d == 0) throw new DivideByZeroException();
if (d < 0) { n = -n; d = -d; }
var g = BigInteger.GreatestCommonDivisor(BigInteger.Abs(n), d);
N = n / g; D = d / g;
}
public Rat15(BigInteger n) : this(n, 1) { }
public static Rat15 operator +(Rat15 a, Rat15 b) => new Rat15(a.N * b.D + b.N * a.D, a.D * b.D);
public static Rat15 operator -(Rat15 a) => new Rat15(-a.N, a.D);
public static Rat15 operator *(Rat15 a, Rat15 b) => new Rat15(a.N * b.N, a.D * b.D);
public static bool operator ==(Rat15 a, Rat15 b) => a.N == b.N && a.D == b.D;
public static bool operator !=(Rat15 a, Rat15 b) => !(a == b);
public bool Equals(Rat15 b) => this == b;
public override bool Equals(object? o) => o is Rat15 r && Equals(r);
public override int GetHashCode() => HashCode.Combine(N, D);
public override string ToString() => D == 1 ? N.ToString(CultureInfo.InvariantCulture) : N.ToString(CultureInfo.InvariantCulture) + "/" + D.ToString(CultureInfo.InvariantCulture);
}
public readonly struct Quad15 : IEquatable<Quad15>
{
public Rat15 A { get; }
public Rat15 B { get; }
public Quad15(Rat15 a, Rat15 b) { A = a; B = b; }
public static Quad15 operator *(Quad15 x, Quad15 y) => new Quad15(x.A * y.A + new Rat15(2) * x.B * y.B, x.A * y.B + x.B * y.A);
public static bool operator ==(Quad15 x, Quad15 y) => x.A == y.A && x.B == y.B;
public static bool operator !=(Quad15 x, Quad15 y) => !(x == y);
public bool Equals(Quad15 x) => this == x;
public override bool Equals(object? o) => o is Quad15 q && Equals(q);
public override int GetHashCode() => HashCode.Combine(A, B);
}
public sealed class Sol15
{
public Quad15 H { get; set; }
public int Sa { get; set; }
public int Sb { get; set; }
public int Sq { get; set; }
public List<(Rat15 A, Rat15 B, Rat15 Qa, Rat15 Qb)> Lignes { get; set; } = new();
}
public static class Decode15
{
public static List<Rat15> Coupures(string s, bool zeros)
{
var r = new List<Rat15> { new Rat15(BigInteger.Parse(s, CultureInfo.InvariantCulture)) };
for (int i = 1; i < s.Length; i++)
{
if (!zeros && s.Length - i > 1 && s[i] == '0') continue;
r.Add(new Rat15(BigInteger.Parse(s.Substring(0, i), CultureInfo.InvariantCulture), BigInteger.Parse(s.Substring(i), CultureInfo.InvariantCulture)));
}
return r;
}
public static List<Sol15> Enumerer(bool zeros)
{
var lignes = new (string A, string B, string Qa, string Qb)[]
{
("536569972", "251458", "197792604162025", "16279142425"),
("84760912", "2518", "3124543897625", "2083022438425"),
("34759474", "252", "38440455782425", "25626909081625"),
("91759312", "2518", "3382532915225", "2255015116825")
};
var resultat = new List<Sol15>();
var hs = Coupures("5384", zeros);
foreach (var h1 in hs) foreach (var h2 in hs)
{
var h = new Quad15(h1, h2); var hcarre = h * h;
foreach (int sa in new[] { 1, -1 }) foreach (int sb in new[] { 1, -1 }) foreach (int sq in new[] { 1, -1 })
{
var retenues15 = new List<(Rat15 A, Rat15 B, Rat15 Qa, Rat15 Qb)>();
foreach (var ligne in lignes)
{
var gauche = new List<(Rat15 Qa, Rat15 Qb, Quad15 Valeur)>();
foreach (var qa in Coupures(ligne.Qa, zeros)) foreach (var qb in Coupures(ligne.Qb, zeros))
gauche.Add((qa, qb, new Quad15(new Rat15(sq) * qa, -qb) * hcarre));
bool trouve = false;
foreach (var a in Coupures(ligne.A, zeros))
{
if (trouve) break;
foreach (var b in Coupures(ligne.B, zeros))
{
if (trouve) break;
var droite = new Quad15(new Rat15(sa) * a, new Rat15(sb) * b);
foreach (var g in gauche)
if (g.Valeur == droite) { retenues15.Add((a, b, g.Qa, g.Qb)); trouve = true; break; }
}
}
if (!trouve) break;
}
if (retenues15.Count == lignes.Length) resultat.Add(new Sol15 { H = h, Sa = sa, Sb = sb, Sq = sq, Lignes = retenues15 });
}
}
return resultat;
}
public static bool Meme(Sol15 a, Sol15 b)
{
if (a.H != b.H || a.Sa != b.Sa || a.Sb != b.Sb || a.Sq != b.Sq || a.Lignes.Count != b.Lignes.Count) return false;
for (int i = 0; i < a.Lignes.Count; i++) if (a.Lignes[i] != b.Lignes[i]) return false;
return true;
}
}
var solutions15 = Decode15.Enumerer(false);
Console.WriteLine("Coupures reconstituant l'identite sur les 4 rangees (2,2) : " + solutions15.Count.ToString(CultureInfo.InvariantCulture));
if (solutions15.Count != 1) throw new InvalidOperationException("ECART : nombre de solutions");
var solution15 = solutions15[0];
Console.WriteLine("Habillage : h = " + solution15.H.A + " + " + solution15.H.B + " sqrt(2) ; signes (bureau, quotient) (" + solution15.Sa.ToString(CultureInfo.InvariantCulture) + ", " + solution15.Sb.ToString(CultureInfo.InvariantCulture) + ", " + solution15.Sq.ToString(CultureInfo.InvariantCulture) + ")");
if (solution15.H != new Quad15(new Rat15(5, 384), new Rat15(5, 384)) || solution15.Sa != 1 || solution15.Sb != 1 || solution15.Sq != 1) throw new InvalidOperationException("ECART : habillage ou signes");
var attendus15 = new (Rat15 A, Rat15 B, Rat15 Qa, Rat15 Qb)[]
{
(new Rat15(536569,972), new Rat15(25,1458), new Rat15(19779260416,2025), new Rat15(162791424,25)),
(new Rat15(847609,12), new Rat15(25,18), new Rat15(31245438976,25), new Rat15(20830224384,25)),
(new Rat15(3475947,4), new Rat15(25,2), new Rat15(384404557824,25), new Rat15(256269090816,25)),
(new Rat15(917593,12), new Rat15(25,18), new Rat15(33825329152,25), new Rat15(22550151168,25))
};
Console.WriteLine("Verification ligne a ligne, bureau = quotient * h^2 :");
for (int i = 0; i < attendus15.Length; i++)
{
var r = solution15.Lignes[i];
bool ok = new Quad15(r.Qa, -r.Qb) * (solution15.H * solution15.H) == new Quad15(r.A, r.B);
Console.WriteLine(" " + r.A.ToString().PadLeft(12) + " + (" + r.B + ") sqrt(2) <- (" + r.Qa + " - (" + r.Qb + ") sqrt(2)) * h^2 -> " + (ok ? "CONFORME" : "ECART"));
if (!ok || r != attendus15[i]) throw new InvalidOperationException("ECART : bureau ou quotient");
}
Console.WriteLine("La coupure retenue est celle annoncee a la cellule precedente -> CONFORME");
Console.WriteLine("-> le decodage est MESURE : le rendu, la coupure, l'habillage carre");
var solutionsLarges15 = Decode15.Enumerer(true);
bool identiques15 = solutionsLarges15.Count == 1 && Decode15.Meme(solution15, solutionsLarges15[0]);
Console.WriteLine("Sans ecarter les denominateurs a zero de tete : " + solutionsLarges15.Count.ToString(CultureInfo.InvariantCulture) + " solution(s) -> " + (identiques15 ? "CONFORME" : "ECART") + " ; la restriction ne fabrique pas l'unicite");
if (!identiques15) throw new InvalidOperationException("ECART : solutions des deux parcours");