给定一个正整数N ,任务是计算N位数字的数量,使得数字中的每个索引(基于 1 的索引)都可以被出现在该索引处的数字整除。由于法院可以非常大,打印它模10 9 + 7 。
例子:
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
朴素方法:解决问题的最简单方法是生成所有可能的N位数字,并对每个这样的数字,检查其所有数字是否满足要求的条件。
时间复杂度: O(10 N *N)
辅助空间: O(1)
高效的方法:优化上述方法的思路是观察这样一个事实,即对于每个位置,从 1 到 9 有 9 个可能的选项。检查每个可能的选项并找到结果。
请按照以下步骤解决此问题:
- 将变量ans初始化为1以存储答案。
- 使用变量索引迭代范围[1, N]并执行以下任务:
- 初始化变量,比如说选择为0,存储的该特定指数期权的数量。
- 使用变量数字迭代范围[1, 9]并执行以下任务:
- 如果index%digit等于0,则将选项的值增加1。
- 将ans的值设置为(ans*1LL*choices)%mod 。
- 执行完上述步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int mod = 1e9 + 7;
// Function to count N-digit numbers
// such that each position is divisible
// by the digit occurring at that position
void countOfNumbers(int N)
{
// Stores the answer.
int ans = 1;
// Iterate from indices 1 to N
for (int index = 1; index <= N; ++index) {
// Stores count of digits that can
// be placed at the current index
int choices = 0;
// Iterate from digit 1 to 9
for (int digit = 1; digit <= 9; ++digit) {
// If index is divisible by digit
if (index % digit == 0) {
++choices;
}
}
// Multiply answer with possible choices
ans = (ans * 1LL * choices) % mod;
}
cout << ans << endl;
}
// Driver Code
int main()
{
// Given Input
int N = 5;
// Function call
countOfNumbers(N);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
static int mod = 1000000007;
// Function to count N-digit numbers
// such that each position is divisible
// by the digit occurring at that position
static void countOfNumbers(int N)
{
// Stores the answer.
int ans = 1;
// Iterate from indices 1 to N
for(int index = 1; index <= N; ++index)
{
// Stores count of digits that can
// be placed at the current index
int choices = 0;
// Iterate from digit 1 to 9
for(int digit = 1; digit <= 9; ++digit)
{
// If index is divisible by digit
if (index % digit == 0)
{
++choices;
}
}
// Multiply answer with possible choices
ans = (ans * choices) % mod;
}
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int N = 5;
// Function call
countOfNumbers(N);
}
}
// This code is contributed by shivanisinghss2110
Python3
# python program for the above approach
mod = 1000000000 + 7
# Function to count N-digit numbers
# such that each position is divisible
# by the digit occurring at that position
def countOfNumbers(N):
# Stores the answer.
ans = 1
# Iterate from indices 1 to N
for index in range(1, N + 1):
# Stores count of digits that can
# be placed at the current index
choices = 0
# Iterate from digit 1 to 9
for digit in range(1, 10):
# If index is divisible by digit
if (index % digit == 0):
choices += 1
# Multiply answer with possible choices
ans = (ans * choices) % mod
print(ans)
# Driver Code
# Given Input
N = 5
# Function call
countOfNumbers(N)
# This code is contributed by amreshkumar3.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static int mod = 1000000007;
// Function to count N-digit numbers
// such that each position is divisible
// by the digit occurring at that position
static void countOfNumbers(int N)
{
// Stores the answer.
int ans = 1;
// Iterate from indices 1 to N
for (int index = 1; index <= N; ++index) {
// Stores count of digits that can
// be placed at the current index
int choices = 0;
// Iterate from digit 1 to 9
for (int digit = 1; digit <= 9; ++digit) {
// If index is divisible by digit
if (index % digit == 0) {
++choices;
}
}
// Multiply answer with possible choices
ans = (ans * choices) % mod;
}
Console.Write(ans);
}
// Driver Code
public static void Main()
{
// Given Input
int N = 5;
// Function call
countOfNumbers(N);
}
}
// This code is contributed by bgangwar59.
Javascript
输出:
24
时间复杂度: O(10 * N)
辅助空间: O(1)