क्रमपरिवर्तन ऐसा है कि कोई k + 2 अंक डिग्री k के किसी भी बहुपद पर नहीं गिरता है


16

विवरण

यदि अंक का एक सेट (उनके सूचकांकों के साथ) डिग्री के एक बहुपद पर गिरता है , तो पूर्णांकों के क्रमबद्धता को न्यूनतम प्रक्षेप{1, 2, ..., n} कहा जा सकता है । अर्थात्,k+2k

  1. कोई दो बिंदु एक क्षैतिज रेखा पर नहीं आते हैं (0-डिग्री बहुपद)
  2. कोई तीन बिंदु एक रेखा पर नहीं आते हैं (1-डिग्री बहुपद)
  3. कोई चार अंक एक परबोला (2-डिग्री बहुपद) पर नहीं आते हैं
  4. Et cetera।

चुनौती

एक कार्यक्रम लिखें जो OEIS अनुक्रम A301802 (n) की गणना करता है , जितना संभव हो उतना बड़े के {1, 2, ..., n}लिए न्यूनतम प्रक्षेपनीय क्रमपरिवर्तन की संख्या n


स्कोरिंग

बढ़ते हुए इनपुट्स के साथ मैं आपके कंप्यूटर (2.3 GHz Intel Core i5, 8 GB RAM) पर आपके कोड को समय दूंगा। आपका स्कोर सबसे बड़ा इनपुट होगा जो सही मूल्य को आउटपुट करने में 1 मिनट से कम समय लेता है।


उदाहरण

उदाहरण के लिए, क्रमपरिवर्तन [1, 2, 4, 3]न्यूनतम रूप से प्रक्षेप्य है क्योंकि

the terms together with their indices 
[(1, 1), (2, 2), (3, 4), (4, 3)] 
have the property that
  (0) No two points have the same y-value.
  (1) No three points lie on a line.
  (2) No four points lie on a parabola.

उदाहरण है कि [१,२,४,३] न्यूनतम रूप से प्रक्षेप्य है। दृष्टांत में, आप देख सकते हैं कि क्षैतिज रेखाएँ (लाल) उन पर एक बिंदु पर होती हैं, उन पर रेखाएँ (नीला) अधिकतम दो बिंदुओं पर होती हैं, और परवल (हरा) उन पर तीन बिंदु होते हैं।


डेटा

यहाँ न्यूनतम interpolable क्रमपरिवर्तन हैं के लिए n=3, n=4और n=5:

n = 3: [1,3,2],[2,1,3],[2,3,1],[3,1,2]
n = 4: [1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,1,4,3],[2,3,1,4],[2,4,1,3],[2,4,3,1],[3,1,2,4],[3,1,4,2],[3,2,4,1],[3,4,1,2],[3,4,2,1],[4,1,3,2],[4,2,1,3],[4,2,3,1],[4,3,1,2]
n = 5: [1,2,5,3,4],[1,3,2,5,4],[1,3,4,2,5],[1,4,2,3,5],[1,4,3,5,2],[1,4,5,2,3],[1,4,5,3,2],[1,5,3,2,4],[2,1,4,3,5],[2,3,1,4,5],[2,3,5,1,4],[2,3,5,4,1],[2,4,1,5,3],[2,4,3,1,5],[2,4,5,1,3],[2,5,1,3,4],[2,5,1,4,3],[2,5,3,4,1],[2,5,4,1,3],[3,1,4,5,2],[3,1,5,2,4],[3,1,5,4,2],[3,2,5,1,4],[3,2,5,4,1],[3,4,1,2,5],[3,4,1,5,2],[3,5,1,2,4],[3,5,1,4,2],[3,5,2,1,4],[4,1,2,5,3],[4,1,3,2,5],[4,1,5,2,3],[4,1,5,3,2],[4,2,1,5,3],[4,2,3,5,1],[4,2,5,1,3],[4,3,1,2,5],[4,3,1,5,2],[4,3,5,2,1],[4,5,2,3,1],[5,1,3,4,2],[5,2,1,3,4],[5,2,1,4,3],[5,2,3,1,4],[5,2,4,3,1],[5,3,2,4,1],[5,3,4,1,2],[5,4,1,3,2]

यदि मेरा प्रोग्राम सही है, तो पहले कुछ मानों की a(n)संख्या, न्यूनतम अंतर-योग्य परमीशन की संख्या {1, 2, ..., n}:

a(1) = 1
a(2) = 2
a(3) = 4
a(4) = 18
a(5) = 48
a(6) = 216
a(7) = 584
a(8) = 2870

अच्छा क्रम संख्या! | यद्यपि आपने सबसे तेज़-कोड निर्दिष्ट किया है, आपने यह निर्दिष्ट नहीं किया कि यह किस मशीन पर सबसे तेज़ है। वास्तव में जीतने का मापदंड क्या है?
user202729

3
User202729 की टिप्पणी में जोड़ने के लिए, मैं कुछ टैग सुझाता हूं जिसका उपयोग आप जीतने के मानदंड निर्धारित करने के लिए कर सकते हैं: सबसे तेज़-कोड की आवश्यकता है कि रनटाइम की तुलना करने के लिए एक ही मशीन पर सबमिशन का परीक्षण किया जाए (आमतौर पर चुनौती का ओपी ऐसा करता है)। सबसे तेज़-एल्गोरिदम उत्तरदाताओं को यथासंभव कम से कम समय की जटिलता के साथ कोड के साथ आने के लिए कहेंगे। कोड-गोल्फ उपयोगकर्ताओं को यथासंभव कम से कम स्रोत कोड (या समतुल्य) कोड के साथ आने के लिए कहेगा। इसके अलावा, यह वास्तव में एक अच्छी चुनौती है।
जुंगवान मिन

आपका उदाहरण पाठ शून्य-अनुक्रमण का उपयोग करता है, हालांकि छवि एक-अनुक्रमण का उपयोग करती है।
जोनाथन फ्रीच

चूंकि सभी बिंदुओं को पहले प्राकृतिक संख्याओं के क्रमांकन द्वारा परिभाषित किया गया है, क्या किसी भी दो बिंदुओं के लिए समान ऊंचाई पर कब्जा करना असंभव नहीं है?
जोनाथन फ्रीच

@JonathanFrech, वास्तव में, यह 1-अनुक्रमित होना चाहिए क्योंकि ये क्रमपरिवर्तन हैं। और तुम सही हो! क्योंकि हम क्रमपरिवर्तन के साथ काम कर रहे हैं, 0 डिग्री बहुपद स्थिति मुफ्त में आती है।
पीटर कैगी

जवाबों:


5

सी#

using System;
using System.Diagnostics;
using BigInteger = System.Int32;

namespace Sandbox
{
    class PPCG160382
    {
        public static void Main(params string[] args)
        {
            if (args.Length != 0)
            {
                foreach (var arg in args) Console.WriteLine(CountValidPerms(int.Parse(arg)));
            }
            else
            {
                int[] smallValues = new int[] { 1, 1, 2, 4, 18, 48 };
                for (int n = 0; n < smallValues.Length; n++)
                {
                    var observed = CountValidPerms(n);
                    var expected = smallValues[n];
                    Console.WriteLine(observed == expected ? $"{n}: Ok" : $"{n}: expected {expected}, observed {observed}, error {observed - expected}");
                }
                for (int n = smallValues.Length; n < 13; n++)
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    Console.WriteLine($"{n}: {CountValidPerms(n)} in {sw.ElapsedMilliseconds}ms");
                }
            }
        }

        private static long CountValidPerms(int n)
        {
            // We work on the basis of exclusion by extrapolation.
            var unused = (1 << n) - 1;
            var excluded = new int[n];
            int[] perm = new int[n];

            // Symmetry exclusion: perm[0] < (n+1) / 2
            if (n > 1) excluded[0] = (1 << n) - (1 << ((n + 1) / 2));

            long count = 0;
            CountValidPerms(ref count, perm, 0, unused, excluded);
            return count;
        }

        private static void CountValidPerms(ref long count, int[] perm, int off, int unused, int[] excluded)
        {
            int n = perm.Length;
            if (off == n)
            {
                count += CountSymmetries(perm);
                return;
            }

            // Quick-aborts
            var completelyExcluded = excluded[off];
            for (int i = off + 1; i < n; i++)
            {
                if ((unused & ~excluded[i]) == 0) return;
                completelyExcluded &= excluded[i];
            }
            if ((unused & completelyExcluded) != 0) return;

            // Consider each unused non-excluded value as a candidate for perm[off]
            var candidates = unused & ~excluded[off];
            for (int val = 0; candidates > 0; val++, candidates >>= 1)
            {
                if ((candidates & 1) == 0) continue;

                perm[off] = val;

                var nextUnused = unused & ~(1 << val);

                var nextExcluded = (int[])excluded.Clone();
                // For each (non-trivial) subset of smaller indices, combine with off and extrapolate to off+1 ... excluded.Length-1
                if (off < n - 1 && off > 0)
                {
                    var points = new Point[off + 1];
                    var denoms = new BigInteger[off + 1];
                    points[0] = new Point { X = off, Y = perm[off] };
                    denoms[0] = 1;
                    ExtendExclusions(perm, off, 0, points, 1, denoms, nextExcluded);
                }

                // Symmetry exclusion: perm[0] < perm[-1] < n - 1 - perm[0]
                if (off == 0 && n > 1)
                {
                    nextExcluded[n - 1] |= (1 << n) - (2 << (n - 1 - val));
                    nextExcluded[n - 1] |= (2 << val) - 1;
                }

                CountValidPerms(ref count, perm, off + 1, nextUnused, nextExcluded);
            }
        }

        private static void ExtendExclusions(int[] perm, int off, int idx, Point[] points, int numPoints, BigInteger[] denoms, int[] excluded)
        {
            if (idx == off) return;

            // Subsets without
            ExtendExclusions(perm, off, idx + 1, points, numPoints, denoms, excluded);

            // Just add this to the subset
            points[numPoints] = new Point { X = idx, Y = perm[idx] };
            denoms = (BigInteger[])denoms.Clone();
            // Update invariant: denoms[s] = prod_{t != s} points[s].X - points[t].X
            denoms[numPoints] = 1;
            for (int s = 0; s < numPoints; s++)
            {
                denoms[s] *= points[s].X - points[numPoints].X;
                denoms[numPoints] *= points[numPoints].X - points[s].X;
            }
            numPoints++;

            for (int target = off + 1; target < excluded.Length; target++)
            {
                BigInteger prod = 1;
                for (int t = 0; t < numPoints; t++) prod *= target - points[t].X;

                Rational sum = new Rational(0, 1);
                for (int s = 0; s < numPoints; s++) sum += new Rational(prod / (target - points[s].X) * points[s].Y, denoms[s]);

                if (sum.Denom == 1 && sum.Num >= 0 && sum.Num < excluded.Length) excluded[target] |= 1 << (int)sum.Num;
            }

            // Subsets with
            ExtendExclusions(perm, off, idx + 1, points, numPoints, denoms, excluded);
        }

        private static int CountSymmetries(int[] perm)
        {
            if (perm.Length < 2) return 1;

            int cmp = 0;
            for (int i = 0, j = perm.Length - 1; i <= j; i++, j--)
            {
                cmp = perm.Length - 1 - perm[i] - perm[j];
                if (cmp != 0) break;
            }

            return cmp > 0 ? 4 : cmp == 0 ? 2 : 0;
        }

        public struct Point
        {
            public int X;
            public int Y;
        }

        public struct Rational
        {
            public Rational(BigInteger num, BigInteger denom)
            {
                if (denom == 0) throw new ArgumentOutOfRangeException(nameof(denom));

                if (denom < 0) { num = -num; denom = -denom; }

                var g = _Gcd(num, denom);
                Num = num / g;
                Denom = denom / g;
            }

            private static BigInteger _Gcd(BigInteger a, BigInteger b)
            {
                if (a < 0) a = -a;
                if (b < 0) b = -b;
                while (a != 0)
                {
                    var tmp = b % a;
                    b = a;
                    a = tmp;
                }
                return b;
            }

            public BigInteger Num;
            public BigInteger Denom;

            public static Rational operator +(Rational a, Rational b) => new Rational(a.Num * b.Denom + a.Denom * b.Num, a.Denom * b.Denom);
        }
    }
}

nकमांड-लाइन तर्कों के मूल्यों को लेता है, या यदि तर्कों के बिना चलता है, तो कई बार n=10। वीएस 2017 में "रिलीज़" के रूप में संकलन और एक इंटेल कोर i7-6700 पर चल रहा है मैं n=91.2 सेकंड में गणना करता हूं , और n=1013.6 सेकंड में। n=11बस 2 मिनट से अधिक है।

FWIW:

n    a(n)
9    10408
10   45244
11   160248
12   762554
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.