Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Current solution for Project Euler 250+ (HackerRank) is giving incorrect answers, why is this?

Post

Current solution for Project Euler 250+ (HackerRank) is giving incorrect answers, why is this? [closed]

+2
−2

Closed as too generic by Karl Knechtel‭ on May 7, 2025 at 20:52

This post contains multiple questions or has many possible indistinguishable correct answers or requires extraordinary long answers.

This question was closed; new answers can no longer be added. Users with the Vote on Holds ability may vote to reopen this question if it has been improved or closed incorrectly.

Note: I originally posted this on Stack Overflow on Stack Exchange here however I am posting the same question here as I might be able to get a answer more quickly here.


Project Euler+ #250 on HackerRank is as follows:

Find the number of non-empty subsets of $\{1^1, 2^2, 3^3,..., n^n\}$, the sum of whose elements are divisible by $k$. Print your answer modulo $10^9$

You can find this here.

The constraints are as follows:

$$\begin{align}1\le&\;n\le10^{400}\\3\le&\;k\le50\end{align}$$

I originally posted a Code Review question here, but I have since modified my solution after taking a 2-month break from HackerRank and after solving the original problem on Project Euler.


Here is my current code (using Python 3.12):

from math import lcm

MOD = 10 ** 9

def phi(n):
    c = 2
    s = n
    while n > 1:
        if n % c == 0:
            s -= s // c
        
        while n % c == 0:
            n //= c
        
        c += 1
    
    return s

def compute(N, n, m = MOD):
    cycle = lcm(n, phi(n))
    # A guess for what the period for `i ** i % k` will be.
    
    def convolve(A, B):
        return [sum(a * b for a, b in zip(A, B[i::-1] + B[:i:-1])) % m for i in range(n)]
    
    q, rem = divmod(N, cycle)
    A = end = [1] + [0] * (n - 1)
    for i in range(1, cycle + 1):
        k = pow(i, i, n)
        A = [(a + b) % m for a, b in zip(A, A[-k:] + A[:-k])]
        if i == rem:
            end = A
    
    base, A = A, [1] + [0] * (n - 1)
    for j in bin(q)[2:]:
        A = convolve(A, convolve(A, base) if j == '1' else A)
    
    A = convolve(A, end)
    return (A[0] - 1) % m
#---------------------------------------------------------------
n, k = map(int, input().split())
print(compute(n, k))

However, this does not solve all 56 test cases. In fact, for all 9 test cases I don't solve (those being hidden test cases 11, 16, 17, 20, 23, 28, 32, 52, 53), I am getting incorrect answers.


My question is: What is wrong with my code that is causing me to get incorrect answers, and how could I possibly fix this?

Something tells me that it has either to do something with my guess for the cycle parameter for some values of $k$, or maybe the problem is with how I am computing the convolutions, but I'm not really sure.

History

2 comment threads

Non-empty? (7 comments)
Far too broad and underspecified (2 comments)
Non-empty?
Lundin‭ wrote over 1 year ago

What does non-empty subsets even mean? How can any item in the given set be "empty"? The specification of the task seems quite unclear.

CrSb0001‭ wrote over 1 year ago

Hi, Project Euler is usually very mathy, I recommend looking at this if you don't understand. Basically it's the set that has no elements, and since its sum is defined (courtesy of the empty sum) to be 0, the sum is always divisible by $k$, and so we have to subtract 1 from the final result.

Lundin‭ wrote over 1 year ago

But since n is specified to be >= 1 then how can it be empty.

Derek Elkins‭ wrote over 1 year ago · edited over 1 year ago

@Lundin This is not ambiguous or even remotely unusual wording. Given the set {1, 4, 27}, say, the subsets of this set are {1,4,27}, {1,4}, {1,27}, {4,27}, {1}, {4}, {27}, and {}. The last is the empty subset. The sum of all the non-empty subsets would thus be 32, 5, 28, 31, 1, 4, and 27. (This example set, of course, corresponds exactly to the n=3 case of the problem.) I am genuinely confused at you being confused by this, so I'm inclined to write it off as a thinko.

Lundin‭ wrote over 1 year ago

Derek Elkins‭ How can n^n ever be empty for any n>=1?

CrSb0001‭ wrote over 1 year ago

Lundin‭ If you're seriously that confused about the empty set (which is IMO very surprising, because of how intuitive it is), I recommend taking a look at either the Wikipedia article I linked 2 days ago or see this video about it.

Derek Elkins‭ wrote over 1 year ago

Lundin‭ My comment was responding to your initial comment, not to the one immediately preceding my comment. Whether n^n is "empty" or not is generally considered a nonsensical question. Even within ZFC where everything is a set - and so the question is at least well-formed - it would depend on your encoding of numbers into sets. This is beside the point because nothing about the problem statement ever brings up that question.

In my previous comment, I listed all the subsets of the set {1,4,27}. Do you agree or disagree that those are the subsets? If you disagree, what would you say are the subsets? If you agree, then n^n for any n isn't one of the things I list, though I do have {1^1}, {2^2}, and {3^3}, i.e. the singleton sets containing 1^1, 2^2, 3^3 respectively. Given this, it should be clear that the problem statement isn't saying anything about the "emptiness" of n^n. A subset of a set S is a set all of whose elements are elements of S. One of those (sub)sets is the empty set.