给定大数的前两位数字digit1和digit2 。还给出了数字c和实际大号的长度。使用公式digit [i] =(digit [i – 1] * c + digit [i – 2])%10计算大数的后n-2个数字。任务是检查形成的数字是否可被41整除。
例子:
Input: digit1 = 1 , digit2 = 2 , c = 1 , n = 3
Output: YES
The number formed is 123
which is divisible by 41
Input: digit1 = 1 , digit2 = 4 , c = 6 , n = 3
Output: NO
天真的方法是使用给定的公式来形成数字。使用%运算符检查形成的数字是否可被41整除。但是,由于数量很大,因此无法存储这么大的数量。
高效的方法:所有数字都是使用给定的公式计算的,然后使用乘法和加法的关联属性来检查数字是否可被41整除。一个数字可以被41整除或不等于(数字%41)等于0或不等于0。
令X为如此形成的大数,可以写成。
X = (digit[0] * 10^n-1) + (digit[1] * 10^n-2) + … + (digit[n-1] * 10^0)
X = ((((digit[0] * 10 + digit[1]) * 10 + digit[2]) * 10 + digit[3]) … ) * 10 + digit[n-1]
X % 41 = ((((((((digit[0] * 10 + digit[1]) % 41) * 10 + digit[2]) % 41) * 10 + digit[3]) % 41) … ) * 10 + digit[n-1]) % 41
因此,在计算完所有数字之后,将遵循以下算法:
- 将第一个数字初始化为ans 。
- 迭代所有n-1个数字。
- 使用关联属性,在每第i步以(ans * 10 + digit [i])%41计算ans。
- 检查ans的最终值(如果不能被41 r整除)。
下面是上述方法的实现。
C++
// C++ program to check a large number
// divisible by 41 or not
#include
using namespace std;
// Check if a number is divisible by 41 or not
bool DivisibleBy41(int first, int second, int c, int n)
{
// array to store all the digits
int digit[n];
// base values
digit[0] = first;
digit[1] = second;
// calculate remaining digits
for (int i = 2; i < n; i++)
digit[i] = (digit[i - 1] * c + digit[i - 2]) % 10;
// calculate answer
int ans = digit[0];
for (int i = 1; i < n; i++)
ans = (ans * 10 + digit[i]) % 41;
// check for divisibility
if (ans % 41 == 0)
return true;
else
return false;
}
// Driver Code
int main()
{
int first = 1, second = 2, c = 1, n = 3;
if (DivisibleBy41(first, second, c, n))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Java program to check
// a large number divisible
// by 41 or not
import java.io.*;
class GFG
{
// Check if a number is
// divisible by 41 or not
static boolean DivisibleBy41(int first,
int second,
int c, int n)
{
// array to store
// all the digits
int digit[] = new int[n];
// base values
digit[0] = first;
digit[1] = second;
// calculate remaining
// digits
for (int i = 2; i < n; i++)
digit[i] = (digit[i - 1] * c +
digit[i - 2]) % 10;
// calculate answer
int ans = digit[0];
for (int i = 1; i < n; i++)
ans = (ans * 10 +
digit[i]) % 41;
// check for
// divisibility
if (ans % 41 == 0)
return true;
else
return false;
}
// Driver Code
public static void main (String[] args)
{
int first = 1, second = 2, c = 1, n = 3;
if (DivisibleBy41(first, second, c, n))
System.out.println("YES");
else
System.out.println("NO");
}
}
// This code is contributed
// by akt_mit
Python3
# Python3 program to check
# a large number divisible
# by 41 or not
# Check if a number is
# divisible by 41 or not
def DivisibleBy41(first,
second, c, n):
# array to store
# all the digits
digit = [0] * n
# base values
digit[0] = first
digit[1] = second
# calculate remaining
# digits
for i in range(2,n):
digit[i] = (digit[i - 1] * c +
digit[i - 2]) % 10
# calculate answer
ans = digit[0]
for i in range(1,n):
ans = (ans * 10 + digit[i]) % 41
# check for
# divisibility
if (ans % 41 == 0):
return True
else:
return False
# Driver Code
first = 1
second = 2
c = 1
n = 3
if (DivisibleBy41(first,
second, c, n)):
print("YES")
else:
print("NO")
# This code is contributed
# by Smita
C#
// C# program to check
// a large number divisible
// by 41 or not
using System;
class GFG
{
// Check if a number is
// divisible by 41 or not
static bool DivisibleBy41(int first,
int second,
int c, int n)
{
// array to store
// all the digits
int []digit = new int[n];
// base values
digit[0] = first;
digit[1] = second;
// calculate
// remaining
// digits
for (int i = 2; i < n; i++)
digit[i] = (digit[i - 1] * c +
digit[i - 2]) % 10;
// calculate answer
int ans = digit[0];
for (int i = 1; i < n; i++)
ans = (ans * 10 +
digit[i]) % 41;
// check for
// divisibility
if (ans % 41 == 0)
return true;
else
return false;
}
// Driver Code
public static void Main ()
{
int first = 1,
second = 2,
c = 1, n = 3;
if (DivisibleBy41(first, second, c, n))
Console.Write("YES");
else
Console.Write("NO");
}
}
// This code is contributed
// by Smita
PHP
Javascript
输出:
YES
时间复杂度: O(N)
辅助空间: O(N)