给定两个数字N和K ,任务是找到不能被N整除的前K个数字的总和。
例子:
Input: N = 5, K = 10
Output: 63
Explanation: Sum of { 1, 2, 3, 4, 6, 7, 8, 9, 11, 12 } is 63.
Input: N = 3, k = 13
Output: 127
Explanation: Sum of { 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19 } is 127.
方法:为了解决此问题,我们需要遵循以下步骤:
- 用(K /(N – 1))* N计算N的最后一个倍数
- 计算值K%(N – 1) 。如果余数为0,则(K /(N – 1))* N – 1给出不能被N整除的最后一个值。否则,我们需要将余数加到(K /(N – 1))* N 。
- 计算总和,直到最后一个值,该最后一个值不能被上述步骤中获得的N整除。
- 计算N的倍数之和,然后从上一步中计算出的总和中减去以获得所需的结果。
下面的代码是上述方法的实现:
C++
// C++ Program to calculate
// the sum of first K
// numbers not divisible by N
#include
using namespace std;
// Function to find the sum
int findSum(int n, int k)
{
// Find the last multiple of N
int val = (k / (n - 1)) * n;
int rem = k % (n - 1);
// Find the K-th non-multiple of N
if (rem == 0) {
val = val - 1;
}
else {
val = val + rem;
}
// Calculate the sum of
// all elements from 1 to val
int sum = (val * (val + 1)) / 2;
// Calculate the sum of
// all multiples of N
// between 1 to val
int x = k / (n - 1);
int sum_of_multiples
= (x
* (x + 1) * n)
/ 2;
sum -= sum_of_multiples;
return sum;
}
// Driver code
int main()
{
int n = 7, k = 13;
cout << findSum(n, k)
<< endl;
}
Java
// Java program to calculate
// the sum of first K numbers
// not divisible by N
import java.util.*;
class GFG {
// Function to find the sum
static int findSum(int n, int k)
{
// Find the last multiple of N
int val = (k / (n - 1)) * n;
int rem = k % (n - 1);
// Find the K-th non-multiple of N
if (rem == 0)
{
val = val - 1;
}
else
{
val = val + rem;
}
// Calculate the sum of
// all elements from 1 to val
int sum = (val * (val + 1)) / 2;
// Calculate the sum of
// all multiples of N
// between 1 to val
int x = k / (n - 1);
int sum_of_multiples = (x * (x + 1) * n) / 2;
sum -= sum_of_multiples;
return sum;
}
// Driver code
public static void main(String[] args)
{
int n = 7, k = 13;
System.out.println(findSum(n, k));
}
}
// This code is contributed by offbeat
Python3
# Python3 Program to calculate
# the sum of first K
# numbers not divisible by N
# Function to find the sum
def findSum(n, k):
# Find the last multiple of N
val = (k // (n - 1)) * n;
rem = k % (n - 1);
# Find the K-th non-multiple of N
if (rem == 0):
val = val - 1;
else:
val = val + rem;
# Calculate the sum of
# all elements from 1 to val
sum = (val * (val + 1)) // 2;
# Calculate the sum of
# all multiples of N
# between 1 to val
x = k // (n - 1);
sum_of_multiples = (x * (x + 1) * n) // 2;
sum -= sum_of_multiples;
return sum;
# Driver code
n = 7; k = 13;
print(findSum(n, k))
# This code is contributed by Code_Mech
C#
// C# program to calculate
// the sum of first K numbers
// not divisible by N
using System;
class GFG{
// Function to find the sum
static int findSum(int n, int k)
{
// Find the last multiple of N
int val = (k / (n - 1)) * n;
int rem = k % (n - 1);
// Find the K-th non-multiple of N
if (rem == 0)
{
val = val - 1;
}
else
{
val = val + rem;
}
// Calculate the sum of
// all elements from 1 to val
int sum = (val * (val + 1)) / 2;
// Calculate the sum of
// all multiples of N
// between 1 to val
int x = k / (n - 1);
int sum_of_multiples = (x * (x + 1) * n) / 2;
sum -= sum_of_multiples;
return sum;
}
// Driver code
public static void Main(String[] args)
{
int n = 7, k = 13;
Console.WriteLine(findSum(n, k));
}
}
// This code is contributed by 29AjayKumar
输出:
99
时间复杂度: O(1)
辅助空间复杂度: O(1)