Skip to content

Commit b87d646

Browse files
authored
Merge pull request ashutosh97#144 from rohitsh16/master
Added prime_subsequence
2 parents 2ae1800 + db7026c commit b87d646

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Prime_subsequence/question.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
Given a sequence of prime numbers A1,A2,…,AN. This sequence has exactly 2N subsequences.
3+
A subsequence of A is good if it does not contain any two identical numbers; in particular, the empty sequence is good.
4+
5+
You to find the number of good subsequences which contain at most K numbers.
6+
This number could be very large, so compute it modulo 1,000,000,007.
7+
8+
Constraints:
9+
1≤K≤N≤105
10+
2≤Ai≤8,000 for each valid i

Prime_subsequence/solution.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
ios_base::sync_with_stdio(false);
5+
int n,k;cin>>n>>k;
6+
int a[n];long long int mod=1000000007;
7+
int max=0;
8+
for(int i=0;i<n;i++){
9+
cin>>a[i];
10+
if(a[i]>max)
11+
max=a[i];
12+
}
13+
long int f[max+1];
14+
memset(f,0,sizeof(f));
15+
for(int i=0;i<n;i++)
16+
f[a[i]]++;
17+
vector<long int>v;
18+
for(int i=0;i<=max;i++)
19+
if(f[i])
20+
v.push_back(f[i]);
21+
long int l=v.size();
22+
k=k>l?l:k;
23+
long int dpans[k+1][l],val;
24+
val=v[0]+1;
25+
memset(dpans,0,sizeof(dpans));
26+
dpans[0][0]=1;
27+
dpans[1][0]=val;
28+
long int total=0;
29+
for(int i=2;i<=k;i++)
30+
dpans[i][0]=val;
31+
for(int i=1;i<l;i++)
32+
dpans[0][i]=1;
33+
for(int i=1;i<l;i++)
34+
for(int j=1;j<=k;j++){
35+
total=((v[i]%mod)*(dpans[j-1][i-1])%mod)%mod;
36+
dpans[j][i]=(((dpans[j][i-1])%mod)+(total%mod))%mod;
37+
}
38+
total=(dpans[k][l-1])%mod;
39+
cout<<total<<endl;
40+
return 0;
41+
}

0 commit comments

Comments
 (0)