Consider all possible integers consisting of exactlyk digits, where each digit is chosen independently from the integer range [l, r] (inclusive). If 0 is included in the range, leading zeros are allowed.
Return an integer representing the sum of all such numbers. Since the answer may be very large, return it modulo10^9 + 7.
classSolution { longlong mod = 1e9 + 7; longlongmodpow(longlong n, longlong x, longlong mod){ if(x<0){ returnmodpow(modpow(n,-x,mod),mod-2,mod); } n%=mod; longlong res=1; while(x){if(x&1){res=res*n%mod;}n=n*n%mod;x>>=1;}return res; } public: intsumOfNumbers(int l, int r, int k){ longlong po = modpow(r - l + 1, k - 1, mod); longlong res = 0, sum = 0; for(int i = l; i <= r; i++) sum += i; longlong base = po * sum % mod; longlong p = (modpow(10, k, mod) - 1 + mod) % mod; longlong q = modpow(9, mod - 2, mod); return base * p % mod * q % mod; } };