0% found this document useful (0 votes)
115 views2 pages

Omega Pairs Media - Net Interview Question

The document contains code to solve a problem using dynamic programming in Python and C++. It first takes input and initializes some variables and arrays. It then uses dynamic programming to calculate the number of ways to represent numbers from 1 to n as products of distinct prime factors, storing the results in a 2D DP table. Finally, it calculates the final answer by summing over the last row of the DP table and multiplying by a factor based on the number of 1s in the input.

Uploaded by

Shivam jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views2 pages

Omega Pairs Media - Net Interview Question

The document contains code to solve a problem using dynamic programming in Python and C++. It first takes input and initializes some variables and arrays. It then uses dynamic programming to calculate the number of ways to represent numbers from 1 to n as products of distinct prime factors, storing the results in a 2D DP table. Finally, it calculates the final answer by summing over the last row of the DP table and multiplying by a factor based on the number of 1s in the input.

Uploaded by

Shivam jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

(Python):

n=int(input())
lst=list(map(int,input().split()))

mod=10**9+7

dp=[[-1 for b in range(1<<10)] for a in range(n)]

primes=[2,3,5,7,11,13,17,19,23,29]

arr=[-1 for _ in range(n)]

for idx,val in enumerate(lst):


res=0
if val==1:
arr[idx]=-1
continue

for i in range(10):

if val%primes[i]==0:
if val%(primes[i]**2)==0:
res=-1
break
res|=(1<<i)

arr[idx]=res

for i in range(n):
for mask in range(1<<10):

if i==0:
dp[i][mask]=1 if mask==arr[0] else 0
else:
if arr[i]==-1:
dp[i][mask]=dp[i-1][mask]
else:
dp[i][mask]=dp[i-1][mask]+(dp[i-1][mask^arr[i]] if mask&arr[i]==arr[i] else
0)+(1 if arr[i]==mask else 0)

if dp[i][mask]>=mod:
dp[i][mask]%=mod

ans=0
for mask in range(1<<10):
ans+=dp[n-1][mask]
ans%=mod

print(ans*(2**lst.count(1))%mod)

(C++):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<ll> a(20000), cnt(31, 0), power(20001, 1);


ll n, M = 1e9 + 7;
vector<vector<ll>> dp(31, vector<ll>(1025, -1));
vector<int> prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

ll solve(ll num, ll mask)


{
if (num > 30)
return mask != 0;

if (dp[num][mask] != -1)
return dp[num][mask];

ll take = 0, nottake = 0;

nottake = solve(num + 1, mask);

ll flag = 1, temp = mask;


for (int j = 0; j < 10; j++)
{
if (num % prime[j] != 0)
continue;

if ((temp & (1 << j)))


{
flag = 0;
break;
}
else
temp |= (1LL << j);
}
if (flag)
(take = (cnt[num] * solve(num + 1, temp)) % M) %= M;

return dp[num][mask] = (take + nottake) % M;


}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] % 4 == 0 || a[i] % 9 == 0 || a[i] % 16 == 0 || a[i] % 25 == 0)
continue;

cnt[a[i]]++;
}

for (int i = 1; i <= 20000; i++)


power[i] = (2 * power[i - 1]) % M;

cout << ll((solve(2, 0) % M) * power[cnt[1]]) % M;


return 0;
}

You might also like