You need to remember, your range much above 30 Mhz is more a factor of your Hight Over Average Terrain than TX power, this is known as the radio horizon.
To get 40 Km at VHF/UHF, your formula is 1.451 x ( SQR (mast in meters / 0.3048) ) * 1.60943 = range in Km ... In Excel it looks like this: =(1.451*(SQRT(89.43/0.3048)))*1.60943
So you're going to require a 90m mast / building / hill etc.
OK, now we have taken care of that part, lets turn to the TX ... The factor you have to consider most is your fade margin, this means when its raining, foggy etc, your signal will be
attenuated. Commercial & Government broadcasters also factor in loft antennas, poor rusty antennas that date back to the 70's etc. Typically a 20 dB safety margin is used.
If you consider an average TV to have a minimal signal requirement of 60 dBuV + our 20 dB safety margin as above, your looking to achieve 80 dBuV at 40Km range.
Getting back to the formulas again, a good rule of thumb for path loss is 96.6 + (20xlog frequency in Mhz) + (20xlog distance in miles),
in excel is looks like this: =96.6+(20*LOG(500/1000))+(20*LOG(24.85351957/1.6))
working this backwards at say 500 Mhz (around channel 25 in most system) you have a path loss of say 120 dB.
Now we can calculate TX power ... Lets start with our RX signal of 80 dBuV - 107 to give us -27 dBm, add our path loss back and lets add 3dbi gain for your RX & TX antennas (-27 + 120 - 3 - 3)
This gives a rough TX power requirement of 87 dBm ... which is close enough to 90 dBm which is 1 MILLION WATTS.
This would be in line with some commercial TV towers I just looked up.
If you wanted bare bones coverage, say 50 dBuV @ 40 Km and were willing to accept signal dropouts during rain etc, you could probably lower the power 30 dB (60 dBm) to around 1,000 Watts.
You have to remember RF is logarithmic meaning anything under a 4x isn't really noticed (I.E. power doesn't go 100w, 200w, 300w, it goes 500w, 2Kw, 8kw etc).
Play around with the formulas, I typed this in quickly so double check the formulas before doing extensive research.
I've heard of a group in Italy getting ~ 2.5 Km radius using 20W UHF, perhaps start small and work your way up
GFG