给定两个整数L和R ,任务是对范围[L,R]中的数字进行计数,该范围分别在奇数位置具有奇数位,在偶数位置具有偶数位。
例子:
Input: L = 3, R = 25
Output: 9
Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18.
Input: L = 128, R = 162
Output: 7
Explanation: The numbers satisfying the conditions are 129, 141, 143, 145, 147, 149 and 161.
方法:可以根据以下观察结果解决给定问题:
It can be observed that every even position has 5 choices {0, 2, 4, 6, 8} and every odd position also has 5 choices {1, 3, 5, 7, 9}. Therefore, there are 5 possibilities for every position. Considering d to be the number of digits in any number satisfying the condition and D to be the number of digits in N, following observations can be made:
- Case 1: If d < D, then the count of total numbers consisting of d digits satisfying the condition is 5d as every number has 5 choices and numbers made will all be less than N.
- Case 2: If D = d, then the count of total numbers satisfying the condition of length d is 5d . But the numbers exceeding N needs to be discarded, which is determined by the following:
- For even places, if the digit in pth place is x, then (5 — (x / 2 + 1)) * 5(D — p) numbers are greater than N.
- For odd places, if the digit in pth place is x, then (5 — (x + 1) / 2) * 5(D — p) numbers will be greater than N.
请按照以下步骤解决问题:
- 定义一个函数countNumberUtill()以对满足条件的[1,N]范围内的数字进行计数,其中N是自然数。
- 初始化整数变量,计数和矢量数字以存储整数N的数字。
- 遍历给定数字的所有数字,即从1到D ,然后执行以下操作:
- 将5 i存储在变量中,例如res 。
- 从p = 1遍历到p = D并执行以下操作:
- 初始化变量x并在其中存储x = digits [p] 。
- 如果p是偶数,则从res减去(5 —(x / 2 +1))* 5 (Dp) 。
- 否则,从res中减去(5 —(x +1)/ 2)* 5 (D — p) 。
- 将res添加到计数中。
- 返回计数。
- 打印countNumberUtill(R)-countNumberUtill(L – 1)作为必需答案。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
#define ll long long
// Function to calculate 5^p
ll getPower(int p)
{
// Stores the result
ll res = 1;
// Multiply 5 p times
while (p--) {
res *= 5;
}
// Return the result
return res;
}
// Function to count all numbers upto N
// having odd digits at odd places and
// even digits at even places
ll countNumbersUtil(ll N)
{
// Stores the count
ll count = 0;
// Stores the digits of N
vector digits;
// Insert the digits of N
while (N) {
digits.push_back(N % 10);
N /= 10;
}
// Reverse the vector to arrange
// the digits from first to last
reverse(digits.begin(), digits.end());
// Stores count of digits of n
int D = digits.size();
for (int i = 1; i <= D; i++) {
// Stores the count of numbers
// with i digits
ll res = getPower(i);
// If the last digit is reached,
// subtract numbers eceeding range
if (i == D) {
// Iterate over all the places
for (int p = 1; p <= D; p++) {
// Stores the digit in the pth place
int x = digits[p - 1];
// Stores the count of numbers
// having a digit greater than x
// in the p-th position
ll tmp = 0;
// Calculate the count of numbers
// exceeding the range if p is even
if (p % 2 == 0) {
tmp = (5 - (x / 2 + 1))
* getPower(D - p);
}
// Calculate the count of numbers
// exceeding the range if p is odd
else {
tmp = (5 - (x + 1) / 2)
* getPower(D - p);
}
// Substract the count of numbers
// exceeding the range from total count
res -= tmp;
// If the parity of p and the
// parity of x are not same
if (p % 2 != x % 2) {
break;
}
}
}
// Add count of numbers having i digits
// and satisfies the given conditions
count += res;
}
// Return the total count of numbers till n
return count;
}
// Function to calculate the count of numbers
// from given range having odd digits places
// and even digits at even places
void countNumbers(ll L, ll R)
{
// Count of numbers in range [L, R] =
// Count of numbers till R -
cout << (countNumbersUtil(R)
// Count of numbers till (L-1)
- countNumbersUtil(L - 1))
<< endl;
}
// Driver Code
int main()
{
ll L = 128, R = 162;
countNumbers(L, R);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
import java.util.Vector;
import java.util.Collections;
class GFG{
// Function to calculate 5^p
static int getPower(int p)
{
// Stores the result
int res = 1;
// Multiply 5 p times
while (p > 0)
{
res *= 5;
p--;
}
// Return the result
return res;
}
// Function to count all numbers upto N
// having odd digits at odd places and
// even digits at even places
static int countNumbersUtil(int N)
{
// Stores the count
int count = 0;
// Stores the digits of N
Vector digits = new Vector();
// Insert the digits of N
while (N > 0)
{
digits.add(N % 10);
N /= 10;
}
// Reverse the vector to arrange
// the digits from first to last
Collections.reverse(digits);
// Stores count of digits of n
int D = digits.size();
for(int i = 1; i <= D; i++)
{
// Stores the count of numbers
// with i digits
int res = getPower(i);
// If the last digit is reached,
// subtract numbers eceeding range
if (i == D)
{
// Iterate over all the places
for(int p = 1; p <= D; p++)
{
// Stores the digit in the pth place
int x = digits.get(p - 1);
// Stores the count of numbers
// having a digit greater than x
// in the p-th position
int tmp = 0;
// Calculate the count of numbers
// exceeding the range if p is even
if (p % 2 == 0)
{
tmp = (5 - (x / 2 + 1)) *
getPower(D - p);
}
// Calculate the count of numbers
// exceeding the range if p is odd
else
{
tmp = (5 - (x + 1) / 2) *
getPower(D - p);
}
// Substract the count of numbers
// exceeding the range from total count
res -= tmp;
// If the parity of p and the
// parity of x are not same
if (p % 2 != x % 2)
{
break;
}
}
}
// Add count of numbers having i digits
// and satisfies the given conditions
count += res;
}
// Return the total count of numbers till n
return count;
}
// Function to calculate the count of numbers
// from given range having odd digits places
// and even digits at even places
static void countNumbers(int L, int R)
{
// Count of numbers in range [L, R] =
// Count of numbers till R -
System.out.println(countNumbersUtil(R) -
// Count of numbers till (L-1)
countNumbersUtil(L - 1));
}
// Driver Code
public static void main(String args[])
{
int L = 128, R = 162;
countNumbers(L, R);
}
}
// This code is contributed by ipg2016107
Python3
# Python3 program to implement
# the above approach
# Function to calculate 5^p
def getPower(p) :
# Stores the result
res = 1
# Multiply 5 p times
while (p) :
res *= 5
p -= 1
# Return the result
return res
# Function to count anumbers upto N
# having odd digits at odd places and
# even digits at even places
def countNumbersUtil(N) :
# Stores the count
count = 0
# Stores the digits of N
digits = []
# Insert the digits of N
while (N) :
digits.append(N % 10)
N //= 10
# Reverse the vector to arrange
# the digits from first to last
digits.reverse()
# Stores count of digits of n
D = len(digits)
for i in range(1, D + 1, 1) :
# Stores the count of numbers
# with i digits
res = getPower(i)
# If the last digit is reached,
# subtract numbers eceeding range
if (i == D) :
# Iterate over athe places
for p in range(1, D + 1, 1) :
# Stores the digit in the pth place
x = digits[p - 1]
# Stores the count of numbers
# having a digit greater than x
# in the p-th position
tmp = 0
# Calculate the count of numbers
# exceeding the range if p is even
if (p % 2 == 0) :
tmp = ((5 - (x // 2 + 1))
* getPower(D - p))
# Calculate the count of numbers
# exceeding the range if p is odd
else :
tmp = ((5 - (x + 1) // 2)
* getPower(D - p))
# Substract the count of numbers
# exceeding the range from total count
res -= tmp
# If the parity of p and the
# parity of x are not same
if (p % 2 != x % 2) :
break
# Add count of numbers having i digits
# and satisfies the given conditions
count += res
# Return the total count of numbers tin
return count
# Function to calculate the count of numbers
# from given range having odd digits places
# and even digits at even places
def countNumbers(L, R) :
# Count of numbers in range [L, R] =
# Count of numbers tiR -
print(countNumbersUtil(R)
# Count of numbers ti(L-1)
- countNumbersUtil(L - 1))
# Driver Code
L = 128
R = 162
countNumbers(L, R)
# This code is contributed by code_hunt
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate 5^p
static int getPower(int p)
{
// Stores the result
int res = 1;
// Multiply 5 p times
while (p > 0)
{
res *= 5;
p--;
}
// Return the result
return res;
}
// Function to count all numbers upto N
// having odd digits at odd places and
// even digits at even places
static int countNumbersUtil(int N)
{
// Stores the count
int count = 0;
// Stores the digits of N
List digits = new List();
// Insert the digits of N
while (N > 0)
{
digits.Add(N % 10);
N /= 10;
}
// Reverse the vector to arrange
// the digits from first to last
digits.Reverse();
// Stores count of digits of n
int D = digits.Count;
for(int i = 1; i <= D; i++)
{
// Stores the count of numbers
// with i digits
int res = getPower(i);
// If the last digit is reached,
// subtract numbers eceeding range
if (i == D)
{
// Iterate over all the places
for(int p = 1; p <= D; p++)
{
// Stores the digit in the pth place
int x = digits[p - 1];
// Stores the count of numbers
// having a digit greater than x
// in the p-th position
int tmp = 0;
// Calculate the count of numbers
// exceeding the range if p is even
if (p % 2 == 0)
{
tmp = (5 - (x / 2 + 1)) *
getPower(D - p);
}
// Calculate the count of numbers
// exceeding the range if p is odd
else
{
tmp = (5 - (x + 1) / 2) *
getPower(D - p);
}
// Substract the count of numbers
// exceeding the range from total count
res -= tmp;
// If the parity of p and the
// parity of x are not same
if (p % 2 != x % 2)
{
break;
}
}
}
// Add count of numbers having i digits
// and satisfies the given conditions
count += res;
}
// Return the total count of numbers till n
return count;
}
// Function to calculate the count of numbers
// from given range having odd digits places
// and even digits at even places
static void countNumbers(int L, int R)
{
// Count of numbers in range [L, R] =
// Count of numbers till R -
Console.WriteLine(countNumbersUtil(R) -
// Count of numbers till (L-1)
countNumbersUtil(L - 1));
}
// Driver Code
public static void Main(String[] args)
{
int L = 128, R = 162;
countNumbers(L, R);
}
}
// This code is contributed by jana_sayantan
7
时间复杂度: O(N 2 ),其中N是R中的位数
辅助空间: O(N)