No. of subsets whose gcd is 1
Subtraction Game 2
https://www.codechef.com/problems/AMSGAME2It is only a branch and bound type of recursion.
- #include<bits/stdc++.h>
- using namespace std;
- long long int n,i;
- long long int a[1000];
- long long int dp[70][50010];
- long long int go(long long int cur_indx,long long int cur_gcd)
- {
- if(cur_indx>=n && cur_gcd==1)
- return 1;
- else if(cur_indx>=n && cur_gcd!=1)
- return 0;
- if(dp[cur_indx][cur_gcd]!=-1)
- return dp[cur_indx][cur_gcd];
- else
- {
- long long int result=0;
- result=go(cur_indx+1,cur_gcd); /// without taking a[i];
- result+=go(cur_indx+1,__gcd(cur_gcd,a[cur_indx])); /// with taking a[i];
- dp[cur_indx][cur_gcd]=result;
- return result;
- }
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- // memset(dp,-1,sizeof(dp));
- for(int i=0;i<70;i++) for(int j=0;j<10005;j++) dp[i][j]=-1;
- cin>>n;
- for(i=0;i<n;i++)
- cin>>a[i];
- cout<<go(0,0)<<endl;
- }
- return 0;
- }
Comments
Post a Comment