Author : shailesh_2004
Required Knowledge : DP, Math, Modulo Arithmetic
Time Complexity :
Editorialist : shailesh_2004
The value of depends on the value of
. The time complexity will be
for each
if we calculate the given expression. If we do this for
cases then it will become
which gives us TLE. Thus, we should precompute all values from
to
.
Let represent the number
repeating
times.
Let's divide the value for into three parts:
(This is not addition, it's concatenation)
To do this concatenation we need number of digits in the value of . Let's store each value's size in another array (where
represents number of digits in
). Let
be number of digits in the number
.
First let's calculate .
To find pattern, first observe any single digit number, let's say :
Now observe for multiple digits, let's say :
Generalization for multiple digits:
Take common, remaining part is in geometric progression with initial value as
and ratio as
and there are n terms.
By using sum of GP formula which is:
We can compute as follows:
(Use Modular Inverse)
Let represent size of
.
It's obvious that
Now, let's evaluate the answer by concatention:
(This is not addition, it's concatenation)
First let's concatenate second part to first part:
We need to append zeroes to
and add
. As
is large use binary exponentiation and modulo.
Then let's concatenate third part to current
We need to append zeroes and add n to it.
Time complexity is , here
factor is because of binary exponentiation and modular inverse.
// Author : Shailesh
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vll vector<ll>
#define pb push_back
#define mp make_pair
#define endl "\n"
#define mod 1000000007
#define tot(a) a.begin(),a.end()
#define minii *min_element
#define maxii *max_element
ll binexp(ll a,ll b,ll mod2=mod)
{
ll res=1;
a%=mod2;
while(b)
{
if(b%2)res=(res*a)%mod2;
a=(a*a)%mod2;
b>>=1;
}
return res;
}
ll modular_inverse(ll a,ll mod2=mod)
{
// By Fermat's little theorem
return binexp(a,mod2-2,mod2);
}
ll ans[100001];
void precompute()
{
ans[1]=1;
ll sz[100001];
sz[1]=1;
ll i;
ll tens[6]={10,100,1000,10000,100000};
ll j=0;
ll d=1;
for(i=2;i<=100000;i++)
{
if(i==tens[j])
{
j++;
d++;
}
sz[i]=i*d+sz[i-1]+i*d;
ll val=((i*(binexp(10,d*i)-1))%mod * modular_inverse(binexp(10,d) - 1))%mod;
ans[i]=(((((val*binexp(10,sz[i-1]))%mod + ans[i-1])%mod)*binexp(10,i*d))%mod + val)%mod;
}
}
void solve()
{
ll n;
cin>>n;
cout<<ans[n]<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
precompute();
ll t;cin>>t;while(t--)
solve();
return 0;
}
// Author : Naveen
// Program Start
// Libraries and Namespace Start
#include <bits/stdc++.h>
using namespace std;
// Libraries and Namespace End
//----------------------------------------------------------------
// Important Shortcuts Start
// Macros Start
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(), v.end()
// Macros End
// Declarations Start
typedef long long int ll;
typedef vector<ll> vll;
// Declarations End
// Constants Start
const ll mod1 = 1e9 + 7;
const char newl = '\n';
// Constants End
// Binary Exponentiation Start
ll power(ll b, ll p, const ll mod) {
if (b <= 0 || p < 0) {
throw invalid_argument("The numbers can't be negative.");
}
ll res = 1;
b %= mod;
while (p > 0) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
// Binary Exponentiation End
// Modular Arithmetic Start
// Modular Inverse Function Start
inline ll mod_inv(ll n, const ll mod = mod1) {
if (n < 0) {
throw invalid_argument("The number can't be negative.");
}
return power(n, mod - 2, mod);
}
// Modular Inverse Function End
// Modular Arithmetic End
//----------------------------------------------------------------
// Solution Class Start
class Solution {
public:
vll ans;
Solution() {
ans.assign(1e5 + 1, 0);
ans[1] = 1;
ll cou = 1;
ll d = 1;
for (ll i = 2;i <= ans.size();i++) {
switch (i) {
case 10:
case 100:
case 1000:
case 10000:
case 100000:
d++;
}
ll p = power(10, i * d, mod1);
ans[i] = (ans[i - 1] * p) % mod1;
cou += i * d;
p = power(10, i * d, mod1);
ll val = ((((p + mod1 - 1) % mod1) * (mod_inv((power(10, d, mod1) + mod1 - 1) % mod1, mod1))) % mod1 * i) % mod1;
ans[i] = ((val * power(10, cou, mod1)) % mod1 + (ans[i] + val) % mod1) % mod1;
cou += i * d;
}
}
void solve(ll index) {
//----------------------------------------------------------------
ll n, i, j, k;
cin >> n;
cout << ans[n] << newl;
// cout << "Case #" << index << ": " << ans << newl;
//----------------------------------------------------------------
}
bool test_cases = true;
};
// Solution Class End
// Main Function Start
int main() {
Solution sol;
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll test_cases = 1;
if (sol.test_cases) {
cin >> test_cases;
}
for (ll test_case = 1; test_case <= test_cases; ++test_case) {
sol.solve(test_case);
}
return 0;
}
// Main Function End
// Program End
//----------------------------------------------------------------