Given a positive integer N, the task is to find the value of (22 * X), where X is the binary representation of N. Since the answer can be very large, print it modulo 109 + 7.
Examples:
Input: N = 2
Output: 1048576
Explanation:
The binary representation of 2 is (10)2.
Therefore, the value of (22 * 10) % (109 + 7) = (220) % (109 + 7) = 1048576
Input: N = 150
Output: 654430484
Naive Approach: The simplest approach to solve this problem is to generate the binary representation of N, say X, and calculate the value of (22 * X) % (109 + 7) using modular exponentiation:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long power( long long X,
long long Y)
{
long long res = 1;
X = X % M;
if (X == 0)
return 0;
while (Y > 0) {
if (Y & 1) {
res = (res * X) % M;
}
Y = Y >> 1;
X = (X * X) % M;
}
return res;
}
int findValue( long long int n)
{
long long X = 0;
long long pow_10 = 1;
while (n) {
if (n & 1) {
X += pow_10;
}
pow_10 *= 10;
n /= 2;
}
X = (X * 2) % M;
long long res = power(2, X);
return res;
}
int main()
{
long long n = 2;
cout << findValue(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int M = 1000000007 ;
static int power( int X,
int Y)
{
int res = 1 ;
X = X % M;
if (X == 0 )
return 0 ;
while (Y > 0 )
{
if ((Y & 1 ) != 0 )
{
res = (res * X) % M;
}
Y = Y >> 1 ;
X = (X * X) % M;
}
return res;
}
static int findValue( int n)
{
int X = 0 ;
int pow_10 = 1 ;
while (n != 0 )
{
if ((n & 1 ) != 0 )
{
X += pow_10;
}
pow_10 *= 10 ;
n /= 2 ;
}
X = (X * 2 ) % M;
int res = power( 2 , X);
return res;
}
public static void main(String[] args)
{
int n = 2 ;
System.out.println(findValue(n));
}
}
|
Python3
M = 1000000007
def power(X, Y):
res = 1
X = X % M
if (X = = 0 ):
return 0
while (Y > 0 ):
if (Y & 1 ):
res = (res * X) % M
Y = Y >> 1
X = (X * X) % M
return res
def findValue(n):
X = 0
pow_10 = 1
while (n):
if (n & 1 ):
X + = pow_10
pow_10 * = 10
n / / = 2
X = (X * 2 ) % M
res = power( 2 , X)
return res
if __name__ = = "__main__" :
n = 2
print (findValue(n))
|
C#
using System;
class GFG
{
static int M = 1000000007;
static int power( int X,
int Y)
{
int res = 1;
X = X % M;
if (X == 0)
return 0;
while (Y > 0)
{
if ((Y & 1) != 0)
{
res = (res * X) % M;
}
Y = Y >> 1;
X = (X * X) % M;
}
return res;
}
static int findValue( int n)
{
int X = 0;
int pow_10 = 1;
while (n != 0)
{
if ((n & 1) != 0)
{
X += pow_10;
}
pow_10 *= 10;
n /= 2;
}
X = (X * 2) % M;
int res = power(2, X);
return res;
}
public static void Main(String[] args)
{
int n = 2;
Console.WriteLine(findValue(n));
}
}
|
Javascript
<script>
M = 1000000007;
function power( X,Y)
{
var res = 1;
X = X % M;
if (X == 0)
return 0;
while (Y > 0)
{
if ((Y & 1) != 0)
{
res = (res * X) % M;
}
Y = Y >> 1;
X = (X * X) % M;
}
return res;
}
function findValue(n)
{
var X = 0;
var pow_10 = 1;
while (n != 0)
{
if ((n & 1) != 0)
{
X += pow_10;
}
pow_10 *= 10;
n /= 2;
}
X = (X * 2) % M;
var res = power(2, X);
return res;
}
var n = 2;
document.write(findValue(n));
</script>
|
Time Complexity: O(log2(X)), where X is the binary representation of N
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using Dynamic programming based on the following observations:
If N == 1 then the required output is (21)2 = (21)2
If N == 2 then the required output is (210)2 = (210)2
If N == 3 then the required output is (211)2 = (210 * 21)2
If N == 4 then the required output is (2100)2 = (2100)2
If N == 5 then the required output is (2101)2 = (2100 * 21)2
………………………..
Below is the relation between the dynamic programming states:
If N is a power of 2, then dp[N] = power(dp[N / 2], 10)
Otherwise, dp[N] = dp[(N & (-N))] * dp[N – (N & (-N))]
dp[N]2: Stores the required output for the positive integer N.
Follow the steps below to solve the problem:
- Initialize an array, say dp[], such that dp[i]2 stores the value of (22 * X) % (109 + 7), where X is the binary representation of i.
- Iterate over the range [3, N] using variable i and find all possible value of dp[i] using the tabulation method.
- Finally, print the value of dp[N]2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long power( long long X,
long long Y)
{
long long res = 1;
X = X % M;
if (X == 0)
return 0;
while (Y > 0) {
if (Y & 1) {
res = (res * X) % M;
}
Y = Y >> 1;
X = (X * X) % M;
}
return res;
}
long long findValue( long long N)
{
long long dp[N + 1];
dp[1] = 2;
dp[2] = 1024;
for ( int i = 3; i <= N; i++) {
int y = (i & (-i));
int x = i - y;
if (x == 0) {
dp[i]
= power(dp[i / 2], 10);
}
else {
dp[i]
= (dp[x] * dp[y]) % M;
}
}
return (dp[N] * dp[N]) % M;
}
int main()
{
long long n = 150;
cout << findValue(n);
return 0;
}
|
Java
class GFG
{
static final long M = 1000000007 ;
static long power( long X,
long Y)
{
long res = 1 ;
X = X % M;
if (X == 0 )
return 0 ;
while (Y > 0 )
{
if (Y % 2 == 1 )
{
res = (res * X) % M;
}
Y = Y >> 1 ;
X = (X * X) % M;
}
return res;
}
static long findValue( int N)
{
long []dp = new long [N + 1 ];
dp[ 1 ] = 2 ;
dp[ 2 ] = 1024 ;
for ( int i = 3 ; i <= N; i++)
{
int y = (i & (-i));
int x = i - y;
if (x == 0 )
{
dp[i]
= power(dp[i / 2 ], 10 );
}
else
{
dp[i]
= (dp[x] * dp[y]) % M;
}
}
return (dp[N] * dp[N]) % M;
}
public static void main(String[] args)
{
int n = 150 ;
System.out.print(findValue(n));
}
}
|
Python3
M = 1000000007 ;
def power(X, Y):
res = 1 ;
X = X % M;
if (X = = 0 ):
return 0 ;
while (Y > 0 ):
if (Y % 2 = = 1 ):
res = (res * X) % M;
Y = Y >> 1 ;
X = (X * X) % M;
return res;
def findValue(N):
dp = [ 0 ] * (N + 1 );
dp[ 1 ] = 2 ;
dp[ 2 ] = 1024 ;
for i in range ( 3 , N + 1 ):
y = (i & ( - i));
x = i - y;
if (x = = 0 ):
dp[i] = power(dp[i / / 2 ], 10 );
else :
dp[i] = (dp[x] * dp[y]) % M;
return (dp[N] * dp[N]) % M;
if __name__ = = '__main__' :
n = 150 ;
print (findValue(n));
|
C#
using System;
class GFG
{
static readonly long M = 1000000007;
static long power( long X,
long Y)
{
long res = 1;
X = X % M;
if (X == 0)
return 0;
while (Y > 0)
{
if (Y % 2 == 1)
{
res = (res * X) % M;
}
Y = Y >> 1;
X = (X * X) % M;
}
return res;
}
static long findValue( int N)
{
long []dp = new long [N + 1];
dp[1] = 2;
dp[2] = 1024;
for ( int i = 3; i <= N; i++)
{
int y = (i & (-i));
int x = i - y;
if (x == 0)
{
dp[i]
= power(dp[i / 2], 10);
}
else
{
dp[i]
= (dp[x] * dp[y]) % M;
}
}
return (dp[N] * dp[N]) % M;
}
public static void Main(String[] args)
{
int n = 150;
Console.Write(findValue(n));
}
}
|
Javascript
<script>
var M = 1000000007n
function findValue(N)
{
var dp = Array(N + 1)
dp[1] = 2n
dp[2] = 1024n
for (let i = 3 ; i <= N ; i++) {
var y = (i & (-i));
var x = i - y;
if (x == 0) {
dp[i] = 1n
for (let j = 0 ; j < 10 ; j++){
dp[i] = BigInt((BigInt(dp[i/2])*dp[i]) % M)
}
}
else {
dp[i] = (dp[x] * dp[y]) % M;
}
}
return (dp[N] * dp[N]) % M;
}
var n = 150
console.log(Number(findValue(n)))
</script>
|
Time Complexity: O(N *log(N))
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!