找到 K 的所有可能值,使得从 K 开始的前 N 个数字的总和为 G
给定一个正整数G ,任务是找到K的值的个数,使得从K开始的前N个数字的总和为G ,即(K + (K + 1) + … + (K + N – 1) )) = G ,其中N可以是任何正整数。
例子:
Input: G = 10
Output: 2
Explanation:
Following are the possible values of K:
- K = 1 and N = 4: The sum of {1, 2, 3, 4} is 10(= G).
- K = 10 and N = 1: The sum of {10} is 10(= G).
Therefore, there are two possible values of K. Hence, print 2.
Input: G = 15
Output: 2
朴素方法:解决给定问题的最简单方法是检查从1到G的每个K值,如果满足给定条件,则计算K的这个值。检查所有可能的K值后,打印总计数。
时间复杂度: O(G 2 )
辅助空间: O(1)
有效方法:上述方法可以通过观察任何K值的数学关系来解决:
=> (K) + (K + 1) + (K + 2) + … + (K + N – 1) = G
=> N*K + (1 + 2 + 3 + 4+ … + N – 1) = G
=> G = N*K + (N*(N – 1))/2
因此,值K = G/N – (N – 1)/2 。从上述关系可以得出, K的可能值存在当且仅当:
- N是G的因数,即(G % N) == 0 。
- N应该是奇数,即(N % 2) == 1 。
请按照以下步骤解决问题:
- 初始化变量,例如count为0 ,它存储K值的结果计数。
- 使用变量i迭代范围[1, √G]并执行以下任务:
- 如果g%i等于0 ,那么如果i不等于g/i ,那么如果i%2等于1 ,则将count的值加1并且如果(g/i)%2等于1 ,然后将count的值加1 。
- 否则,如果i%2等于1 ,则将count的值加1 。
- 完成上述步骤后,打印计数的值作为结果。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the count the value
// of K such that sum of the first N
// numbers from K is G
void findValuesOfK(int g)
{
// Stores the total count of K
int count = 0;
// Iterate till square root of g
for (int i = 1; i * i <= g; i++) {
// If the number is factor of g
if (g % i == 0) {
// If the second factor is
// not equal to first factor
if (i != g / i) {
// Check if two factors
// are odd or not
if (i & 1) {
count++;
}
if ((g / i) & 1) {
count++;
}
}
// If second factor is the
// same as the first factor
// then check if the first
// factor is odd or not
else if (i & 1) {
count++;
}
}
}
// Print the resultant count
cout << count;
}
// Driver Code
int main()
{
int G = 125;
findValuesOfK(G);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the count the value
// of K such that sum of the first N
// numbers from K is G
static void findValuesOfK(int g)
{
// Stores the total count of K
int count = 0;
// Iterate till square root of g
for (int i = 1; i * i <= g; i++) {
// If the number is factor of g
if (g % i == 0) {
// If the second factor is
// not equal to first factor
if (i != g / i) {
// Check if two factors
// are odd or not
if (i % 2 == 1) {
count++;
}
if ((g / i) % 2 == 1) {
count++;
}
}
// If second factor is the
// same as the first factor
// then check if the first
// factor is odd or not
else if (i % 2 == 1) {
count++;
}
}
}
// Print the resultant count
System.out.println(count);
}
// Driver code
public static void main(String[] args)
{
int G = 125;
findValuesOfK(G);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 program for the above approach
from math import sqrt
# Function to find the count the value
# of K such that sum of the first N
# numbers from K is G
def findValuesOfK(g):
# Stores the total count of K
count = 0
# Iterate till square root of g
for i in range(1,int(sqrt(g)) + 1, 1):
# If the number is factor of g
if (g % i == 0):
# If the second factor is
# not equal to first factor
if (i != g // i):
# Check if two factors
# are odd or not
if (i & 1):
count += 1
if ((g // i) & 1):
count += 1
# If second factor is the
# same as the first factor
# then check if the first
# factor is odd or not
elif (i & 1):
count += 1
# Print the resultant count
print(count)
# Driver Code
if __name__ == '__main__':
G = 125
findValuesOfK(G)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the count the value
// of K such that sum of the first N
// numbers from K is G
static void findValuesOfK(int g)
{
// Stores the total count of K
int count = 0;
// Iterate till square root of g
for(int i = 1; i * i <= g; i++)
{
// If the number is factor of g
if (g % i == 0)
{
// If the second factor is
// not equal to first factor
if (i != g / i)
{
// Check if two factors
// are odd or not
if (i % 2 == 1)
{
count++;
}
if ((g / i) % 2 == 1)
{
count++;
}
}
// If second factor is the
// same as the first factor
// then check if the first
// factor is odd or not
else if (i % 2 == 1)
{
count++;
}
}
}
// Print the resultant count
Console.WriteLine(count);
}
// Driver code
public static void Main()
{
int G = 125;
findValuesOfK(G);
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
4
时间复杂度: O(√G)
辅助空间: O(1)