给定两个整数N和K ,任务是找到数字N K的前M个和后M个数字。
例子:
Input: N = 2345, K = 3, M = 3
Output: 128 625
Explanation:
2345 3 = 12895213625
Therefore, the first M(= 3) digits are 128 and the last three digits are 625.
Input: N = 12, K = 12, M = 4
Output: 8916 8256
Explanation:
12 12 = 8916100448256
天真的方法:
解决该问题的最简单方法是计算N K的值,然后迭代N K mod 10 M到前M个数字,然后找到最后M个数字。
时间复杂度: O(K)
辅助空间: O(1)
高效方法:
可以通过以下观察来优化上述方法:
Let us consider a number x which can be written as 10y Where y is a decimal number.
Let x = NK
NK = 10 y
Taking log10 on both sides of the above expression, we get:
K * log10(N) = log10(10 y)
=> K * log10(N) = y * (log1010)
=> y = K * log10(N)
Now y will be a decimal number of form abc—.xyz—
Therefore,
NK = 10abc—.xyz—
=> NK = 10abc— + 0.xyz—
=> NK = 10abc— * 100.xyz—
In the above equation, 10abc— only moves the decimal point forward.
By calculating 100.xyz—, the first M digits can be figured out by moving the decimal point forward.
请按照以下步骤解决问题:
- 通过计算(N K )mod(10 M )找出N K的前M个数字。
- 计算K * log 10 (N) 。
- 计算10 K * log 10 (N) 。
- 通过计算10 K * log 10 (N) * 10 M – 1来找到前M个数字。
- 打印获得的前M位和后M位。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
#define ll long long int
using namespace std;
// Function to find a^b modulo M
ll modPower(ll a, ll b, ll M)
{
ll res = 1;
while (b) {
if (b & 1)
res = res * a % M;
a = a * a % M;
b >>= 1;
}
return res;
}
// Function to find the first and last
// M digits from N^K
void findFirstAndLastM(ll N, ll K, ll M)
{
// Calculate Last M digits
ll lastM
= modPower(N, K, (1LL) * pow(10, M));
// Calculate First M digits
ll firstM;
double y = (double)K * log10(N * 1.0);
// Extract the number after decimal
y = y - (ll)y;
// Find 10 ^ y
double temp = pow(10.0, y);
// Move the Decimal Point M - 1 digits forward
firstM = temp * (1LL) * pow(10, M - 1);
// Print the result
cout << firstM << " " << lastM << endl;
}
// Driver Code
int main()
{
ll N = 12, K = 12, M = 4;
findFirstAndLastM(N, K, M);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find a^b modulo M
static long modPower(long a, long b, long M)
{
long res = 1;
while (b > 0)
{
if (b % 2 == 1)
res = res * a % M;
a = a * a % M;
b >>= 1;
}
return res;
}
// Function to find the first and last
// M digits from N^K
static void findFirstAndLastM(long N, long K,
long M)
{
// Calculate Last M digits
long lastM = modPower(N, K, (1L) *
(long)Math.pow(10, M));
// Calculate First M digits
long firstM;
double y = (double)K * Math.log10(N * 1.0);
// Extract the number after decimal
y = y - (long)y;
// Find 10 ^ y
double temp = Math.pow(10.0, y);
// Move the Decimal Point M - 1 digits forward
firstM = (long)(temp * (1L) *
Math.pow(10, (M - 1)));
// Print the result
System.out.print(firstM + " " +
lastM + "\n");
}
// Driver Code
public static void main(String[] args)
{
long N = 12, K = 12, M = 4;
findFirstAndLastM(N, K, M);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to implement
# the above approach
from math import *
# Function to find a^b modulo M
def modPower(a, b, M):
res = 1
while (b):
if (b & 1):
res = res * a % M
a = a * a % M
b >>= 1
return res
# Function to find the first and
# last M digits from N^K
def findFirstAndLastM(N, K, M):
# Calculate Last M digits
lastM = modPower(N, K, int(pow(10, M)))
# Calculate First M digits
firstM = 0
y = K * log10(N * 1.0)
# Extract the number after decimal
y = y - int(y)
# Find 10 ^ y
temp = pow(10.0, y)
# Move the Decimal Point M - 1
# digits forward
firstM = int(temp * pow(10, M - 1))
# Print the result
print(firstM, lastM)
# Driver Code
N = 12
K = 12
M = 4
findFirstAndLastM(N, K, M)
# This code is contributed by himanshu77
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find a^b modulo M
static long modPower(long a, long b, long M)
{
long res = 1;
while (b > 0)
{
if (b % 2 == 1)
res = res * a % M;
a = a * a % M;
b >>= 1;
}
return res;
}
// Function to find the first and last
// M digits from N^K
static void findFirstAndLastM(long N, long K,
long M)
{
// Calculate Last M digits
long lastM = modPower(N, K, (1L) *
(long)Math.Pow(10, M));
// Calculate First M digits
long firstM;
double y = (double)K * Math.Log10(N * 1.0);
// Extract the number after decimal
y = y - (long)y;
// Find 10 ^ y
double temp = Math.Pow(10.0, y);
// Move the Decimal Point M - 1 digits forward
firstM = (long)(temp * (1L) *
Math.Pow(10, (M - 1)));
// Print the result
Console.Write(firstM + " " +
lastM + "\n");
}
// Driver Code
public static void Main(String[] args)
{
long N = 12, K = 12, M = 4;
findFirstAndLastM(N, K, M);
}
}
// This code is contributed by gauravrajput1
8916 8256
时间复杂度: O(log K)
辅助空间: O(1)