📌  相关文章
📜  在AP中查找第一个元素,该元素是给定素数的倍数

📅  最后修改于: 2021-04-26 18:42:08             🧑  作者: Mango

给定算术级数的第一项(A)和共同差(D),以及质数(P)。任务是找到给定AP中第一个元素的位置,该位置是给定素数P的倍数。

例子

方法:

设项为A N。所以,

AN = (A + (N-1)*D)

现在,假设A N是P的倍数。然后,

A + (N-1)*D = k*P

Where, k is a constant.

现在让A为(A%P),D为(D%P)。因此,我们有(N-1)* D =(k * P – A)。

在RHS上加上和减去P,我们得到:

(N-1)*D = P(k-1) + (P-A), 

Where P-A is a non-negative number 
(since A is replaced by A%P which is less than P)

最终双方都接受mod:

((N-1)*D)%P = (P-A)%P
 or, ((N-1)D)%P = P-A

让我们找到一个X

因此答案N为:

((X*(P-A)) % P) + 1. 

下面是上述方法的实现:

C++
#include 
using namespace std;
  
// Iterative Function to calculate 
// (x^y)%p in O(log y) */
int power(int x, int y, int p)
{
    // Initialize result
    int res = 1; 
  
    // Update x if it is more than or
    // equal to p
    x = x % p; 
  
    while (y > 0) {
  
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
  
    return res;
}
  
// function to find nearest element in common
int NearestElement(int A, int D, int P)
{
    // base conditions
    if (A == 0)
        return 0;
  
    else if (D == 0)
        return -1;
  
    else {
        int X = power(D, P - 2, P);
        return (X * (P - A)) % P;
    }
}
  
// Driver code
int main()
{
    int A = 4, D = 9, P = 11;
  
    // module both A and D
    A %= P;
    D %= P;
  
    // function call
    cout << NearestElement(A, D, P);
  
    return 0;
}


Java
// Java Program to Find First
// element in AP which is 
// multiple of given prime
class GFG
{
// Iterative Function to 
// calculate (x^y)%p in
// O(log y) */
static int power(int x, 
                 int y, int p)
{
    // Initialize result
    int res = 1; 
  
    // Update x if it is 
    // more than or equal to p
    x = x % p; 
  
    while (y > 0)
    {
  
        // If y is odd, multiply
        // x with result
        if ((y & 1) != 0)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
  
    return res;
}
  
// function to find nearest
// element in common
static int NearestElement(int A, 
                          int D, int P)
{
    // base conditions
    if (A == 0)
        return 0;
  
    else if (D == 0)
        return -1;
  
    else
    {
        int X = power(D, P - 2, P);
        return (X * (P - A)) % P;
    }
}
  
// Driver code
public static void main(String args[])
{
    int A = 4, D = 9, P = 11;
  
    // module both A and D
    A %= P;
    D %= P;
  
    // function call
    System.out.println(NearestElement(A, D, P));
}
}
  
// This code is contributed 
// by Arnab Kundu


Python 3
# Python 3 Program to Find First 
# element in AP which is  
# multiple of given prime
  
# Iterative Function to calculate  
# (x^y)%p in O(log y) 
def power(x, y, p) :
  
    # Initialize result
    res = 1
  
    # Update x if it is more than or 
    # equal to p 
    x = x % p
  
    while y > 0 :
  
        # If y is odd, multiply x with result 
        if y & 1 :
            res = (res * x) % p
  
        # y must be even now
        #  y = y/2 
        y = y >> 1
        x = (x * x) % p
  
    return res
  
# function to find nearest element in common
def NearestElement(A, D, P) :
  
    # base conditions 
    if A == 0 :
        return 0
  
    elif D == 0 :
        return -1
  
    else :
        X = power(D, P - 2, P)
        return (X * (P - A)) % P
      
# Driver Code
if __name__ == "__main__" :
      
    A, D, P = 4, 9, 11
  
    # module both A and D 
    A %= P
    D %= P
  
    # function call
    print(NearestElement(A, D, P))
      
# This code is contributed by ANKITRAI1


C#
// C# Program to Find First
// element in AP which is 
// multiple of given prime
using System;
  
class GFG
{
// Iterative Function to 
// calculate (x^y)%p in
// O(log y) */
static int power(int x, 
                 int y, int p)
{
    // Initialize result
    int res = 1; 
  
    // Update x if it is 
    // more than or equal to p
    x = x % p; 
  
    while (y > 0)
    {
  
        // If y is odd, multiply
        // x with result
        if ((y & 1) != 0)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
  
    return res;
}
  
// function to find nearest
// element in common
static int NearestElement(int A, 
                          int D, int P)
{
    // base conditions
    if (A == 0)
        return 0;
  
    else if (D == 0)
        return -1;
  
    else
    {
        int X = power(D, P - 2, P);
        return (X * (P - A)) % P;
    }
}
  
// Driver code
public static void Main()
{
    int A = 4, D = 9, P = 11;
  
    // module both A and D
    A %= P;
    D %= P;
  
    // function call
    Console.WriteLine(NearestElement(A, D, P));
}
}
  
// This code is contributed 
// by chandan_jnu.


PHP
 0) 
    {
  
        // If y is odd, multiply
        // x with result
        if ($y & 1)
            $res = ($res * $x) %$p;
  
        // y must be even now
        $y = $y >> 1; // y = y/2
        $x = ($x * $x) %$p;
    }
  
    return $res;
}
  
// function to find nearest
// element in common
function NearestElement($A, $D, $P)
{
    // base conditions
    if ($A == 0)
        return 0;
  
    else if ($D == 0)
        return -1;
  
    else 
    {
        $X = power($D, $P - 2, $P);
        return ($X * ($P - $A)) %$P;
    }
}
  
// Driver code
$A = 4; $D = 9; $P = 11;
  
// module both A and D
$A %= $P;
$D %= $P;
  
// function call
echo NearestElement($A, $D, $P);
  
// This code is contributed 
// by chandan_jnu.
?>


输出:
2