The 13th entry of "to" in OALD：
used to show a quantity or rate
There are 2.54 centimetres to an inch.
This car does 30 miles to the gallon.
I'm wondering why the sencond example is used with the, not a.
"<This car> does 30 miles to the gallon" is an informal idiom. In this case, "the gallon" is not a specific gallon.
"<This car> gets 30 miles per gallon" is a more formal idiom with the same meaning.
"Given a gallon of gas, <this car> will go 30 miles" is a less common way of saying the same thing. In this case, the choice between "a" and "the" is based on whether you have a specific gallon of gas in mind. If someone just put a gallon of gas in the car, you would say "the gallon". If you are speaking about the car's gas mileage in general, you would say "a gallon".
There doesn't seem to be a consensus by usage, except that the is preferred to a.
Source: Google Books Ngram
Either one is acceptable, but searching for usage in context shows a different pattern. to the gallon is primarily associated with the ratio of miles to gallons:
But to a gallon is primarily associated with addition or additives:
When in doubt, do what I did. Plug the concept into a search engine and see the context.