DP good Sequences

Source: Internet
Author: User

To give n numbers in ascending order, you must find a substring. Each two adjacent numbers do not have mutual quality, and the length of the longest string is obtained.

Prompt

1) DP [I] indicates the longest string to the number I

2) DP [I] is updated with metric items that are not in the same quality as item I in the previous I-1

3) The search and I items do not have mutual quality, that is, the search and I items have a public factor, so the establishment of an array dig [I] indicates the number of times this factor appears

# Include <stdio. h> # include <string. h> # define maxn limit 5int DP [maxn]; int dig [maxn]; int mark [maxn]; int gcd (int A, int B) {int K; while (B! = 0) {k = A % B; A = B; B = K;} return a;} int max (int A, int B) {return a <B? B: A;} int find (int A) {int Maxx = 0; if (a = 1 | A = 2 | A = 3) {dig [a] ++; return 1 ;}int I = 0, j = 2; int flag = 1; while (J * j <=) {if (a % J = 0) {dig [J] ++; flag = 0; If (J * J! = A) Dig [A/J] ++; Maxx = max (Maxx, dig [J]), dig [A/J]);} J ++ ;} j = 2; while (J * j <= A) // update the number of occurrences of all the I-th factors to the maximum value !!! {If (a % J = 0) {dig [J] = Maxx; If (J * J! = A) Dig [A/J] = Maxx;} J ++;} If (flag = 1) // Dig [a] of prime numbers is directly added with 1 {dig [a] ++; return 1;} return Maxx;} void Init () {int I; for (I = 0; I <maxn; I ++) {mark [I] = 0; DP [I] = 0; Dig [I] = 0 ;}} int main () {int N; int I, j, k; int num [maxn]; int m; int tot; while (scanf ("% d", & N )! =-1) {Init (); M = 0; for (I = 1; I <= N; I ++) {scanf ("% d ", & num [I]); Mark [num [I] = I ;}for (I = 1; I <= N; I ++) {tot = find (Num [I]); DP [I] = tot; M = max (M, DP [I]);} printf ("% d \ n ", m);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.