debugger2017's blog

By debugger2017, history, 8 years ago, In English

Given an array, find the number of increasing sub-sequences having GCD=1 Example: A={1, 2, 3} Output: 5 The increasing sub-sequences are {1},{1,2},{1,2,3},{1,3},{2,3} The only approach I can think is to try all the possible sub-sequences and calculate their GCD.

  • Vote: I like it
  • -16
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
Rev. 3   Vote: I like it +21 Vote: I do not like it

UPD: contest is over, if you know how to solve this task, I'd like to know either.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This problem was earlier discussed in another blog 1-2 weeks back on CF.

  • »
    »
    8 years ago, # ^ |
    Rev. 2   Vote: I like it +17 Vote: I do not like it

    Constraints were small, so dpij = number of LIS ending at i with GCD j.

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Things will be more clear with a recurrence.

      • »
        »
        »
        »
        8 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it
        for(int i=0;i<n;i++)
        {
        	for(int j=i-1;j>=0;j--)
        	{
        	 	if(a[i]>a[j])
        		{
        	 		for(int k=1;k<=100;k++)
        			{
        			 	dp[i][gcd(a[i],k)]+=dp[j][k];
        			}
        		}
        	}
        }
        
        before this , you must set base values for all i, where dp[i][a[i]]=1;