Concatenation of Consecutive Binary Numbers
给一个数字n, 求[1,n]的所有数变成二进制后, 连接起来的数的十进制表示, 需要取模10e9+7后的数字.
这个题我自己看了下答案, 里面居然还有用逆元求等比数列和的做法, 我还在研究中.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public int concatenatedBinary(int n) { int MOD = 1000000000+7; int res = 0; for(int i = 1; i <= n; i++) { String s = Integer.toBinaryString(i); for(int j = 0; j < s.length(); j++) { res = (res * 2 + (s.charAt(j) == '0'? 0 : 1)) % MOD; } } return res; } } |