Codeforces Round #162 (Div. 1) B. Good Sequences (dp+分解素数)

题目:http://codeforces.com/problemset/problem/264/B

题意:给你一个递增序列,然后找出满足两点要求的最长子序列

第一点是a[i]>a[i-1]

第二点 gcd(a[i],a[i-1])>1 也就是说两个数不能互质

找出最长的子序列长度

思路:首先想互质问题,如果两个数互质说明两个数之间没有素因子相同,我们想以每个素因子结尾的最大长度是多少

然后比如样例 2 3 4 6 9

第一个数 2      2结尾 1

第二个数 3      3结尾 1

第三个数 4       2结尾 2

第四个数6        拆分因子有   2,3      2结尾 3, 3结尾2        ,但是这个时候我以6结尾,那么其实3这个位置长度也是3了,

(所以,我们要找出最大的那个长度,再重新赋值到这个数的每个素因子上) 这个时候就能解出来了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<iostream>
#include<map>
#define mod 1000007
#define maxn 200001
using namespace std;
typedef long long ll;
ll a[maxn];
ll dp[maxn];
ll n;
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        ll z=a[i];
        map<ll,ll> mp;
        vector<int> q;
            while(z!=1){
                ll t=(ll)sqrt((double)z);//必须要加,不然会超时
                ll j=2;
                for(;j<=t;j++){
                    if(z%j==0){
                        if(mp[j]==0) q.push_back(j);
                        mp[j]++;
                        z/=j;
                        break;
                    }
                }
                if(j==t+1){
                    if(mp[z]==0) q.push_back(z);//判断素数情况
                    break;
                }
            }
        ll mx=-1;
        for(int j=0;j<q.size();j++){//找出最大长度
            dp[q[j]]++;
            if(mx==-1) mx=dp[q[j]];
            else mx=max(mx,dp[q[j]]);
        }
        for(int j=0;j<q.size();j++){//重新赋到每一个位置
            dp[q[j]]=mx;
        }
    }
    ll x=dp[0];
    for(int i=0;i<maxn;i++){
        x=max(x,dp[i]);
    }
    cout<<max((ll)1,x);
} 

原文地址:https://www.cnblogs.com/Lis-/p/10727115.html

时间: 2024-08-29 13:35:16

Codeforces Round #162 (Div. 1) B. Good Sequences (dp+分解素数)的相关文章

Codeforces Round #162 (Div. 1) C Choosing Balls dp

//dp[i] 表示以颜色为i结尾的最大值 //dp[i] = max(dp[i] , dp[i] + a*v[i] ,other_max + b*v[i]) ; //为除颜色i以外的其它颜色的最大值 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 100010 ; const __int64 inf = 0x7fffffffffff

数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

题目传送门 1 /* 2 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 3 另外不足一个区间的直接计算个数就可以了 4 */ 5 #include <cstdio> 6 #include <cmath> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 using namespace std; 11 12 typedef

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

题目链接:Codeforces Round #369 (Div. 2) C. Coloring Trees 题意: 有n个树,每个树有一个颜色,如果颜色值为0,表示没有颜色,一共有m个颜色,第j种颜色涂第i棵树需要花费pij,颜色一样且相邻的分为一组 现在要将所有颜色为0的树涂上颜色,使得这些树恰好可以分为k组,问最小的花费 题解: 考虑dp[i][j][k],表示考虑第i棵树涂第j种颜色,当前分为k组的最小花费,然后状态转移看代码,注意的是dp的初始状态 1 #include<bits/std

Codeforces Round #455 (Div. 2) C. Python Indentation dp递推

Codeforces Round #455 (Div. 2) C. Python Indentation 题意:python 里面,给出 n 个 for 循环或陈述语句,'f' 里面必须要有语句.按 python 缩进的方式组合成合法的程序,问有多少种可能方案. tags: dp dp[i][j] 表示第 i 个语句缩进为 j 时的可能方案数, 转移: 1] 如果第 i 个是 'f' , 则第 i+1 个肯定要比第 i 个多缩进一个单位,即 dp[i+1][j] = dp[i][j]. 2]如果

Codeforces Round #162 (Div. 2) A~D 题解

A. Colorful Stones (Simplified Edition) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There is a sequence of colorful stones. The color of each stone is one of red, green, or blue. You a

Codeforces Round #162 (Div. 1) B dp

//存入所有数的素数因数 //若两个数不互质,那么他们之间必然有素数因数 //dp[i][0]表示第i个数不选前i个数中能得到的最长序列 //dp[i][1]表示选了第i个数 //dp[i][0] = max(dp[i-1][0] , dp[i-1][1]) //dp[i][1] = max(dp[pos][1] + 1 ,dp[i][1] ); //pos位第i个数的质数因子出现的最后一个位置 #include<cstdio> #include<cstring> #include

Codeforces Round #162 (Div. 1) A. Escape from Stones

A. Escape from Stones time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squ