두 개의 정수가 주어지면 N 연령 케이 작업은 처음 N 자연수, 즉 1%K + 2%K + ..... + N%K의 모듈로 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.Recommended Practice 역코딩 시도해 보세요!
방법 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 // PHP program to find sum // of modulo K of first N // natural numbers. // Return sum of modulo K of // first N natural numbers. function findSum($N $K) { $ans = 0; // Iterate from 1 to N && // evaluating and adding i % K. for ($i = 1; $i <= $N; $i++) $ans += ($i % $K); return $ans; } // Driver Code $N = 10; $K = 2; echo findSum($N $K) 'n'; // This code is contributed by ajit ?> JavaScript <script> // JavaScript program to find sum // of modulo K of first N natural // numbers. // Return sum of modulo K of // first N natural numbers. function findSum(N K) { let ans = 0; // Iterate from 1 to N && evaluating // and adding i % K. for(let i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver Code let N = 10 K = 2; document.write(findSum(N K)); // This code is contributed by code_hunt </script>
출력 :
5
시간 복잡도: 에).
보조 공간: 오(1)
방법 2:
이 방법에서는 두 가지 경우가 발생합니다.
사례 1: 언제 N< K 각 숫자 i N >= i >= 1에 대해 모듈로 K로 연산할 때 결과로 i를 제공합니다. 따라서 필요한 합계는 첫 번째 N 자연수 N*(N+1)/2의 합계가 됩니다.
사례 2: 언제 N >= K 그런 다음 자연수 시퀀스에서 1부터 K까지의 정수는 모듈로 K로 작동할 때 결과로 1 2 3 ..... K - 1 0을 생성합니다. 마찬가지로 K + 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 // PHP program to find sum of modulo // K of first N natural numbers. // Return sum of modulo K of // first N natural numbers. function 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 $ans; } // Driver program $N = 10; $K = 2; echo findSum($N $K) ; // This code is contributed by anuj_67. ?> JavaScript <script> // Javascript program to find sum of modulo // K of first N natural numbers. // Return sum of modulo K of // first N natural numbers. function findSum(N K) { let ans = 0; // Counting the number of times // 1 2 .. K-1 0 sequence occurs. let y = N / K; // Finding the number of elements // left which are incomplete of // sequence Leads to Case 1 type. let 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 code let N = 10; let K = 2; document.write(findSum(N K)); // This code is contributed by _saurabh_jaiswal </script>
출력 :
5
시간 복잡도: 오(1).
보조 공간: 오(1)