Professional Documents
Culture Documents
Tips Programming Math
Tips Programming Math
,
If m1 and m2 are 2 maps and we want to copy m2
into m1 then,
m1.insert(m2.begin(),m2.end());
Output
floor(ln(number)/ln(base))
Range sum query, range minimum query
Segment Tree Binary Index Tree (BIT)
If n is a number then,
n=p a1 + p a2 + + pak
1
Where
pi
is a prime number.
a
( 1+1) ( a 2+1 ) .( ak +1)
Number of divisor=
Sum of divisor=
(1+ p1 + p ++ pa3 )( 1+ p 2+ p 22+ + p a2 ) .(1+ pk + p2k ++ p ak )
2
1
p 11
p 21
pk 1
1
, ( )
Sort an array keeping track of index
Sort digit wise
Divisor
1
2
5
6
7
Divisibility condition
Examples
No special condition. Any integer
2 is divisible by 1.
is divisible by 1.
The last digit is even (0, 2, 4, 6, or
1,294: 4 is even.
8).[1][2]
405 4 + 0 + 5 = 9 and 636 6 + 3 + 6 = 15
which both are clearly divisible by 3.
Sum the digits. If the result is
16,499,205,854,376
divisible by 3, then the original
1+6+4+9+9+2+0+5+8+5+4+3+7+6 sums to 69 6
number is divisible by 3.[1][3][4]
+ 9 = 15 1 + 5 = 6, which is clearly divisible by
3.
Subtract the quantity of the digits Using the example above: 16,499,205,854,376 has
2, 5, and 8 in the number from the four of the digits 1, 4 and 7 and four of the digits 2,
quantity of the digits 1, 4, and 7 in 5 and 8; Since 4 4 = 0 is a multiple of 3, the
the number.
number 16,499,205,854,376 is divisible by 3.
[1][2]
Examine the last two digits.
40832: 32 is divisible by 4.
If the tens digit is even, the ones
digit must be 0, 4, or 8.
40832: 3 is odd, and the last digit is 2.
If the tens digit is odd, the ones
digit must be 2 or 6.
Twice the tens digit, plus the ones
40832: 2 3 + 2 = 8, which is divisible by 4.
digit.
The last digit is 0 or 5.[1][2]
495: the last digit is 5.
1,458: 1 + 4 + 5 + 8 = 18, so it is divisible by 3 and
[5]
It is divisible by 2 and by 3.
the last digit is even, hence the number is divisible
by 6.
Form the alternating sum of blocks
1,369,851: 851 369 + 1 = 483 = 7 69
of three from right to left.[4][6]
Subtract 2 times the last digit from
the rest. (Works because 21 is
483: 48 (3 2) = 42 = 7 6.
divisible by 7.)
Or, add 5 times the last digit to the
rest. (Works because 49 is divisible 483: 48 + (3 5) = 63 = 7 9.
by 7.)
Or, add 3 times the first digit to the
next. (This works because 10a + b
483: 43 + 8 = 20 remainder 6, 63 + 3 = 21.
7a = 3a + b last number has
the same remainder)
Multiply each digit (from right to 483595: (4 (-2)) + (8 (-3)) + (3 (-1)) + (5 2)
left) by the digit in the
+ (9 3) + (5 1) = 7.
corresponding position in this
pattern (from left to right): 1, 3, 2,
-1, -3, -2 (repeating for digits
9
10
11
12
13
14
15
16
17
18
19
20