Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

godmodegod's blog

By godmodegod, 2 years ago, In English

A chain is a sequence of positive integers A1, A2, A3, ..., An such that Ai+1 divides Ai. (In other words, A1 is a divisor of A2, A2 is a divisor of A3, and so on.)

The length of a chain is the number of elements in the sequence.

Given N elements, you need to find a chain of maximum length using a subset of the given elements.

1<=N<=1e6 1<=a[i]<=1e6

Now, you can sort the array and track gcd (LIS like approach), but how do you come up with a solution which adheres to the time limit?

N = 5 array = 4 2 5 80 4

ans = 4, i.e. [2,4,4,80]

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Did you try by yourself?

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    
    int LIS(unsigned int ind, vector<int>&a, int gg) {
    	if (ind == a.size())return 0;
    	int take = 0;
    	int ngg = __gcd(gg, a[ind]);
    	if (ngg != 1)take = max(LIS(ind + 1, a, ngg) + 1, take);
    
    	int nottake = LIS(ind + 1, a, gg);
    	return max(take, nottake);
    }
    
    int main()
    {
    	ios_base::sync_with_stdio(false);
    	cin.tie(NULL), cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    	freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
    #endif
    	int n;
    	cin >> n;
    	vector<int> a(n);
    
    	for (int i = 0; i < n; ++i)cin >> a[i];
    	sort(a.begin(), a.end());
    
    	int ans = 0;
    
    	for (int i = 0; i < n; ++i) {
    		ans = max(ans, LIS(i, a, 0));
    	}
    
    	cout << ans << endl;
    
    
    
    	return 0;
    }
    

    Obvious TLE.

»
2 years ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

for every index i and for every prime p, draw an edge from i to the smallest index j such that that A_j is divisible by p*A_i. This creates a DAG and then you can DP on this to find the longest path. I think you can do this N log N.

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

    Could you implement it please, thanks!!!

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

    Won't this TLE? N <= 10^6 and there are roughly 70K primes less than 500000.

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

      For A_i there are only MAX_N/A_i numbers you can multiply by (even less primes), so if you combine identical values that bounds the complexity to the harmonic series (N/1 + N/2 + ...) or N log N.

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

      Oh shoot primes don't even matter, there are very few possible edges (by the same harmonic series argument) so you can just draw all edges explicitly and then find the answer.

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    void solve()
    {
    
        int n, ans = 0; cin >> n;
        vector<int> a(n);
    
        for (auto &i : a) cin >> i;
        int mx = *max_element(a.begin(), a.end());
    
        vector<int> dp(mx + 1, 0);
        for (auto &i : a) dp[i]++;
    
        for (int i = mx; i >= 1; i--) {
    
            if (!dp[i]) continue;
            int val = dp[i];
    
            for (int j = 2 * i; j <= mx; j += i)
                dp[i] = max(dp[i], val + dp[j]);
    
            ans = max(ans, dp[i]);
    
        }
    
        cout << ans << "\n";
    }
    

    Won't this be enough?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

dp problem