Querying maximum number of divisors that a number in a given range has | A C ++ implementation of the above idea to process queries of finding a number with maximum divisors . ; Finds smallest prime factor of all numbers inrange [ 1 , maxn ) and stores them in smallest_prime [ ] , smallest_prime [ i ] should contain the smallest prime that divides i ; Initialize the smallest_prime factors of all to infinity ; to be built like eratosthenes sieve ; prime number will have its smallest_prime equal to itself ; if ' i ' is the first prime number reaching ' j ' ; number of divisors of n = ( p1 ^ k1 ) * ( p2 ^ k2 ) . . . ( pn ^ kn ) are equal to ( k1 + 1 ) * ( k2 + 1 ) . . . ( kn + 1 ) this function finds the number of divisors of all numbersin range [ 1 , maxn ) and stores it in divisors [ ] divisors [ i ] stores the number of divisors i has ; we can obtain the prime factorization of the number n n = ( p1 ^ k1 ) * ( p2 ^ k2 ) . . . ( pn ^ kn ) using the smallest_prime [ ] array , we keep dividing n by its smallest_prime until it becomes 1 , whilst we check if we have need to set k zero ; use p ^ k , initialize k to 0 ; builds segment tree for divisors [ ] array ; leaf node ; build left and right subtree ; combine the information from left and right subtree at current node ; returns the maximum number of divisors in [ l , r ] ; If current node 's range is disjoint with query range ; If the current node stores information for the range that is completely inside the query range ; Returns maximum number of divisors from left or right subtree ; driver code ; First find smallest prime divisors for all the numbers ; Then build the divisors [ ] array to store the number of divisors ; Build segment tree for the divisors [ ] array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxn  1000005 NEW_LINE #define INF  99999999 NEW_LINE int smallest_prime [ maxn ] ; int divisors [ maxn ] ; int segmentTree [ 4 * maxn ] ; void findSmallestPrimeFactors ( ) { for ( int i = 0 ; i < maxn ; i ++ ) smallest_prime [ i ] = INF ; for ( long long i = 2 ; i < maxn ; i ++ ) { if ( smallest_prime [ i ] == INF ) { smallest_prime [ i ] = i ; for ( long long j = i * i ; j < maxn ; j += i ) if ( smallest_prime [ j ] > i ) smallest_prime [ j ] = i ; } } } void buildDivisorsArray ( ) { for ( int i = 1 ; i < maxn ; i ++ ) { divisors [ i ] = 1 ; int n = i , p = smallest_prime [ i ] , k = 0 ; while ( n > 1 ) { n = n / p ; k ++ ; if ( smallest_prime [ n ] != p ) { divisors [ i ] = divisors [ i ] * ( k + 1 ) ; k = 0 ; } p = smallest_prime [ n ] ; } } } void buildSegtmentTree ( int node , int a , int b ) { if ( a == b ) { segmentTree [ node ] = divisors [ a ] ; return ; } buildSegtmentTree ( 2 * node , a , ( a + b ) / 2 ) ; buildSegtmentTree ( 2 * node + 1 , ( ( a + b ) / 2 ) + 1 , b ) ; segmentTree [ node ] = max ( segmentTree [ 2 * node ] , segmentTree [ 2 * node + 1 ] ) ; } int query ( int node , int a , int b , int l , int r ) { if ( l > b a > r ) return -1 ; if ( a >= l && b <= r ) return segmentTree [ node ] ; return max ( query ( 2 * node , a , ( a + b ) / 2 , l , r ) , query ( 2 * node + 1 , ( ( a + b ) / 2 ) + 1 , b , l , r ) ) ; } int main ( ) { findSmallestPrimeFactors ( ) ; buildDivisorsArray ( ) ; buildSegtmentTree ( 1 , 1 , maxn - 1 ) ; cout << " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ has ▁ " << " ▁ in ▁ [ 1 , ▁ 100 ] ▁ are ▁ " << query ( 1 , 1 , maxn - 1 , 1 , 100 ) << endl ; cout << " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ has " << " ▁ in ▁ [ 10 , ▁ 48 ] ▁ are ▁ " << query ( 1 , 1 , maxn - 1 , 10 , 48 ) << endl ; cout << " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ has " << " ▁ in ▁ [ 1 , ▁ 10 ] ▁ are ▁ " << query ( 1 , 1 , maxn - 1 , 1 , 10 ) << endl ; return 0 ; }