/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2009 by Doina Logofatu in C#: Michael Gärtner ************************************************/, using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; namespace Logofatu { class P11Orangensport02 { private static String FileInputName = "orangen.in"; private static String FileOutputName = "orangen.out"; private const int BOUND = 100; private static List factorizations(int n) { List list = new List(); if ( n == 1 ) { BitArray one = new BitArray(2); one.Set(1, true); list.Add(one); } else back(n, 2, new BitArray(BOUND + 1), list); return list; } private static void back(int n, int k, BitArray found, List l) { for ( int f = k; f < BOUND && f*f < n; f++ ) { if ( n % f == 0 ) { found.Set(f, true); back(n / f, f + 1, found, l); found.Set(f,false); } } if ( n <= BOUND ) { BitArray copy = (BitArray)found.Clone(); copy.Set(n, true); l.Add(copy); } } private static void makeDecision(int n1, int n2, StreamWriter sw) { List b = factorizations(n1); List c = factorizations(n2); if ( b.Count == 0 && c.Count == 0 ) sw.WriteLine("Beide luegen! Unentschieden!"); else if ( b.Count == 0 ) sw.WriteLine("Der Erste luegt! Der Zweite gewinnt!"); else if ( c.Count == 0 ) sw.WriteLine("Der Zweite luegt! Der Erste gewinnt!"); else if ( n1 < n2 ) if ( isADisjointCombination(b, c) ) sw.WriteLine("Der zweite Spieler gewinnt!"); else sw.WriteLine("Der erste Spieler gewinnt!"); else if ( isADisjointCombination(b, c) ) sw.WriteLine("Der erste Spieler gewinnt!"); else sw.WriteLine("Der zweite Spieler gewinnt!"); } private static bool isADisjointCombination(List list1, List list2) { foreach ( BitArray b in list1 ) foreach ( BitArray c in list2 ) if ( !intersects(b, c) ) return true; return false; } private static bool intersects(BitArray b, BitArray c) { int i = Math.Min(b.Count, c.Count); while ( --i >= 0 ) { if ( (b[i] & c[i]) ) return true; } return false; } static void Main(string[] args) { StreamReader sr = null; StreamWriter sw = null; try { sw = new StreamWriter(FileOutputName); sr = new StreamReader(FileInputName); while ( !sr.EndOfStream ) { string[] sArray = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int n1 = int.Parse(sArray[0]); int n2 = int.Parse(sArray[1]); makeDecision(n1, n2, sw); } } catch ( IOException ex ) { Console.WriteLine("Fehler bei der Dateiverarbeitung!\n" + ex.ToString()); } finally { if ( sr != null ) { sr.Close(); } if ( sw != null ) { sw.Close(); } } } } }