codeforces 264B

题意很简单,给你n个严格上升的数字ai,
然后你要求出最大的子序列,满足相邻的数字不互质

我的思路比较奇葩,首先唯一分解每个ai,
然后通过他的质因子来寻找可能出现的转移的地方,然后dp[i] = max(dp[i], dp[k] + 1)
如果直接这样做会tle,然后我们想想,最靠前的边价值越小,不带转移的必要,所以我估算了下概率,舍掉80%的边直接不转移,然后就过了。。

//
//  Created by Matrix on 2016-01-22
//  Copyright (c) 2015 Matrix. All rights reserved.
//
//
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <stack>
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x,begin())
#define ll long long
#define CLR(x) memset(x, 0, sizeof x)
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 1e5 + 10;
const int maxv = 1e3 + 10;
const double eps = 1e-9;

int n;
int a[maxn];
vector <int> G[maxn];
int dp[maxn];
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    while(scanf("%d", &n) != EOF) {
        for(int i = 1; i <= 1e5; i++)
            G[i].clear();
        CLR(dp);
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", a + i);
            dp[i] = 1;
            vector <int> factors;
            int k = a[i];
            int num = sqrt(a[i] + 0.5);
            for(int j = 2; j <= num; j++) {
                if(k % j == 0) {
                    factors.push_back(j);
                    while(k % j == 0) {
                        k /= j;
                    }
                }
                if(k == 1) break;
            }
            if(k != 1) factors.push_back(k);
            for(k = 0; k < factors.size(); k++) {
                num = factors[k];
                int up = G[num].size();
                up = up * 0.8;
                for(int j = G[num].size() - 1; j >= up; j--) {
                    dp[i] = max(dp[i], dp[G[num][j]] + 1);
                }
            }
            for(k = 0; k < factors.size(); k++) {
                num = factors[k];
                G[num].push_back(i);
            }
            ans = max(ans, dp[i]);
        }
        cout << ans << endl;
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值