📌  相关文章
📜  当N除以从1到N + 1的所有正整数时,找出所有可能的余数

📅  最后修改于: 2021-04-27 19:19:36             🧑  作者: Mango

给定一个大整数N ,任务是找到N除以从1N +1的所有正整数所得的所有可能的余数。

例子:

天真的方法:运行从1N + 1的循环,并返回将N除以范围内的任何整数时发现的所有唯一余数。但是,这种方法对于较大的N值无效。

高效的方法:可以看出,答案的一部分将始终包含介于0ceil(sqrt(n))之间的数字。可以通过对较小的N值运行朴素算法并检查获得的余数,或者通过求解方程ceil(N / k)= xx≤(N / k)来证明这一点,其中x是其中的一个当N被除以k对于k1N + 1的所有整数数k。
解决上述不等式的方法只不过是长度为N / x – N /(x + 1)= N /(x 2 + x)的(N /(x + 1),N / x]的整数k 。从k = 1迭代到ceil(sqrt(N))并存储所有唯一的N%k如果上述k大于ceil(sqrt(N))怎么办?它们将始终对应于值0≤x 。因此,再次开始存储从N /(ceil(sqrt(N))– 10的余数,并返回所有可能的余数的最终答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
typedef long long int ll;
  
// Function to find all the distinct
// remainders when n is divided by
// all the elements from
// the range [1, n + 1]
void findRemainders(ll n)
{
  
    // Set will be used to store
    // the remainders in order
    // to eliminate duplicates
    set vc;
  
    // Find the remainders
    for (ll i = 1; i <= ceil(sqrt(n)); i++)
        vc.insert(n / i);
    for (ll i = n / ceil(sqrt(n)) - 1; i >= 0; i--)
        vc.insert(i);
  
    // Print the contents of the set
    for (auto it : vc)
        cout << it << " ";
}
  
// Driver code
int main()
{
    ll n = 5;
  
    findRemainders(n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to find all the distinct
// remainders when n is divided by
// all the elements from
// the range [1, n + 1]
static void findRemainders(long n)
{
  
    // Set will be used to store
    // the remainders in order
    // to eliminate duplicates
    HashSet vc = new HashSet();
  
    // Find the remainders
    for (long i = 1; i <= Math.ceil(Math.sqrt(n)); i++)
        vc.add(n / i);
    for (long i = (long) (n / Math.ceil(Math.sqrt(n)) - 1); 
                                                i >= 0; i--)
        vc.add(i);
  
    // Print the contents of the set
    for (long it : vc)
        System.out.print(it+ " ");
}
  
// Driver code
public static void main(String[] args)
{
    long n = 5;
  
    findRemainders(n);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
from math import ceil, floor, sqrt
  
# Function to find all the distinct
# remainders when n is divided by
# all the elements from
# the range [1, n + 1]
def findRemainders(n):
  
    # Set will be used to store
    # the remainders in order
    # to eliminate duplicates
    vc = dict()
  
    # Find the remainders
    for i in range(1, ceil(sqrt(n)) + 1):
        vc[n // i] = 1
    for i in range(n // ceil(sqrt(n)) - 1, -1, -1):
        vc[i] = 1
  
    # Print the contents of the set
    for it in sorted(vc):
        print(it, end = " ")
  
# Driver code
n = 5
  
findRemainders(n)
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to find all the distinct
// remainders when n is divided by
// all the elements from
// the range [1, n + 1]
static void findRemainders(long n)
{
  
    // Set will be used to store
    // the remainders in order
    // to eliminate duplicates
    List vc = new List();
  
    // Find the remainders
  
    for (long i = 1; i <= Math.Ceiling(Math.Sqrt(n)); i++)
        vc.Add(n / i);
    for (long i = (long) (n / Math.Ceiling(Math.Sqrt(n)) - 1); 
                                                 i >= 0; i--)
        vc.Add(i);
    vc.Reverse();
      
    // Print the contents of the set
    foreach (long it in vc)
        Console.Write(it + " ");
}
  
// Driver code
public static void Main(String[] args)
{
    long n = 5;
  
    findRemainders(n);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
0 1 2 5