Even at +19 to hit, you need to roll a natural 20 to hit AC 40. And you should have a -6 range penalty at 500 feet, even with a Composite Longbow and the Far Shot feet. Catching the dragon flat-footed won't help, as he has no dex bonus to his AC.
Even if you do get your attack bonus up high enough that you can hit him, you better hope that he doesn't have the Wind Wall spell. As a 15th level caster it create a Wind Wall that is 75 feet high and 150 feet long, and can put it 250 feet in front of it. Cast it out to the extent of it's range in the direction the arrow came from, and advance under cover of the wall, completely immune to arrows.