So, this was a question on one of the problems that I encountered in an online contest, a few days ago.
Question:
Accept the two entrances.
- A large number of digits N ,
- The number of Q questions will be asked .
In each of the questions you need to find whether the number formed by the line between the indices L i and R i will be divided by 7 or not.
Input:
The first line comprises a number consisting of numbers N . The next line contains Q , indicating the number of questions. Each of the following lines Q contains 2 integers L i and R i sub>.
Conclusion:
For each question, type “YES” or “NO” if the number formed by the line between the indices L i and R i is divided by 7.
Limitations:
1 ≤ N ≤ 10 5
1 ≤ Q ≤ 10 5
1 ≤ L i , R i ≤ N
Input Example:
357753
3
1 2
2 3
4 4
Output result:
YES
NO YES
:
35, 7.
: 1,0 .
: 256
: 1024
:
, , , .. N, 10 5 , , .
:
, . , .. O (N).
static String isDivisibleBy(String theIndexedNumber, int divisiblityNo){
int moduloValue = 0;
for(int i = 0; i < theIndexedNumber.length(); i++){
moduloValue = moduloValue * 10;
moduloValue += Character.getNumericValue(theIndexedNumber.charAt(i));
moduloValue %= divisiblityNo;
}
if(moduloValue == 0){
return "YES";
} else{
return "NO";
}
}
Q, 10 5.
, , O (Q.N), . , .
:
, 7. , , , . , . , , , O (Q.N)
- 7,
:
42,341,530 → 530 - 341 = 189 + 42 = 231 → 23 - (1 × 2) = 21
, , 1/3rd Q.N, .
- ? - ?
,