There is no contradiction between Dt 5:9 and 24:16. As with so many things, the answer to your question is not an "either/or" but a "both/and."
I also agree with this answer that "visiting" is not exactly the same as "punishing," even though the NIV uses the word "punishing."
Sin is an insidious thing. Since no man or woman is an island, our sinful choices have negative effects inter-generationally. Both sin, as in idol worship, and good deeds, as in loving God and obeying His commandments, bear fruit. We all reap what we sow, individually and corporately (Ez 18:4,20; Ga 6:7,8). Interestingly, while some sins, as Paul points out, are evident in this life and in that sense precede us into judgment, some sins are not so evident--"hidden"--in this life, but they do follow us, individually and inter-generationally, into judgment (I Ti 5:24,25). (The word follow carries with it, I suggest, the hint of the progenitor-to-progeny succession of sin.)
Is there reason for hope that this intergenerational "curse" can be broken? Yes! God shows His steadfast love to those who repent and begin--and continue--to love God and keep His commandments.
Notice, too, the chiastic contrast between the "third and fourth generation of those who hate Me" and "a thousand generations of those who love Me . . .." (De 5:9 NIV). In analogous form you have: visit/punish the children of three or four generations is to those who hate Me, as those who love and obey me is to showing love to a thousand generations.
Culling from what little I know of Hebrew (next to nothing!), Hebrew might express this analogy even be more succinctly (and please correct me if I am wrong):
"LORD visits [a] few hating-generations; loves many loving-/obeying [generations]."
God's love in so many ways is so much greater than sin! Amen?
If, on the other hand, the progeny who refuse to repent of their father's idolatry also choose to hate God by following in their father's footsteps, there will be inevitably and inexorably an unpleasant "visitation" by God. Each child is accountable for his or her own sin, however, and un-repented-of sin bears the fruit of spiritual death in every generation, both in time and eternity.
Thank God He still graciously encourages us sinners to repent, and His doing so offers us a way to break the chain of intergenerational sin, for perhaps thousands of generations!
In conclusion, while it is true that each progenitor's descendant starts out with a "dirty slate," since we are all, like David, "brought forth in iniquity . . . and [conceived in sin]" (Ps 51:5), it is not inevitable in some fatalistic way that he or she continue in sin as a way of life; we all have a choice, as Joshua indicated in Jos 24:14,15:
"Now, therefore, fear the LORD and serve Him in sincerity and truth; and put away the gods which your fathers served beyond the River and in Egypt, and serve the LORD. If it is disagreeable in your sight to serve the LORD, choose for yourselves today whom you will serve: whether the gods which your fathers served which were beyond the River, or the gods of the Amorites in whose land you are living; but as for me and my house, we will serve the LORD."
I apologize in advance for my prolix answer. If you think it can benefit from some judicious editing, please help yourself.