📜  重复单位可除性

📅  最后修改于: 2021-04-24 22:24:35             🧑  作者: Mango

一个包含完全重复次数的数字被称为重复单元。我们将定义长度为k的重复单元数的R(k)。例如, R(6)= 111111。
对于给定的数n,一个正整数且GCD(n,10)= 1,存在一个值k,使得R(k)可被n整除。现在,让A(n)等于k的最小值,即A(n)= k。
因此,对于给定的n值,我们必须找到k(重复一次被n除以n)的A(n)值最小。
例子:

Input : n = 7
Output : A(7) = 6
A(7) = 6 means 6 times one i.e. (111111) is divided by 7.

Input : n = 41
Output : A(41) = 5
A(41) = 5 means 5 times one i.e. (11111) is divided by 41.

首先,如果给定的数字应与10互质数,否则返回0。
如果给定数与10互质,那么我们必须找到最小数k ,以使R(k)= 0 mod n
考虑重复单元R(1),R(2),R(3),R(4)等。对于每个重复的单元,R(j)在计算R(j)的余数时除以n。有n个可以想象的余数。我们得到R(i),R(j)(其中i

C++
// CPP program to find least value of
// k for which R(k) is divisible by n
#include 
using namespace std;
 
// To find least value of k
int repUnitValue(int n)
{
 
    // To check n is coprime or not
    if (n % 2 == 0 || n % 5 == 0)
        return 0;
 
    // to store R(k) mod n and 10^k
    // mod n value
    int rem = 1;
    int power = 1;
    int k = 1;
    while (rem % n != 0)
    {
        k++;
        power = power * 10 % n;
        rem = (rem + power) % n;
    }
     
    return k;
}
 
// Driver code
int main()
{
    int n = 13;
    cout << repUnitValue(n);
    return 0;
}


Java
// Java program to find least value of
// k for which R(k) is divisible by n
import java.util.*;
 
class GFG {
 
    // To find least value of k
    public static int repUnitValue(int n)
    {
         
        // To check n is coprime or not
        if (n % 2 == 0 || n % 5 == 0)
            return 0;
 
        // to store the R(k) mod n and
        // 10^k mod n value
        int rem = 1;
        int power = 1;
        int k = 1;
        while (rem % n != 0)
        {
            k++;
            power = power * 10 % n;
            rem = (rem + power) % n;
        }
         
        return k;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 13;
        System.out.println(repUnitValue(n));
    }
}


Python3
# python program to find least value of
# k for which R(k) is divisible by n
 
# To find least value of k
def repUnitValue(n):
     
    # To check n is coprime or not
    if (n % 2 == 0 or n % 5 == 0):
        return 0
 
    # to store R(k) mod n and 10^k
    # mod n value
    rem = 1
    power = 1
    k = 1
    while (rem % n != 0):
         
        k += 1
        power = power * 10 % n
        rem = (rem + power) % n
     
    return k
     
# Driver code
n = 13
print(repUnitValue(n))
 
# This code is contributed by Sam007.


C#
// C# program to find least value of
// k for which R(k) is divisible by n
using System;
 
public class GFG {
     
    // To find least value of k
    public static int repUnitValue(int n)
    {
         
        // To check n is coprime or not
        if (n % 2 == 0 || n % 5 == 0)
            return 0;
 
        // to store the R(k) mod n and
        // 10^k mod n value
        int rem = 1;
        int power = 1;
        int k = 1;
        while (rem % n != 0)
        {
            k++;
            power = power * 10 % n;
            rem = (rem + power) % n;
        }
         
        return k;
    }
 
    public static void Main()
    {
        int n = 13;
        Console.Write(repUnitValue(n));
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出:
6