Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

Блог пользователя godmodegod

Автор godmodegod, 2 года назад, По-английски

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]

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Did you try by yourself?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится
    #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 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

dp problem