长度为 N 的数的计数,在奇数索引处具有素数,在偶数索引处具有奇数
给定一个数字N ,任务是计算长度为 N的数字的计数,这些数字在奇数索引处具有素数,在偶数索引处具有奇数。
示例:
Input : N = 1
Output: 5
Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers.
Input: N = 2
Output: 20
Explanation: There are 20 valid numbers of length 2.
方法:这个问题可以在组合学的帮助下解决。奇数索引的数字有 4 个选择,偶数索引的数字有 5 个选择。
请按照以下步骤解决问题:
- 偶数索引有 5 种选择(1、3、5、7、9),奇数索引有 4 种选择(2、3、5、7)。
- 对于长度为N 的数字,将有N/2 个奇数索引和 ( N/2 + N%2 ) 个偶数索引。
- 因此,填充 N/2 奇数索引的方法数为4 N/2 。
- 填充偶数索引的方法数量为5 (N/2 + N%2) 。
- 因此,所有有效数字的总数将为4 N/2 * 5 (N/2 + N%2) 。
以下是上述方法的实现:
C++
// c++ program to Count of numbers of length
// N having prime numbers at odd indices and
// odd numbers at even indices
#include
using namespace std;
// function to find total number of ways
int find_Numb_ways(int n)
{
// No of odd indices in n-digit number
int odd_indices = n/2;
// No of even indices in n-digit number
int even_indices = (n / 2) + (n % 2);
// No of ways of arranging prime number
// digits in odd indices
int arr_odd = pow(4, odd_indices);
// No of ways of arranging odd number
// digits in even indices
int arr_even = pow(5, even_indices);
// returning the total number of ways
return arr_odd * arr_even;
}
// drive code
int main()
{
int n = 4;
cout << find_Numb_ways(n) << endl;
return 0;
}
// This code is contributed by kondamrohan02.
Java
// Java program to Count of numbers of length
// N having prime numbers at odd indices and
// odd numbers at even indices
import java.util.*;
class GFG
{
// function to find total number of ways
static int find_Numb_ways(int n)
{
// No of odd indices in n-digit number
int odd_indices = n/2;
// No of even indices in n-digit number
int even_indices = (n / 2) + (n % 2);
// No of ways of arranging prime number
// digits in odd indices
int arr_odd = (int)Math.pow(4, odd_indices);
// No of ways of arranging odd number
// digits in even indices
int arr_even = (int)Math.pow(5, even_indices);
// returning the total number of ways
return arr_odd * arr_even;
}
// Driver Code
public static void main(String[] args) {
int n = 4;
System.out.print(find_Numb_ways(n));
}
}
// This code is contributed by code_hunt.
Python3
# python program for above approach
def count(N):
# No of odd indices in N-digit number
odd_indices = N//2
# No of even indices in N-digit number
even_indices = N//2 + N % 2
# No of ways of arranging prime number
# digits in odd indices
arrange_odd = 4 ** odd_indices
# No of ways of arranging odd number
# digits in even indices
arrange_even = 5 ** even_indices
# returning the total number of ways
return arrange_odd * arrange_even
# Driver code
if __name__ == "__main__":
N = 4
# calling the function
print(count(N))
C#
// C# program to Count of numbers of length
// N having prime numbers at odd indices and
// odd numbers at even indices
using System;
using System.Collections.Generic;
class GFG{
// function to find total number of ways
static int find_Numb_ways(int n)
{
// No of odd indices in n-digit number
int odd_indices = n/2;
// No of even indices in n-digit number
int even_indices = (n / 2) + (n % 2);
// No of ways of arranging prime number
// digits in odd indices
int arr_odd = (int)Math.Pow(4, odd_indices);
// No of ways of arranging odd number
// digits in even indices
int arr_even = (int)Math.Pow(5, even_indices);
// returning the total number of ways
return arr_odd * arr_even;
}
// drive code
public static void Main()
{
int n = 4;
Console.Write(find_Numb_ways(n));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
400
时间复杂度:O(1),(恒定时间操作)
辅助空间:O(1),(不需要额外空间)