给定两个整数S和D ,任务是通过重复将S模D加到S来检查整数S 是否可以被D整除。如果S可被D整除,则打印“Yes” 。否则,打印“否” 。
例子:
Input: S = 3, D = 6
Output: Yes
Explanation:
Lets say that the value on the ith interval mod D is V(i) i.e., V(i) = (V(i – 1) + V(i – 1) % D) % D then,
V(0) = S % D = 3
V(1) = (V(0) + V(0) % D) % D = (3 + (3%6)) % 6 = 0
So, 6 is divisible by 6. Therefore, print “Yes”.
Input: S = 1, D = 5
Output: No
方法:给定的问题可以通过使用鸽子洞原理来解决。请按照以下步骤解决问题:
- 根据原理,如果有超过D 个数字与D取模,那么([0, D – 1])范围内的值至少会出现两次。
- 迭代(D + 1)次并将V(i)的值存储在 HashMap 中,如果有重复则跳出循环。
- 当余数值为0时存在边缘情况,在这种情况下退出循环以及这是可分的情况,但我们的逻辑将其视为一个循环。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if S is divisible
// by D while changing S to (S + S%D)
string isDivisibleByDivisor(int S, int D)
{
// V(0) = S % D
S %= D;
// Stores the encountered values
unordered_set hashMap;
hashMap.insert(S);
for (int i = 0; i <= D; i++) {
// V(i) = (V(i-1) + V(i-1)%D) % D
S += (S % D);
S %= D;
// Check if the value has
// already been encountered
if (hashMap.find(S)
!= hashMap.end()) {
// Edge Case
if (S == 0) {
return "Yes";
}
return "No";
}
// Otherwise, insert it into
// the hashmap
else
hashMap.insert(S);
}
return "Yes";
}
// Driver Code
int main()
{
int S = 3, D = 6;
cout << isDivisibleByDivisor(S, D);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to check if S is divisible
// by D while changing S to (S + S%D)
static String isDivisibleByDivisor(int S, int D)
{
// V(0) = S % D
S %= D;
// Stores the encountered values
Set hashMap = new HashSet<>();
hashMap.add(S);
for(int i = 0; i <= D; i++)
{
// V(i) = (V(i-1) + V(i-1)%D) % D
S += (S % D);
S %= D;
// Check if the value has
// already been encountered
if (hashMap.contains(S))
{
// Edge Case
if (S == 0)
{
return "Yes";
}
return "No";
}
// Otherwise, insert it into
// the hashmap
else
hashMap.add(S);
}
return "Yes";
}
// Driver code
public static void main(String[] args)
{
int S = 3, D = 6;
System.out.println(isDivisibleByDivisor(S, D));
}
}
// This code is contributed by offbeat
Python3
# Python 3 program for the above approach
# Function to check if S is divisible
# by D while changing S to (S + S%D)
def isDivisibleByDivisor(S, D):
# V(0) = S % D
S %= D
# Stores the encountered values
hashMap = set()
hashMap.add(S)
for i in range(D+1):
# V(i) = (V(i-1) + V(i-1)%D) % D
S += (S % D)
S %= D
# Check if the value has
# already been encountered
if (S in hashMap):
# Edge Case
if (S == 0):
return "Yes"
return "No"
# Otherwise, insert it into
# the hashmap
else:
hashMap.add(S)
return "Yes"
# Driver Code
if __name__ == '__main__':
S = 3
D = 6
print(isDivisibleByDivisor(S, D))
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
// Function to check if S is divisible
// by D while changing S to (S + S%D)
static string isDivisibleByDivisor(int S, int D)
{
// V(0) = S % D
S %= D;
// Stores the encountered values
List hashMap = new List();;
hashMap.Add(S);
for (int i = 0; i <= D; i++) {
// V(i) = (V(i-1) + V(i-1)%D) % D
S += (S % D);
S %= D;
// Check if the value has
// already been encountered
if (hashMap.Contains(S)) {
// Edge Case
if (S == 0) {
return "Yes";
}
return "No";
}
// Otherwise, insert it into
// the hashmap
else
hashMap.Add(S);
}
return "Yes";
}
// Driver Code
static void Main()
{
int S = 3, D = 6;
Console.Write( isDivisibleByDivisor(S, D));
}
}
// This code is contributed by sanjoy_62.
Javascript
输出:
Yes
时间复杂度: O(D)
辅助空间: O(D)