ยาก 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Digit Factorial Chains

Count starting numbers whose digit factorial chain has exactly a given number of distinct terms.

สิ่งที่คุณจะได้เรียนรู้

  • Follow a chain until a value repeats
  • Find chain lengths inside and before a loop
  • Reuse stored lengths

โจทย์

ดูฉบับแปล

Replace a number by the sum of the factorials of its digits: 145 gives 1! + 4! + 5! = 145 again, and 69 gives 6! + 9! = 363600. Repeating this always falls into a loop. The chain of a start is its terms until the first repeat: 69, 363600, 1454, 169, 363601 and then 1454 again, so 69 has 5 distinct terms. Count the starting numbers from 1 to limit whose chain has exactly length distinct terms.

Implement digit_factorial_chains(limit, length).

Many chains run into the same numbers. Store each number's chain length once it is known and reuse it.

Input

limit: The largest starting number.

length: The number of distinct terms wanted, the start included.

Output

Return how many starting numbers n with 1 ≤ n ≤ limit have exactly length distinct terms in their chain.

เงื่อนไข

1 ≤ limit ≤ 100,000; 1 ≤ length ≤ 70

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
limit = 2
length = 1

ผลลัพธ์ที่คาดหวัง:
expected = 2
อินพุต:
long long limit = 2;
long long length = 1;

ผลลัพธ์ที่คาดหวัง:
long long expected = 2;
อินพุต:
long limit = 2;
long length = 1;

ผลลัพธ์ที่คาดหวัง:
long expected = 2;

คำอธิบาย

1! = 1 and 2! = 2, so 1 and 2 each map to themselves and have a chain of 1 term. The answer is 2.

ตัวอย่างที่ 2
อินพุต:
limit = 10
length = 2

ผลลัพธ์ที่คาดหวัง:
expected = 1
อินพุต:
long long limit = 10;
long long length = 2;

ผลลัพธ์ที่คาดหวัง:
long long expected = 1;
อินพุต:
long limit = 10;
long length = 2;

ผลลัพธ์ที่คาดหวัง:
long expected = 1;

คำอธิบาย

3 gives 6, then 720, 5043, ... so it is long. 10 gives 1! + 0! = 2 and then 2 again: two terms. Only 10 has exactly 2, so the answer is 1.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Precompute 0! to 9! once.

คำใบ้ 2

Record each value's step number in the current chain; a repeat tells you where the loop starts.

คำใบ้ 3

Values in the loop all have the loop's length, and each value before it has one more than the next.

ความซับซ้อนที่คาดหวัง

O(limit × log limit) time with memoization, O(limit) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก