We have, two option for each digit of a n digit number
so, total 2n numbers are possible
1 and 2 should be used once, so we have to subtract the numbers which have all digits as1 and all digits as 2
two such cases are possible, hence
2n−2=510
⇒2n=512
⇒n=9