When can I use the formula P=V^2/R?
The way P=V^2/R works is very much different than they way you applied it. 

The voltage in that formula must be the one across the load or subject of which you are interested to calculate the power, and the R is the resistance of that subject.

This raise a question. How did you come to the state the 40KV is across the end to end of the power line?  The power line which is having only mere one ohm of resistance?

In order to calculate power loss in the transmission lines, you need to know the power received by the load end and the voltage at the load end. Use these two to calculate the current drawn from the line. Here you use P=VI. Then use the this current value and the total resistance of the transmission line to calculate the power loss. Here you use P=I^2.R.