DP
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits> 11 #include<string.h> 12 #include<cmath> 13 #include<stdlib.h> 14 #include<vector> 15 #define INF 1e7 16 #define MAXN 111111 17 #define Mod 1000007 18 using namespace std; 19 typedef long long LL; 20 21 int dp[777][10010]; 22 int a[777]; 23 int n, g, i, j; 24 25 int gcd(int a, int b) 26 { 27 return b == 0 ? a : gcd(b,a%b); 28 } 29 30 void init() 31 { 32 g = 0; 33 for (i = 1; i <= n; ++i) 34 for (j = 0; j <= 10004; ++j) 35 dp[i][j] = Mod; 36 } 37 int main() 38 { 39 cin >> n; 40 init(); 41 int maxi = 1; 42 for (i = 1; i <= n; ++i) { 43 cin >> a[i]; 44 g = gcd(a[i],g); 45 maxi = max(maxi, a[i]); 46 dp[i][a[i]] = 1; 47 } 48 for (i = 2; i <= n; ++i) { 49 for (j = 0; j <= maxi; ++j) { 50 if (dp[i - 1][j] != Mod) { 51 int gg = gcd(a[i],j); 52 if (gg == j) 53 dp[i][gg] = min(dp[i][gg], dp[i - 1][j]); 54 else 55 dp[i][gg] = min(dp[i][gg], dp[i - 1][j] + 1); 56 } 57 } 58 } 59 cout << n - dp[n][g] << endl; 60 return 0; 61 }
时间: 2024-10-20 03:42:12