给定两个整数N ans K ,任务是找到前N个自然数的模K的和,即1%K + 2%K +….. + N%K。
例子 :
Input : N = 10 and K = 2.
Output : 5
Sum = 1%2 + 2%2 + 3%2 + 4%2 + 5%2 + 6%2 +
7%2 + 8%2 + 9%2 + 10%2
= 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0
= 5.
方法1:
将变量i从1迭代到N,求值并加上i%K。
以下是此方法的实现:
C++
// C++ program to find sum of
// modulo K of first N natural numbers.
#include
using namespace std;
// Return sum of modulo K of
// first N natural numbers.
int findSum(int N, int K)
{
int ans = 0;
// Iterate from 1 to N &&
// evaluating and adding i % K.
for (int i = 1; i <= N; i++)
ans += (i % K);
return ans;
}
// Driver Program
int main()
{
int N = 10, K = 2;
cout << findSum(N, K) << endl;
return 0;
}
Java
// Java program to find sum of modulo
// K of first N natural numbers.
import java.io.*;
class GFG {
// Return sum of modulo K of
// first N natural numbers.
static int findSum(int N, int K)
{
int ans = 0;
// Iterate from 1 to N && evaluating
// and adding i % K.
for (int i = 1; i <= N; i++)
ans += (i % K);
return ans;
}
// Driver program
static public void main(String[] args)
{
int N = 10, K = 2;
System.out.println(findSum(N, K));
}
}
// This code is contributed by vt_m.
Python3
# Python3 program to find sum
# of modulo K of first N
# natural numbers.
# Return sum of modulo K of
# first N natural numbers.
def findSum(N, K):
ans = 0;
# Iterate from 1 to N &&
# evaluating and adding i % K.
for i in range(1, N + 1):
ans += (i % K);
return ans;
# Driver Code
N = 10;
K = 2;
print(findSum(N, K));
# This code is contributed by mits
C#
// C# program to find sum of modulo
// K of first N natural numbers.
using System;
class GFG {
// Return sum of modulo K of
// first N natural numbers.
static int findSum(int N, int K)
{
int ans = 0;
// Iterate from 1 to N && evaluating
// and adding i % K.
for (int i = 1; i <= N; i++)
ans += (i % K);
return ans;
}
// Driver program
static public void Main()
{
int N = 10, K = 2;
Console.WriteLine(findSum(N, K));
}
}
// This code is contributed by vt_m.
PHP
Javascript
C++
// C++ program to find sum of modulo
// K of first N natural numbers.
#include
using namespace std;
// Return sum of modulo K of
// first N natural numbers.
int findSum(int N, int K)
{
int ans = 0;
// Counting the number of times 1, 2, ..,
// K-1, 0 sequence occurs.
int y = N / K;
// Finding the number of elements left which
// are incomplete of sequence Leads to Case 1 type.
int x = N % K;
// adding multiplication of number of
// times 1, 2, .., K-1, 0 sequence occurs
// and sum of first k natural number and sequence
// from case 1.
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
// Driver program
int main()
{
int N = 10, K = 2;
cout << findSum(N, K) << endl;
return 0;
}
Java
// Java program to find sum of modulo
// K of first N natural numbers.
import java.io.*;
class GFG {
// Return sum of modulo K of
// first N natural numbers.
static int findSum(int N, int K)
{
int ans = 0;
// Counting the number of times 1, 2, ..,
// K-1, 0 sequence occurs.
int y = N / K;
// Finding the number of elements left which
// are incomplete of sequence Leads to Case 1 type.
int x = N % K;
// adding multiplication of number of times
// 1, 2, .., K-1, 0 sequence occurs and sum
// of first k natural number and sequence
// from case 1.
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
// Driver program
static public void main(String[] args)
{
int N = 10, K = 2;
System.out.println(findSum(N, K));
}
}
// This Code is contributed by vt_m.
Python3
# Python3 program to find sum of modulo
# K of first N natural numbers.
# Return sum of modulo K of
# first N natural numbers.
def findSum(N, K):
ans = 0;
# Counting the number of times
# 1, 2, .., K-1, 0 sequence occurs.
y = N / K;
# Finding the number of elements
# left which are incomplete of
# sequence Leads to Case 1 type.
x = N % K;
# adding multiplication of number
# of times 1, 2, .., K-1, 0
# sequence occurs and sum of
# first k natural number and
# sequence from case 1.
ans = ((K * (K - 1) / 2) * y +
(x * (x + 1)) / 2);
return int(ans);
# Driver Code
N = 10;
K = 2;
print(findSum(N, K));
# This code is contributed by mits
C#
// C# program to find sum of modulo
// K of first N natural numbers.
using System;
class GFG {
// Return sum of modulo K of
// first N natural numbers.
static int findSum(int N, int K)
{
int ans = 0;
// Counting the number of times 1, 2, ..,
// K-1, 0 sequence occurs.
int y = N / K;
// Finding the number of elements left which
// are incomplete of sequence Leads to Case 1 type.
int x = N % K;
// adding multiplication of number of times
// 1, 2, .., K-1, 0 sequence occurs and sum
// of first k natural number and sequence
// from case 1.
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
// Driver program
static public void Main()
{
int N = 10, K = 2;
Console.WriteLine(findSum(N, K));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
5
时间复杂度: O(N)。
方法2:
此方法出现两种情况。
情况1:当N
情况2:当N> = K时,自然数序列中从1到K的整数将对模K进行运算,结果将产生1,2,3,….. K – 1,0。 1至2K,将产生相同的结果。因此,想法是计算该序列出现的次数,然后将其乘以第一个K – 1个自然数的总和。
以下是此方法的实现:
C++
// C++ program to find sum of modulo
// K of first N natural numbers.
#include
using namespace std;
// Return sum of modulo K of
// first N natural numbers.
int findSum(int N, int K)
{
int ans = 0;
// Counting the number of times 1, 2, ..,
// K-1, 0 sequence occurs.
int y = N / K;
// Finding the number of elements left which
// are incomplete of sequence Leads to Case 1 type.
int x = N % K;
// adding multiplication of number of
// times 1, 2, .., K-1, 0 sequence occurs
// and sum of first k natural number and sequence
// from case 1.
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
// Driver program
int main()
{
int N = 10, K = 2;
cout << findSum(N, K) << endl;
return 0;
}
Java
// Java program to find sum of modulo
// K of first N natural numbers.
import java.io.*;
class GFG {
// Return sum of modulo K of
// first N natural numbers.
static int findSum(int N, int K)
{
int ans = 0;
// Counting the number of times 1, 2, ..,
// K-1, 0 sequence occurs.
int y = N / K;
// Finding the number of elements left which
// are incomplete of sequence Leads to Case 1 type.
int x = N % K;
// adding multiplication of number of times
// 1, 2, .., K-1, 0 sequence occurs and sum
// of first k natural number and sequence
// from case 1.
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
// Driver program
static public void main(String[] args)
{
int N = 10, K = 2;
System.out.println(findSum(N, K));
}
}
// This Code is contributed by vt_m.
Python3
# Python3 program to find sum of modulo
# K of first N natural numbers.
# Return sum of modulo K of
# first N natural numbers.
def findSum(N, K):
ans = 0;
# Counting the number of times
# 1, 2, .., K-1, 0 sequence occurs.
y = N / K;
# Finding the number of elements
# left which are incomplete of
# sequence Leads to Case 1 type.
x = N % K;
# adding multiplication of number
# of times 1, 2, .., K-1, 0
# sequence occurs and sum of
# first k natural number and
# sequence from case 1.
ans = ((K * (K - 1) / 2) * y +
(x * (x + 1)) / 2);
return int(ans);
# Driver Code
N = 10;
K = 2;
print(findSum(N, K));
# This code is contributed by mits
C#
// C# program to find sum of modulo
// K of first N natural numbers.
using System;
class GFG {
// Return sum of modulo K of
// first N natural numbers.
static int findSum(int N, int K)
{
int ans = 0;
// Counting the number of times 1, 2, ..,
// K-1, 0 sequence occurs.
int y = N / K;
// Finding the number of elements left which
// are incomplete of sequence Leads to Case 1 type.
int x = N % K;
// adding multiplication of number of times
// 1, 2, .., K-1, 0 sequence occurs and sum
// of first k natural number and sequence
// from case 1.
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
// Driver program
static public void Main()
{
int N = 10, K = 2;
Console.WriteLine(findSum(N, K));
}
}
// This code is contributed by vt_m.
的PHP
Java脚本
输出 :
5
时间复杂度: O(1)。