Count N-digit numbers such that every position is divisible by the digit at that position
Last Updated :
22 Jul, 2021
Given a positive integer N, the task is to count the number of N-digit numbers such that every index (1-based indexing) in the number is divisible by the digit occurring at that index. Since the court can be very large, print it modulo 109 + 7.
Examples:
Input: N = 2
Output: 2
Explanation: The numbers 11 and 12 are the only 2-digit numbers satisfying the condition.
Input: N = 5
Output: 24
Naive Approach: The simplest approach to solve the problem is to generate all possible N-digit numbers and for each such number, check if all its digits satisfy the required condition or not.
Time Complexity: O(10N*N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to observe the fact that for every position, there are 9 possible options from 1 to 9. Check for every possible option and find the result.
Follow the steps below to solve this problem:
- Initialize the variable ans as 1 to store the answer.
- Iterate over the range [1, N] using the variable index and perform the following tasks:
- Initialize the variable, say choices as 0, to store the number of options for that particular index.
- Iterate over the range [1, 9] using a variable digit and perform the following tasks:
- If index%digit is equal to 0, then increase the value of choices by 1.
- Set the value of ans as (ans*1LL*choices)%mod.
- After performing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void countOfNumbers( int N)
{
int ans = 1;
for ( int index = 1; index <= N; ++index) {
int choices = 0;
for ( int digit = 1; digit <= 9; ++digit) {
if (index % digit == 0) {
++choices;
}
}
ans = (ans * 1LL * choices) % mod;
}
cout << ans << endl;
}
int main()
{
int N = 5;
countOfNumbers(N);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static int mod = 1000000007 ;
static void countOfNumbers( int N)
{
int ans = 1 ;
for ( int index = 1 ; index <= N; ++index)
{
int choices = 0 ;
for ( int digit = 1 ; digit <= 9 ; ++digit)
{
if (index % digit == 0 )
{
++choices;
}
}
ans = (ans * choices) % mod;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 5 ;
countOfNumbers(N);
}
}
|
Python3
mod = 1000000000 + 7
def countOfNumbers(N):
ans = 1
for index in range ( 1 , N + 1 ):
choices = 0
for digit in range ( 1 , 10 ):
if (index % digit = = 0 ):
choices + = 1
ans = (ans * choices) % mod
print (ans)
N = 5
countOfNumbers(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int mod = 1000000007;
static void countOfNumbers( int N)
{
int ans = 1;
for ( int index = 1; index <= N; ++index) {
int choices = 0;
for ( int digit = 1; digit <= 9; ++digit) {
if (index % digit == 0) {
++choices;
}
}
ans = (ans * choices) % mod;
}
Console.Write(ans);
}
public static void Main()
{
int N = 5;
countOfNumbers(N);
}
}
|
Javascript
<script>
const mod = 1e9 + 7;
function countOfNumbers(N)
{
let ans = 1;
for (let index = 1; index <= N; ++index) {
let choices = 0;
for (let digit = 1; digit <= 9; ++digit) {
if (index % digit == 0) {
++choices;
}
}
ans = (ans * 1 * choices) % mod;
}
document.write(ans);
}
let N = 5;
countOfNumbers(N);
</script>
|
Time Complexity: O(10 * N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...