CSES - Datatähti 2023 alku - Results
Submission details
Task:Kertoma
Sender:Esinko
Submission time:2022-11-01 09:48:16 +0200
Language:Node.js
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1--1, 2, 3details
#2--1, 2, 3details
#3--1, 2, 3details
#4--1, 2, 3details
#5--1, 2, 3details
#6--1, 2, 3details
#7--2, 3details
#8--2, 3details
#9--2, 3details
#10--2, 3details
#11--3details
#12--3details
#13--3details
#14--3details
#15--3details
#16--3details

Code

// Päättele luku x
// Kun saat tietoa kertomasta

// const { performance } = require("perf_hooks")

const { createInterface } = require("readline")
const interface = createInterface({
    input: process.stdin,
    terminal: false
})

function factorial(number) {
    let returnValue = 1n
    for (let i = 2; i <= number; i++) returnValue *= BigInt(i)
    return returnValue
}

// Implements Stirling approximation
// Total black magic!
function factorialApproximation(number) {
    number += 1
    //return Math.ceil(Math.sqrt(2 * Math.PI / number) * ((number / Math.E).toFixed(0)) ** number)
    return BigInt(Math.ceil(Math.sqrt(2 * Math.PI / number).toFixed(0) + 1)) * (BigInt((number / Math.E).toFixed(0)) ** BigInt(number))
}

function getCounts(string) {
    let counts = new Array(10).fill(0)
    for (const char of string.split("")) {
        if (isNaN(parseInt(char))) continue
        counts[parseInt(char)] += 1
    }
    return counts
}

const manualTarget = 100000n
const fallbackThreshold = 2n

interface.once("line", (line) => {
    const expectedCounts = line.split(" ")
        .map((val) => parseInt(val))

    // const start = performance.now()
    
    // Find the expected length of the factorial and find a factorial within a range of lengths with a given precision
    const expectedLength = expectedCounts.reduce((a, b) => a + b)
    let currentTarget = [1n, manualTarget]
    while(true) {
        // If the min factorial's length is more than the mean of the min-max factorial, add the mean as the min
        // and the other way around for if it's larger
        if (currentTarget[1] - currentTarget[0] < fallbackThreshold) break
        const newTarget = currentTarget[0] + (currentTarget[1] - currentTarget[0]) / 2n
        const testFactorial = factorialApproximation(parseInt(newTarget.toString()))
        if (testFactorial.toString().length > expectedLength) currentTarget[1] = newTarget
        else currentTarget[0] = newTarget
    }
    // console.log("Achieved precision of", (currentTarget[1] - currentTarget[0]).toString(), "at", (performance.now() - start) / 1000, "s")

    // Now we know the number is one of at max 10 factorials, calculate them all until we find a match
    let lastValue = factorial(currentTarget[0] - 1n)
    let i = currentTarget[0] - 1n
    while (i !== manualTarget) {
        ++i
        lastValue *= i
        const calculatedCounts = getCounts(lastValue.toString())
        if (expectedCounts.join("") === calculatedCounts.join("")) {
            console.log(i.toString())
            break
        }
    }
    process.exit()
})

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
0 0 1 0 0 0 0 0 0 0

correct output
2

user output
(empty)

Test 2

Group: 1, 2, 3

Verdict:

input
0 0 0 0 0 0 1 0 0 0

correct output
3

user output
(empty)

Test 3

Group: 1, 2, 3

Verdict:

input
0 0 1 0 1 0 0 0 0 0

correct output
4

user output
(empty)

Test 4

Group: 1, 2, 3

Verdict:

input
2 0 1 1 0 0 1 0 2 0

correct output
10

user output
(empty)

Test 5

Group: 1, 2, 3

Verdict:

input
9 3 1 1 2 2 3 1 6 1

correct output
27

user output
(empty)

Test 6

Group: 1, 2, 3

Verdict:

input
10 4 3 4 3 2 2 4 3 7

correct output
36

user output
(empty)

Test 7

Group: 2, 3

Verdict:

input
71 53 36 30 25 29 42 24 34 29

correct output
199

user output
(empty)

Test 8

Group: 2, 3

Verdict:

input
71 33 46 38 27 45 36 21 35 35

correct output
205

user output
(empty)

Test 9

Group: 2, 3

Verdict:

input
93 38 35 26 43 54 38 25 41 34

correct output
222

user output
(empty)

Test 10

Group: 2, 3

Verdict:

input
100 33 33 45 36 43 38 54 56 36

correct output
242

user output
(empty)

Test 11

Group: 3

Verdict:

input
3419 1797 1845 1849 1879 1791 ...

correct output
5959

user output
(empty)

Test 12

Group: 3

Verdict:

input
4776 2695 2709 2781 2616 2753 ...

correct output
8391

user output
(empty)

Test 13

Group: 3

Verdict:

input
20097 12282 12229 12214 12406 ...

correct output
32001

user output
(empty)

Test 14

Group: 3

Verdict:

input
47934 29918 29878 29713 29984 ...

correct output
71718

user output
(empty)

Test 15

Group: 3

Verdict:

input
84691 54156 54277 54533 54296 ...

correct output
123123

user output
(empty)

Test 16

Group: 3

Verdict:

input
99098 63339 63878 64182 63904 ...

correct output
142663

user output
(empty)