主要是标记前面素数的最大的DP值,要认真一些。没想到居然写了一个很难发现的错误。
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <cmath> 5 #include <ctime> 6 #include <cstdlib> 7 #include <iostream> 8 using namespace std; 9 int o[200001]; 10 int prim[200001]; 11 int p[200001]; 12 int sp[200001]; 13 int flag[200001]; 14 int main() 15 { 16 int i,j,n,num = 1; 17 int temp,maxz; 18 for(i = 2; i <= 100000; i ++) 19 { 20 if(!o[i]) 21 { 22 sp[i] = num; 23 for(j = i+i; j <= 100000; j += i) 24 o[j] = 1; 25 prim[num++] = i; 26 } 27 } 28 scanf("%d",&n); 29 for(i = 0; i < n; i ++) 30 scanf("%d",&p[i]); 31 if(n == 1) 32 { 33 printf("1\n"); 34 return 0; 35 } 36 for(i = 0; i < n; i ++) 37 { 38 maxz = 0; 39 temp = p[i]; 40 for(j = 1; j < 200; j ++) 41 { 42 if(temp == 1) break; 43 if(temp%prim[j] == 0) 44 { 45 maxz = max(maxz,flag[j]); 46 while(temp%prim[j] == 0) 47 { 48 temp /= prim[j]; 49 } 50 } 51 } 52 if(temp != 1) 53 maxz = max(maxz,flag[sp[temp]]); 54 temp = p[i]; 55 for(j = 1; j < 200; j ++) 56 { 57 if(temp == 1) break; 58 if(temp%prim[j] == 0) 59 { 60 flag[j] = maxz + 1; 61 while(temp%prim[j] == 0) 62 { 63 temp /= prim[j]; 64 } 65 } 66 } 67 if(temp != 1) 68 flag[sp[temp]] = maxz + 1; 69 } 70 maxz = 0; 71 for(i = 1;i < num;i ++) 72 { 73 maxz = max(maxz,flag[i]); 74 } 75 printf("%d\n",maxz); 76 return 0; 77 }