给定两个整数S和D ,任务是计算和S和公共差D可能的算术级数。
例子:
Input: S = 12, D = 1
Output: 4
Explanation: Following 4 arithmetic progressions with sum 12 and common difference 1 are possible:
- {1, 2}
- {3, 4, 5}
- {-2, -1, 0, 1, 2, 3, 4, 5}
- {-11, -10, -9, …, 10, 11, 12}
Input: S = 1, D = 1
Output: 2
方法:可以根据以下观察结果解决给定问题:
- AP系列的总和由下式给出:
where,
S is the sum of the AP series,
a is the first term of the series,
N is the number of terms in the series,
d is a common difference
- 重新排列以上表达式后:
=> 2*S = N*(2*a + (N – 1)*d) … (1)
=> …(2)
- 从以上两个表达式:
- 这个想法是考虑所有2 * S的因子,并检查是否存在任何因子F ,以使F和(2 * a +(F – 1)* d)的乘积等于2 *S 。如果发现是真的,则对具有给定总和S的可能AP之一计算该因数。
- 如果存在任何因子F ,使得(D * F –(2 * S / F)+ D)可被2整除,则对具有给定总和S的可能AP之一计算该因子。
请按照以下步骤解决问题:
- 初始化一个变量,例如answer ,以存储具有和S和共同差D的AP的计数。
- 迭代[1,√2* S]范围,并检查2 * S是否可被i整除,然后执行以下步骤:
- 如果(((2 * S / i)+ 1 – i * D)的值可被2整除,则将answer增加1 。
- 如果(i * D – S / i + 1)的值可被2整除,则将答案增加1 。
- 完成上述步骤后,将answer的值打印为AP的结果计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of APs
// with sum S and common difference D
int countAPs(int S, int D)
{
// Multiply S by 2
S = S * 2;
// Stores the count of APs
int answer = 0;
// Iterate over the factors of 2*S
for (int i = 1; i <= sqrt(S); i++) {
// Check if i is the factor
// or not
if (S % i == 0) {
// Conditions to check if AP
// can be formed using factor F
if (((S / i) - D * i + D) % 2 == 0)
answer++;
if ((D * i - (S / i) + D) % 2 == 0)
answer++;
}
}
// Return the total count of APs
return answer;
}
// Driver Code
int main()
{
int S = 12, D = 1;
cout << countAPs(S, D);
return 0;
}
Java
// Java program for above approach
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
// Function to count the number of APs
// with sum S and common difference D
static int countAPs(int S, int D)
{
// Multiply S by 2
S = S * 2;
// Stores the count of APs
int answer = 0;
// Iterate over the factors of 2*S
for (int i = 1; i <= Math.sqrt(S); i++) {
// Check if i is the factor
// or not
if (S % i == 0) {
// Conditions to check if AP
// can be formed using factor F
if (((S / i) - D * i + D) % 2 == 0)
answer++;
if ((D * i - (S / i) + D) % 2 == 0)
answer++;
}
}
// Return the total count of APs
return answer;
}
// Driver code
public static void main(String[] args)
{
int S = 12, D = 1;
System.out.println(countAPs(S, D));
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python3 program for the above approach
# Function to count the number of APs
# with sum S and common difference D
def countAPs(S, D):
# Multiply S by 2
S = S * 2
# Stores the count of APs
answer = 0
# Iterate over the factors of 2*S
for i in range(1, S):
if i * i > S:
break
# Check if i is the factor
# or not
if (S % i == 0):
# Conditions to check if AP
# can be formed using factor F
if (((S // i) - D * i + D) % 2 == 0):
answer += 1
if ((D * i - (S // i) + D) % 2 == 0):
answer += 1
# Return the total count of APs
return answer
# Driver Code
if __name__ == '__main__':
S, D = 12, 1
print(countAPs(S, D));
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of APs
// with sum S and common difference D
static int countAPs(int S, int D)
{
// Multiply S by 2
S = S * 2;
// Stores the count of APs
int answer = 0;
// Iterate over the factors of 2*S
for (int i = 1; i <= Math.Sqrt(S); i++) {
// Check if i is the factor
// or not
if (S % i == 0) {
// Conditions to check if AP
// can be formed using factor F
if (((S / i) - D * i + D) % 2 == 0)
answer++;
if ((D * i - (S / i) + D) % 2 == 0)
answer++;
}
}
// Return the total count of APs
return answer;
}
// Driver code
static void Main()
{
int S = 12, D = 1;
Console.Write(countAPs(S, D));
}
}
// This code is contributed by sanjoy_62.
输出:
4
时间复杂度: O(√S)
辅助空间: O(1)