📌  相关文章
📜  检查 N 在除以 K 以内的所有值时是否只留下不同的余数

📅  最后修改于: 2021-09-06 05:31:35             🧑  作者: Mango

给定两个整数NK ,任务是检查N在除以[1, K]范围内的所有整数时是否只留下不同的余数。如果是,请打印Yes 。否则,打印No
例子:

方法:
请按照以下步骤解决问题:

  • 初始化一个集合S
  • 迭代范围[1, K]
  • 在每次迭代中,检查N % i是否已经存在于Set S 中
  • 如果不存在,则将N % i插入集合S
  • 否则,打印No并终止。

下面是上述方法的实现:

C++
// C++ Program to check if all
// remainders are distinct or not
#include 
using namespace std;
 
// Function to check and return
// if all remainders are distinct
bool is_distinct(long long n, long long k)
{
 
    // Stores the remainder
    unordered_set s;
 
    for (int i = 1; i <= k; i++) {
 
        // Calculate the remainder
        long long tmp = n % i;
 
        // If remainder already occurred
        if (s.find(tmp) != s.end()) {
            return false;
        }
 
        // Insert into the set
        s.insert(tmp);
    }
 
    return true;
}
 
// Driver Code
int main()
{
 
    long long N = 5, K = 3;
 
    if (is_distinct(N, K))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program to check if all
// remainders are distinct or not
import java.util.*;
 
class GFG{
 
// Function to check and return
// if all remainders are distinct
static boolean is_distinct(long n, long k)
{
 
    // Stores the remainder
    HashSet s = new HashSet();
 
    for(int i = 1; i <= k; i++)
    {
         
        // Calculate the remainder
        long tmp = n % i;
 
        // If remainder already occurred
        if (s.contains(tmp))
        {
            return false;
        }
 
        // Insert into the set
        s.add(tmp);
    }
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    long N = 5, K = 3;
 
    if (is_distinct(N, K))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program to check if all
# remainders are distinct or not
 
# Function to check and return
# if all remainders are distinct
def is_distinct(n, k):
 
    # Stores the remainder
    s = set()
 
    for i in range(1, k + 1):
 
        # Calculate the remainder
        tmp = n % i
 
        # If remainder already occurred
        if (tmp in s):
            return False
 
        # Insert into the set
        s.add(tmp)
 
    return True
 
# Driver Code
if __name__ == '__main__':
 
    N = 5
    K = 3
     
    if (is_distinct(N, K)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by Shivam Singh


C#
// C# program to check if all
// remainders are distinct or not
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check and return
// if all remainders are distinct
static bool is_distinct(long n, long k)
{
 
    // Stores the remainder
    HashSet s = new HashSet();
 
    for(int i = 1; i <= k; i++)
    {
         
        // Calculate the remainder
        long tmp = n % i;
 
        // If remainder already occurred
        if (s.Contains(tmp))
        {
            return false;
        }
 
        // Insert into the set
        s.Add(tmp);
    }
    return true;
}
 
// Driver Code
public static void Main(String[] args)
{
    long N = 5, K = 3;
 
    if (is_distinct(N, K))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
Yes

时间复杂度: O(K)
辅助空间: O(K)