text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int INF = ((long long)1E9 + 7); template <class C> void mini(C& _a, C _b) { _a = min(_a, _b); } template <class C> void maxi(C& _a, C _b) { _a = max(_a, _b); } const int MAXN = 5100; long long a[MAXN]; int c[MAXN]; int res[MAXN]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < (n); ++i) { cin >> a[i]; while (a[i] % 2 == 0) { ++c[i]; a[i] /= 2; } } a[n] = 1; for (int i = 0; i < (n); ++i) res[i] = INF; res[n] = 0; int ans = INF; for (int j = (n - 1); j >= (0); --j) { for (int i = (j + 1); i <= (n); ++i) { int d = i - j; if (a[j] % a[i] == 0 && (c[j] == c[i] - d || c[i] < d)) mini(res[j], res[i] + d - 1); } mini(ans, res[j] + j); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[100010], b[100010], c[100010]; int f[100010]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (int i = 1; i <= n; i++) { long long temp = a[i]; while (temp % 2 == 0) temp /= 2, b[i]++; c[i] = temp; } f[1] = 0; for (int i = 2; i <= n; i++) { f[i] = i - 1; for (int j = 1; j < i; j++) if (c[j] % c[i] == 0) if (b[i] == b[j] + i - j || b[i] <= i - j - 1) f[i] = min(f[i], f[j] + i - j - 1); } int ans = n - 1; for (int i = 1; i <= n; i++) ans = min(ans, f[i] + n - i); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 5005; long long dp[N], n, x[N], cnt[N]; signed main() { cin >> n; for (long long i = 1; i <= n; i++) { cin >> x[i]; dp[i] = i - 1; while (x[i] % 2 == 0) { x[i] /= 2; cnt[i]++; } } for (long long i = 2; i <= n; i++) { for (long long j = 0; j < i; j++) { long long y = i - j - 1; if (x[j] % x[i] == 0 && (cnt[i] - cnt[j] == y + 1 || cnt[i] <= y)) { dp[i] = min(dp[i], dp[j] + y); } } } long long ans = 1e9; for (long long i = 1; i <= n; i++) ans = min(ans, dp[i] + n - i); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using db = long double; using row = vector<int>; using ii = pair<int, int>; const int N = 1e6 + 5, M = 26, A = 6561, LG = 19, MOD = 1e9 + 7; const int BLOCK = 500; const long double EPS = 1e-7; int n, dp[5005], cnt[5005]; ll a[5005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n + 1; i++) { cin >> a[i]; while (a[i] % 2 == 0) a[i] /= 2, cnt[i]++; } for (int i = 1; i < n + 1; i++) { dp[i] = 1; for (int j = 1; j < i; j++) if (a[j] % a[i] == 0 && (cnt[i] - i == cnt[j] - j || cnt[i] <= i - j - 1)) dp[i] = max(dp[i], dp[j] + 1); } cout << n - *max_element(dp + 1, dp + n + 1) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5010; long long a[maxn]; int p[maxn]; int dp[maxn]; int main() { int n; scanf("%d", &n); memset(p, 0, sizeof(p)); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); while (a[i] % 2 == 0) a[i] /= 2, p[i]++; } int tmp = 1; for (int i = 0; i < n; i++) { dp[i] = 1; for (int j = 0; j < i; j++) { if (!p[i]) { if (a[j] % a[i] == 0) dp[i] = max(dp[j] + 1, dp[i]); } else { if (i - j - 1 < p[i]) { if ((p[i] - p[j]) == (i - j) && a[j] % a[i] == 0) dp[i] = max(dp[j] + 1, dp[i]); } else { if (a[j] % a[i] == 0) dp[i] = max(dp[j] + 1, dp[i]); } } } tmp = max(dp[i], tmp); } printf("%d\n", n - tmp); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:512000000") using namespace std; template <class T> inline T sqr(T x) { return x * x; } template <class T> inline string tostr(const T& x) { stringstream ss; ss << x; return ss.str(); } const double EPS = 1e-6; const int INF = 1000 * 1000 * 1000; const char CINF = 102; const long long LINF = INF * 1ll * INF; const long double PI = 3.1415926535897932384626433832795; int n; long long src[5100]; long long A[5100]; int P[5100]; int dp[5100]; int main() { cin >> n; for (int i = 0; i < (n); ++i) { cin >> src[i]; A[i] = src[i]; P[i] = 0; while (A[i] % 2 == 0) { A[i] /= 2; ++P[i]; } } int res = n - 1; for (int i = 0; i < (n); ++i) { dp[i] = i; for (int j = i - 1; j >= 0; --j) { int k = i - j - 1; if (dp[i] <= k) break; if (A[j] % A[i] || (P[j] + k + 1 != P[i] && k < P[i])) continue; dp[i] = min(dp[i], dp[j] + k); } res = min(res, dp[i] + (n - i - 1)); } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5005; long long a[N]; int dp[N]; void solve() { int n; scanf("%d ", &n); int ret = 0; for (int i = 1; i <= n; ++i) { scanf("%lld ", &a[i]); dp[i] = 1; long long v = a[i]; for (int j = i - 1; j; --j) { if (v & 1) { if (a[j] % v == 0) { dp[i] = max(dp[i], dp[j] + 1); } } else { long long t = a[j] * 2; if (t % v == 0 && (t / v) & 1) { dp[i] = max(dp[i], dp[j] + 1); } } if (v % 2 == 0) { v /= 2; } } ret = max(ret, dp[i]); } printf("%d\n", n - ret); } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 5000 + 10; int dp[MAX_N]; long long a[MAX_N]; long long a2[MAX_N]; int pw[MAX_N]; int n; bool check(int i, int j) { if (a2[i] % a2[j] != 0) return false; return pw[j] <= j - i - 1 || pw[j] == pw[i] + j - i; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; a2[i] = a[i]; while (a2[i] % 2 == 0) { a2[i] /= 2; ++pw[i]; } } for (int i = 0; i < n; ++i) { dp[i] = max(dp[i], 1); for (int j = i + 1; j < n; ++j) { if (check(i, j)) dp[j] = max(dp[j], dp[i] + 1); } } cout << n - *max_element(dp, dp + n) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5005; int n; long long a[N]; int f[N]; int main() { int i, j, ans; long long tmp; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%I64d", a + i); for (i = 1; i <= n; i++) { f[i] = 1; tmp = a[i]; for (j = i - 1; j; j--) { if ((tmp & 1) ? (a[j] % tmp == 0) : (a[j] % tmp == (tmp / 2))) f[i] = max(f[i], f[j] + 1); if ((tmp & 1) == 0) tmp >>= 1; } } ans = 0; for (i = 1; i <= n; i++) ans = max(ans, f[i]); printf("%d\n", n - ans); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; long long n, a[5010], odd[5010], pw2[5010], dp[5010], ans; int main() { long long i, j; scanf("%I64d", &n); for (i = 0; i < n; i++) { scanf("%I64d", &a[i]); odd[i] = a[i]; while (odd[i] % 2 == 0) { pw2[i]++; odd[i] /= 2; } } for (i = 1; i < n; i++) { for (j = 0; j < i; j++) { if (odd[j] % odd[i] == 0) { if (pw2[i] < (i - j)) { dp[i] = max(dp[i], dp[j] + 1); } else { if (pw2[j] == pw2[i] - (i - j)) { dp[i] = max(dp[i], dp[j] + 1); } } } } } ans = n; for (i = 0; i < n; i++) { ans = min(ans, n - dp[i] - 1); } printf("%I64d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long dp[5010]; long long n; long long a[5010]; long long hs[5010]; int main() { memset(dp, -1, sizeof dp); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; hs[i]++; } } dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = i - 1; for (int j = 1; j < i; j++) { if (a[j] % a[i] == 0 && dp[j] != -1 && (hs[j] + i - j == hs[i] || (hs[i] == 0 || (hs[j] == 0 && i - j >= hs[i]) || (hs[j] != 0 && i - j - 1 >= hs[i])))) { dp[i] = min(dp[j] + i - j - 1, dp[i]); } } } long long ans = n; for (int i = 1; i <= n; i++) { if (dp[i] != -1) { ans = min(ans, dp[i] + n - i); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5046; const int oo = (1 << 28); int b[N], dp[N]; long long a[N]; int main() { int n; while (cin >> n) { for (int i = 1; i <= n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { b[i]++; a[i] /= 2; } } for (int i = n; i >= 1; i--) { dp[i] = 1; for (int j = i + 1; j <= n; j++) { if (a[i] % a[j] == 0 && (b[i] + j - i == b[j] || b[j] <= j - i - 1)) { dp[i] = max(dp[i], dp[j] + 1); } } } int ans = oo; for (int i = 1; i <= n; i++) { ans = min(ans, n - dp[i]); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const double EPS = 1e-7; long long a[5000]; int pot2[5000]; int dp[5000]; int n; void comp_pot2() { for (int i = 0; i < n; i++) { pot2[i] = 0; long long tmp = a[i]; while (tmp % 2 == 0) { pot2[i]++; tmp /= 2; } } } bool valid(int j, int i) { long long fi = a[i] >> pot2[i]; if (a[j] % fi) return false; if (pot2[j] + i - j == pot2[i]) return true; return pot2[i] < i - j; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; comp_pot2(); for (int i = 0; i < n; i++) { dp[i] = 1; for (int j = 0; j < i; j++) if (valid(j, i)) dp[i] = max(dp[i], dp[j] + 1); } int ans = 0; for (int i = 0; i < n; i++) ans = max(ans, dp[i]); cout << (n - ans) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, dp[10001], v[10001]; long long f[10001]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> f[i]; while (f[i] % 2 == 0) f[i] /= 2, v[i]++; } int ans = 0; for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) if (f[j] % f[i] == 0 && (v[i] - i == v[j] - j || v[i] <= i - j - 1)) dp[i] = max(dp[i], dp[j] + 1); ans = max(ans, dp[i]); } cout << n - ans << '\n'; }
#include <bits/stdc++.h> using namespace std; const long long inf = INT_MAX, df = 5005; inline long long read() { long long x = 0, y = 1; char ch = getchar(); while (ch > '9' || ch < '0') y = (ch == '-') ? -1 : 1, ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return x * y; } long long i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a[df], b[df], f[df], c[df]; int main() { n = read(); for (long long i = (1); i <= (n); ++i) a[i] = read(); for (long long i = (1); i <= (n); ++i) { b[i] = 0; while (!(a[i] & 1)) a[i] >>= 1, b[i]++; } f[0] = f[1] = 0; for (long long i = (2); i <= (n); ++i) f[i] = i - 1; for (long long i = (2); i <= (n); ++i) { for (long long j = (i - 1); j >= (1); --j) { long long x = i - j; if (a[j] % a[i] == 0 && (b[i] - b[j] == x || b[i] <= x - 1)) { f[i] = min(f[i], f[j] + x - 1); } } } long long ans = inf; for (long long i = (1); i <= (n); ++i) ans = min(ans, f[i] + (n - i)); return printf("%lld\n", ans), 0; }
#include <bits/stdc++.h> using namespace std; long long a[5010], x; int d[5010], ans; int main() { int n, i, j; scanf("%d", &n); for (i = 0; i < n; ++i) cin >> a[i]; for (i = 0; i < n; ++i) { d[i] = 1; x = a[i]; for (j = i - 1; j >= 0; --j) { if (((x & 1) && a[j] % x == 0) || ((x & 1) == 0 && a[j] % x == (x >> 1))) { d[i] = max(d[i], d[j] + 1); } if ((x & 1) == 0) x >>= 1; } } for (i = 0; i < n; ++i) ans = max(ans, d[i]); printf("%d\n", n - ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; template <class T> void dbs(string str, T t) { cout << str << " : " << t << endl; } template <class T, class... second> void dbs(string str, T t, second... s) { int idx = str.find(','); cout << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...); } template <class second, class T> ostream& operator<<(ostream& os, const pair<second, T>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, const set<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class second, class T> ostream& operator<<(ostream& os, const map<second, T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> void prc(T a, T b) { cout << "["; for (T i = a; i != b; ++i) { if (i != a) cout << ", "; cout << *i; } cout << "]"; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int ea[n]; memset(ea, 0, sizeof ea); long long a[n]; for (auto i = (0); i <= (n - 1); ++i) { cin >> a[i]; while (!(a[i] & 1)) a[i] >>= 1, ++ea[i]; } int dp[n]; int ans = 1; for (auto i = (0); i <= (n - 1); ++i) { dp[i] = 1; for (auto j = (0); j <= (i - 1); ++j) { if (a[j] % a[i] == 0 and (ea[i] - (i - j) <= -1 or ea[j] == ea[i] - (i - j))) dp[i] = max(dp[i], 1 + dp[j]); } ans = max(ans, dp[i]); } cout << n - ans << "\n"; return 0; }
#include <bits/stdc++.h> template <class t> inline void read(t &s) { s = 0; register long long f = 1; register char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); s *= f; return; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), x = -x; long long buf[21], top = 0; while (x) buf[++top] = x % 10, x /= 10; if (!top) buf[++top] = 0; while (top) putchar(buf[top--] ^ '0'); return; } inline void setIn(std::string s) { freopen(s.c_str(), "r", stdin); return; } inline void setOut(std::string s) { freopen(s.c_str(), "w", stdout); return; } inline void setIO(std::string s = "") { setIn(s + ".in"); setOut(s + ".out"); return; } template <class t> inline bool ckmin(t &x, t y) { if (x > y) { x = y; return 1; } return 0; } template <class t> inline bool ckmax(t &x, t y) { if (x < y) { x = y; return 1; } return 0; } inline long long lowbit(long long x) { return x & (-x); } const long long MaxN = 5050; long long a[MaxN], k[MaxN], f[MaxN], n; inline void check() { for (long long i = 1; i < n; ++i) { if (a[i] % a[i + 1]) return; if (k[i + 1] - k[i] != 1 && k[i + 1] > 0) return; } std::puts("0"), exit(0); } signed main(void) { read(n); for (long long i = 1; i <= n; ++i) { register long long x; read(x); while (!(x & 1)) x >>= 1, ++k[i]; a[i] = x; } check(); for (long long i = 1; i <= n; ++i) f[i] = i - 1; for (long long i = 1; i <= n; ++i) for (long long j = 0; j < i; ++j) if (!(a[j] % a[i]) && (k[i] - k[j] == i - j || k[i] <= i - j - 1)) ckmin(f[i], f[j] + i - j - 1); register long long ans = n - 1; for (long long i = 1; i <= n; ++i) ckmin(ans, f[i] + n - i); ckmin(ans, f[n]); write(ans), std::puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 5005; long long a[N], p[N], q[N]; long long n, f[N]; long long OK(long long x, long long y) { if (q[x] % q[y]) return 0; return p[y] - p[x] == y - x || p[y] <= y - x - 1; } int main() { scanf("%I64d", &n); for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (long long i = 1; i <= n; i++) { p[i] = 0; while (a[i] % 2 == 0) p[i]++, a[i] >>= 1; q[i] = a[i]; } for (long long i = 1; i <= n; i++) { f[i] = i - 1; for (long long j = 1; j < i; j++) if (OK(j, i)) { f[i] = min(f[i], f[j] + i - j - 1); } } long long ans = n; for (long long i = 1; i <= n; i++) ans = min(ans, f[i] + n - i); printf("%I64d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int M = 5010; const int mod = 1000000007; const double eps = 1e-8; const double Pi = 2 * acos(0.0); int n, dp[M], b[M]; long long a[M]; int main() { ios::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { b[i]++; a[i] /= 2; } } int ans = n; dp[0] = 1; for (int i = 0; i < n; i++) { dp[i] = i; for (int j = i - 1; j >= 0; j--) if (a[j] % a[i] == 0 && (b[j] + i - j == b[i] || b[i] <= i - j - 1)) dp[i] = min(dp[j] + i - j - 1, dp[i]); ans = min(ans, n - i - 1 + dp[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long v1[5010], v2[5010], g[5010]; int bst[5010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> g[i]; int rsp = n; for (int i = n - 1; i >= 0; i--) { v2[i] = 0; long long tmp = g[i]; while ((tmp & 1LL) == 0) tmp >>= 1LL, v2[i]++; v1[i] = tmp; bst[i] = n - i - 1; for (int j = i + 1; j < n; j++) { int dif = j - i; if (v1[i] % v1[j] == 0 && (v2[j] < dif || v2[j] == v2[i] + dif)) bst[i] = min(bst[i], bst[j] + dif - 1); } rsp = min(rsp, bst[i] + i); } cout << rsp << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long a[5010]; int pw2[5010]; int ans = 1e9; bool check(int x, int y) { if (a[x] % a[y] != 0LL) return false; if (pw2[y] - (y - x - 1) <= 0) return true; return (pw2[x] + 1 == pw2[y] - (y - x - 1)); } int dp[5010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", a + i); while (a[i] % 2LL == 0LL) { a[i] /= 2LL; pw2[i]++; } } for (int i = 1; i <= n; i++) { dp[i] = i - 1; for (int j = 1; j < i; j++) { if (!check(j, i)) continue; dp[i] = min(dp[i], dp[j] + (i - j - 1)); } ans = min(ans, dp[i] + n - i); } printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long T = 5005; long long N, j, i; long long B[T], a, A[T]; long long ans, dp[T], deg[T]; bool check(long long a, long long b) { if (B[a] % B[b] > 0) return 0; if (deg[a] + b - a == deg[b]) return 1; if (deg[b] <= b - a - 1) return 1; return 0; } int main() { scanf("%I64d", &N); for (i = 1; i <= N; i++) scanf("%I64d", &A[i]); for (i = 1; i <= N; i++) { a = A[i]; while (a % 2LL == 0LL) { deg[i]++; a /= 2; } B[i] = a; } dp[1] = 1; for (i = 2; i <= N; i++) { dp[i] = 1; for (j = 1; j < i; j++) if (check(j, i)) dp[i] = max(dp[j] + 1, dp[i]); } ans = 0; for (i = 1; i <= N; i++) ans = max(ans, dp[i]); cout << N - ans << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5000; int n, p[maxn], f[maxn]; long long q[maxn]; inline bool check(int x, int y) { if (q[x] % q[y]) return false; if (p[y] - p[x] == y - x || p[y] <= y - x - 1) return true; return false; } int main() { scanf("%d", &n); int ans = n; for (int i = 0; i < n; ++i) { long long x; scanf("%I64d", &x); p[i] = __builtin_ctzll(x); q[i] = x >> p[i]; } for (int i = 0; i < n; ++i) { f[i] = i; for (int j = 0; j < i; ++j) { if (!check(j, i)) continue; f[i] = min(f[i], f[j] + i - j - 1); } ans = min(ans, f[i] + n - i - 1); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; inline void chkmax(int &a, int b) { if (b > a) a = b; } int n, dp[5010], c2[5010]; long long a[5010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) while (a[i] % 2 == 0) c2[i]++, a[i] /= 2; for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) if ((c2[i] - c2[j] == i - j || c2[i] < i - j) && a[j] % a[i] == 0) chkmax(dp[i], dp[j] + 1); } int ans = n - *max_element(dp + 1, dp + n + 1); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int N, dp[5005], V[5005]; long long F[5005]; int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%I64d", &F[i]); while ((F[i] & 1) == 0) { F[i] /= 2; V[i]++; } } dp[0] = 1; int ans = 1; for (int i = 1; i < N; i++) { for (int j = 0; j < i; j++) if (F[j] % F[i] == 0 && (V[j] + i - j == V[i] || V[i] <= (i - j - 1))) dp[i] = max(dp[i], dp[j]); dp[i]++; ans = max(ans, dp[i]); } printf("%d\n", N - ans); }
#include <bits/stdc++.h> using namespace std; long long a[5005]; int p[5005], d[5005]; int main() { for (int n; scanf("%d", &n) != EOF;) { for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); p[i] = 0; for (p[i] = 0; !(a[i] & 1); a[i] >>= 1, p[i]++) ; } int ans = 1; d[1] = 1; for (int i = 2; i <= n; i++) { d[i] = 1; for (int j = 1; j < i; j++) { if (a[j] % a[i] == 0 && (p[j] + i - j == p[i] || p[i] <= i - j - 1)) d[i] = max(d[i], d[j] + 1); } ans = max(ans, d[i]); } printf("%d\n", n - ans); } }
#include <bits/stdc++.h> using namespace std; const int maxN = 5100; int dp[maxN], n; long long a[maxN]; int p[maxN]; bool can(int i, int j) { int x = p[i]; int y = p[j] - (j - i - 1); if (y < 0) y = 0; return ((x == y - 1 && y > 0) || y == 0) && a[i] % a[j] == 0; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; ++p[i]; } } for (int i = 1; i <= n; ++i) { dp[i] = i - 1; for (int j = 1; j < i; ++j) { if (can(j, i)) { dp[i] = min(dp[i], dp[j] + i - j - 1); } } } int res = n - 1; for (int i = 1; i <= n; ++i) { res = min(res, dp[i] + n - i); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 5000 + 100; long long a[maxn], p2[maxn], dp[maxn]; long long n, ans; int main() { cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; p2[i]++; } } for (long long i = n - 1; i >= 0; i--) { if (i != 0 && a[i] <= 0) continue; for (long long j = i + 1; j < n; j++) if (a[i] % a[j] == 0 && (p2[j] - p2[i] == j - i || p2[j] <= j - i - 1)) dp[i] = max(dp[i], dp[j]); dp[i]++; ans = max(ans, dp[i]); } cout << n - ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long num[5005]; int two[5005]; int dp[5005]; int main() { int n; scanf("%d", &n); for (int i = (1); i <= (n); i++) { scanf("%lld", &num[i]); while (~num[i] & 1) ++two[i], num[i] >>= 1; } int mn = n - 1; for (int i = (1); i <= (n); i++) { dp[i] = 1; for (int j = (1); j <= (i - 1); j++) { if (two[i] && !(two[i] < i - j || two[i] - two[j] == i - j) || num[j] % num[i] != 0) continue; dp[i] = max(dp[i], dp[j] + 1); } mn = min(mn, n - dp[i]); } printf("%d\n", mn); return 0; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 5010; long long a[MaxN], k; int n, i, j, f[MaxN]; bool v[MaxN][MaxN]; int main() { cin >> n; for (i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (i = n; i; i--) { k = a[i]; for (j = i - 1; j; j--) if (k & 1) { if (a[j] % k == 0) v[j][i] = 1; } else { k /= 2; if (a[j] % k == 0 && (a[j] / k & 1)) v[j][i] = 1; } v[0][i] = 1; v[i][n + 1] = 1; } memset(f, 0x3f, sizeof(f)); f[0] = 0; for (i = 1; i <= n + 1; i++) for (j = 0; j < i; j++) if (v[j][i]) f[i] = min(f[i], f[j] + i - j - 1); cout << f[n + 1] << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5010; int n; int a[maxn], f[maxn]; long long b[maxn]; int main() { int i, ans = 0, j; long long w; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%I64d", &w); while (w % 2 == 0) ++a[i], w /= 2; b[i] = w; } for (i = 1; i <= n; ++i) { for (j = 1; j < i; ++j) if ((a[i] == a[j] + i - j || a[i] < i - j) && b[j] % b[i] == 0) f[i] = max(f[i], f[j]); ans = max(ans, ++f[i]); } printf("%d", n - ans); return 0; }
#include <bits/stdc++.h> using namespace std; int dp[5005][5005], precal[5005]; long long ara[5005]; int solve(int now, int prv) { if (now < 0) return 0; int &ret = dp[now][prv]; if (ret != -1) return ret; long long prvval = (ara[prv] >> min(prv - now - 1, precal[prv])); ret = 1 + solve(now - 1, prv); if ((prvval % 2 && ara[now] % prvval == 0) || (prvval % 2 == 0 && ara[now] % prvval == (prvval >> 1))) ret = min(ret, solve(now - 1, now)); return ret; } int main() { int i, n; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%I64d", &ara[i]); precal[i] = __builtin_ctzll(ara[i]); } memset(dp, -1, sizeof(dp)); int ans = solve(n - 2, n - 1); precal[n - 1] = 0; ara[n - 1] = 1; memset(dp, -1, sizeof(dp)); ans = min(ans, 1 + solve(n - 2, n - 1)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5003; int n; long long a[MAXN]; void read() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; } pair<int, bool> dp[MAXN][MAXN]; int cnt[MAXN]; int calc(int pos, int last) { if (pos == n) return 0; if (dp[pos][last].second) return dp[pos][last].first; dp[pos][last].second = true; dp[pos][last].first = calc(pos + 1, last) + 1; if (cnt[pos + 1] == 0 && a[last] % a[pos + 1] == 0) dp[pos][last].first = min(dp[pos][last].first, calc(pos + 1, pos + 1)); if (cnt[pos + 1] > 0) { int cnt2 = 1 + pos - last + cnt[last]; int curr_cnt2 = cnt[pos + 1]; if (a[last] % a[pos + 1] == 0) if ((curr_cnt2 == cnt2 || curr_cnt2 <= pos - last)) dp[pos][last].first = min(dp[pos][last].first, calc(pos + 1, pos + 1)); } return dp[pos][last].first; } void solve() { int ans = INT_MAX; for (int i = 1; i <= n; i++) while (a[i] % 2ll == 0) cnt[i]++, a[i] /= 2ll; for (int i = 1; i <= n; i++) ans = min(ans, calc(i, i) + i - 1); cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); read(); solve(); }
#include <bits/stdc++.h> using namespace std; const long long oo = 1e18 + 7, mod = 1e9 + 7; const long long N = 5005; long long n, a[N], dp[N], ans = oo, pw2[N]; signed main() { ios_base::sync_with_stdio(0); cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= n; i++) { long long x = a[i]; while (!(x & 1)) { pw2[i]++; x /= 2; } } ans = n; for (long long i = 2; i <= n; i++) { dp[i] = oo; long long temp = a[i]; for (long long j = i - 1; j >= 0; j--) { if (!pw2[i]) { if (!(a[j] % a[i])) dp[i] = min(dp[i], dp[j] + (i - j - 1)); } if ((pw2[j] + (i - j) != pw2[i]) && ((i - j) < pw2[i])) continue; if ((i - j) == pw2[i] && pw2[j]) continue; long long temp1 = (a[j] / (1LL << pw2[j])), temp2 = (a[i] / (1LL << pw2[i])); if (!(temp1 % temp2)) dp[i] = min(dp[i], dp[j] + (i - j - 1)); } dp[i] = min(dp[i], i - 1); ans = min(ans, dp[i] + (n - i)); } cout << ans; }
#include <bits/stdc++.h> using namespace std; const int MAXN = (1 << 20); int64_t n, a[MAXN], p2[MAXN], dp[MAXN]; void read() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while (a[i] % 2 == 0) a[i] /= 2, p2[i]++; } } void solve() { int64_t res = n - 1; dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = i - 1; for (int j = i - 1; j > 0; j--) { if (!(a[j] % a[i] == 0)) continue; if (p2[i] - p2[j] == i - j || p2[i] < i - j) dp[i] = min(dp[i], i - j - 1 + dp[j]); } res = min(res, dp[i] + (n - i)); } cout << res << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
#include <bits/stdc++.h> const int N = 5005; long long a[N], cnt[N], dp[N]; bool ok(int j, int i) { if (a[j] % a[i]) return false; if (cnt[i] == 0) return true; else { if (cnt[i] - cnt[j] == i - j) return true; if (cnt[i] < i - j) return true; return false; } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); while (a[i] % 2 == 0) { a[i] /= 2; cnt[i]++; } } a[n] = 1; dp[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = i; for (int j = 0; j < i; j++) { if (ok(j, i)) dp[i] = std::min(dp[i], dp[j] + i - j - 1); } } printf("%d\n", dp[n]); return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long neg = 1, num = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') neg = -1; for (; isdigit(c); c = getchar()) num = (num << 1) + (num << 3) + c - '0'; return neg * num; } int n, b[200010], f[200010], ans; long long a[200010]; int main() { n = read(); for (int i = 1; i <= n; i++) { a[i] = read(); while (a[i] % 2 == 0) b[i]++, a[i] /= 2; } for (int i = 1; i <= n; i++) { f[i] = 1; for (int j = 1; j < i; j++) { if (a[j] % a[i] == 0 && (b[j] - j == b[i] - i || b[i] <= i - j - 1)) f[i] = max(f[i], f[j] + 1); } ans = max(ans, f[i]); } printf("%d", n - ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 1; const int INF = 1e9 + 9; const int B = 1e9 + 7; int n, b[N], d[N]; long long a[N]; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cin >> n; int ans = n; for (int i = int(1); i <= int(n); ++i) { cin >> a[i]; while (a[i] % 2 == 0) a[i] /= 2, ++b[i]; d[i] = i - 1; for (int j = int(1); j <= int(i - 1); ++j) if (a[j] % a[i] == 0 && (b[j] + (i - j) == b[i] || b[i] <= i - j - 1 || b[i] == 0)) d[i] = min(d[i], d[j] + (i - j - 1)); ans = min(ans, d[i] + (n - i)); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5100; long long a[maxn]; long long b[maxn]; long long n; long long dp[maxn]; long long tav2(long long i) { if (i % 2) return 0; return tav2(i / 2) + 1; } long long ans; int main() { ios_base::sync_with_stdio(false); cin >> n; for (long long y = 0; y < n; y++) cin >> a[y], b[y] = tav2(a[y]), a[y] /= (1LL * (1 << b[y])); a[n] = 1; for (long long y = n - 1; y > -1; y--) { for (long long x = y + 1; x <= n; x++) { if (a[y] % a[x] == 0 && (x - y == b[x] - b[y] || b[x] < x - y)) dp[y] = max(dp[y], dp[x] + 1); } ans = max(ans, dp[y]); } cout << n - ans; }
#include <bits/stdc++.h> using namespace std; long long dp[5010]; long long n; long long a[5010]; long long hs[5010]; int main() { memset(dp, -1, sizeof dp); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; hs[i]++; } } dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = i - 1; for (int j = 1; j < i; j++) { if (a[j] % a[i] == 0 && dp[j] != -1 && (hs[j] + i - j == hs[i] || (i - j - 1 >= hs[i]))) { dp[i] = min(dp[j] + i - j - 1, dp[i]); } } } long long ans = n; for (int i = 1; i <= n; i++) { if (dp[i] != -1) { ans = min(ans, dp[i] + n - i); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[6000]; pair<long long, int> a[5111]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i].first; for (int i = 1; i <= n; i++) { while (a[i].first % 2 == 0) { a[i].second++; a[i].first /= 2; } } dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = i - 1; for (int j = 1; j < i; j++) { if (a[j].first % a[i].first == 0) if (((a[i].second - a[j].second <= i - j) && ((a[j].second - j == a[i].second - i) || a[j].second == 0)) || (a[i].second == 0) || (a[i].second <= i - j - 1)) { dp[i] = min(dp[j] + (i - j - 1), dp[i]); } } } int ans = 100000; for (int i = 1; i <= n; i++) { ans = min(ans, dp[i] + n - i); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int inv = numeric_limits<int>::max(); const int minv = numeric_limits<int>::min(); const int max_n = 5010; int n; long long A[max_n]; int P[max_n]; int dp[max_n]; bool cm(int j, int i) { if (A[j] % A[i]) return false; if (i - j <= P[i]) return (P[i] - P[j] == i - j); return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { cin >> A[i]; while ((A[i] % (2ll)) == 0) { A[i] /= (2ll); ++P[i]; } } dp[0] = 1; int res = n - 1; for (int i = 1; i < n; ++i) { dp[i] = 1; for (int j = 0; j < i; ++j) { if (cm(j, i)) dp[i] = max(dp[i], dp[j] + 1); } res = min(res, n - dp[i]); } printf("%d\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, ans, a[5050], pw[5050], dp[5050]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; pw[i]++; } } for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) { if (a[j] % a[i] == 0 && (pw[i] < i - j || pw[j] == pw[i] - i + j)) { dp[i] = max(dp[i], dp[j] + 1); } } ans = max(ans, dp[i]); } cout << n - ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; inline int ___INT() { int ret; scanf("%d", &ret); return ret; } int n; long long A[5005]; int dp[5005]; int cnt2(long long& x) { int ret = 0; while (!(x & 1)) { ++ret; x >>= 1; } return ret; } bool can(long long src, long long dest, int steps) { int cnt2src = cnt2(src), cnt2dest = cnt2(dest); if (src % dest != 0) return false; if (cnt2dest - cnt2src == steps) return true; if (cnt2dest + 1 <= steps) return true; return false; } int main() { n = ___INT(); for (int i = 0; i < (n); ++i) { cin >> A[i]; } int sub_ans = 1; for (int i = n - 1; i >= 0; --i) { int steps = 1; dp[i] = 1; for (int j = i + 1; j < n; ++j) { if (can(A[i], A[j], steps)) { dp[i] = max(dp[i], dp[j] + 1); } ++steps; } sub_ans = max(sub_ans, dp[i]); } printf("%d\n", n - sub_ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, set<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class L, class R> ostream& operator<<(ostream& os, map<L, R> M) { os << "{ "; for (auto m : M) os << "(" << m.F << ":" << m.S << ")"; return os << "}"; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int N = 5e3 + 100; int dp[N]; long long A[N]; int ep[N]; int n; void pre() { for (int i = 1; i <= n; ++i) { while (A[i] % 2 == 0) { A[i] /= 2; ++ep[i]; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(25); cin >> n; for (int i = 1; i <= n; ++i) cin >> A[i]; reverse(A + 1, A + n + 1); pre(); int mx = 0; dp[1] = 1; for (int i = 2; i <= n; ++i) { dp[i] = 1; for (int j = 1; j < i; ++j) { if (A[i] % A[j] == 0 && (ep[j] < i - j || ep[j] - ep[i] == i - j)) { dp[i] = max(dp[i], dp[j] + 1); } } mx = max(mx, dp[i]); } cout << n - mx << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e4 + 7; int n, v[N], dp[N]; long long a[N], f[N]; int main() { cin >> n; for (int i = (1); i < (n + 1); ++i) { cin >> a[i]; for (int j = 59; j >= 0; --j) if (a[i] % (1ll << j) == 0) { v[i] = j; break; } f[i] = a[i] / (1ll << v[i]); } int ans = 1; dp[n] = 1; for (int i = n - 1; i > 0; --i) { dp[i] = 1; for (int j = (i + 1); j < (n + 1); ++j) { if (f[i] % f[j]) continue; if (v[i] + j - i == v[j] || v[j] <= j - i - 1) dp[i] = max(dp[i], dp[j] + 1); } ans = max(ans, dp[i]); } cout << n - ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int i, j, k, l, r, x, y; string s; int N, dp[5010]; signed long long A[5010]; cin >> N; int ret = N; for (x = 0; x < N; x++) { cin >> A[x]; dp[x] = x; signed long long X = A[x]; for (y = x - 1; y >= 0; y--) { if (X % 2 == 0 && (A[y] % X) == X / 2) dp[x] = min(dp[x], dp[y] + x - y - 1); if (X % 2 == 1 && (A[y] % X) == 0) dp[x] = min(dp[x], dp[y] + x - y - 1); if (X % 2 == 0) X >>= 1; } ret = min(ret, dp[x] + (N - (x + 1))); } cout << ret << endl; } int main(int argc, char** argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false); for (i = 0; i < argc - 1; i++) s += argv[i + 1], s += '\n'; for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int dp[5005], B[5050], i, n, ans, j; long long A[5050], X; bool OK(int x, int y) { if (A[x] % A[y] != 0) return false; if (B[x] + y - x == B[y]) return true; if (y - x - 1 >= B[y]) return true; return false; } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%I64d", &A[i]); X = A[i]; B[i] = 0; while (X % 2 == 0 && X) { X /= 2; B[i]++; } A[i] = X; } ans = n; for (i = 1; i <= n; i++) { dp[i] = i - 1; for (j = 1; j < i; j++) if (OK(j, i)) dp[i] = min(dp[i], dp[j] + i - j - 1); ans = min(ans, dp[i] + n - i); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[6000]; long long A[6000]; int stepA[6000]; long long stepOdd[6000]; int isPossible(int start, int end) { if (A[start] % 2 == 1 && (A[end] % A[start] == 0)) return 1; if (A[start] % 2 == 0) { long long a = A[start]; int required = end - start; if (required <= stepA[start]) { a = (a >> required); if ((A[end] % a) == 0 && ((A[end] / a) % 2) == 1) { return 1; } return 0; } if ((A[end] % stepOdd[start]) == 0) return 1; } return 0; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> A[i]; } reverse(A + 1, A + n + 1); for (int i = 1; i <= n; i++) { long long a = A[i]; while (a % 2 == 0) { stepA[i]++; a /= 2; } stepOdd[i] = a; } dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = i - 1; } for (int i = 2; i <= n; i++) { for (int j = 1; j <= i - 1; j++) { if (isPossible(j, i)) { dp[i] = min(dp[i], dp[j] + (i - j - 1)); } } } int ret = 10000; for (int i = 1; i <= n; i++) { ret = min(ret, dp[i] + (n - i)); } cout << ret << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m; long long A[200306], pr[200306]; long long dp[200306]; void solve() { cin >> n; for (long long i = (1), iend = (n); i <= iend; ++i) { scanf("%lld", A + i); while (A[i] % 2 == 0) A[i] /= 2, ++pr[i]; } long long re = n + 1; for (long long i = (1), iend = (n); i <= iend; ++i) { dp[i] = i - 1; for (long long j = (1), jend = (i - 1); j <= jend; ++j) { if (A[j] % A[i] == 0 && (pr[i] < i - j || pr[i] - pr[j] == i - j)) dp[i] = min(dp[i], dp[j] + (i - j - 1)); } re = min(re, dp[i] + n - i); } cout << re << endl; } signed main() { solve(); }
#include <bits/stdc++.h> using namespace std; const int tope = 6000; int n; long long int a[tope]; int minnocambia[tope]; int mintotal[tope]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { minnocambia[i] = i; for (int j = 0; j < i; j++) { long long int val = a[i]; for (int k = i; k > j + 1; k--) if (val % 2 == 0) val /= 2; else break; if (val % 2 == 1) { if (a[j] % val == 0) minnocambia[i] = min(minnocambia[i], minnocambia[j] + i - j - 1); } else { val /= 2; if (a[j] % val == 0 and a[j] / val % 2 == 1) minnocambia[i] = min(minnocambia[i], minnocambia[j] + i - j - 1); } } if (i > 0) mintotal[i] = min(mintotal[i - 1] + 1, minnocambia[i]); } cout << mintotal[n - 1] << endl; }
#include <bits/stdc++.h> int dp[5010][2]; long long a[5010]; int main(void) { int n; scanf("%d", &n); dp[0][0] = 0; dp[0][1] = 0; int tmp; long long pa; for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); if (i > 0) { dp[i][0] = dp[i - 1][0] + 1; }; tmp = i; pa = a[i]; for (int j = i - 1; j >= 0; j--) { if (pa % 2ll) { if (a[j] % pa == 0ll) { tmp = std::min(dp[j][1] + i - j - 1, tmp); } } else { if ((a[j] + (pa >> 1ll)) % pa == 0ll) { tmp = std::min(dp[j][1] + i - j - 1, tmp); pa = (pa >> 1ll); } else { pa = (pa >> 1ll); } }; }; dp[i][0] = std::min(dp[i][0], tmp); dp[i][1] = tmp; }; printf("%d\n", dp[n - 1][0]); return 0; };
#include <bits/stdc++.h> using namespace std; const int N = 5100; int n, d[N], p2[N]; long long a[N], f[N]; bool ver(int a, int b) { if (a == 0) return 1; if (f[a] % f[b] == 0 && (p2[a] + b - a == p2[b] || p2[b] <= b - a - 1)) return 1; return 0; } int main() { int i; cin >> n; for (i = 1; i <= n; ++i) { cin >> a[i]; long long t = a[i]; while (t % 2 == 0) { p2[i]++; t /= 2; } f[i] = t; } d[1] = 1; int smax = 0; for (i = 2; i <= n; ++i) { d[i] = 1; for (int j = 0; j < i - 1; ++j) if (ver(j + 1, i)) d[i] = max(d[i], d[j + 1] + 1); smax = max(smax, d[i]); } cout << n - smax; return 0; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 5010; int n; long long a[MaxN]; int F[MaxN]; bool u[MaxN][MaxN]; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = n; i >= 1; --i) { long long k = a[i]; for (int j = i - 1; j >= 1; --j) { if (k % 2 == 0) { k /= 2; if (a[j] % k == 0 && a[j] / k % 2 == 1) u[j][i] = 1; } else if (a[j] % k == 0) u[j][i] = 1; } u[0][i] = 1; u[i][n + 1] = 1; } for (int i = 1; i <= n + 1; ++i) { F[i] = MaxN; for (int j = 0; j < i; ++j) if (u[j][i]) F[i] = min(F[i], F[j] + i - j - 1); } cout << F[n + 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5005; int f[maxn], n; long long a[maxn], p[maxn], q[maxn]; inline bool check(int x, int y) { if (q[x] % q[y]) return 0; return p[y] - p[x] == y - x || p[y] <= y - x - 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); while (!(a[i] & 1)) { p[i]++; a[i] >>= 1; } q[i] = a[i]; } for (int i = 1; i <= n; i++) { f[i] = i - 1; for (int j = 1; j < i; j++) if (check(j, i)) f[i] = min(f[i], f[j] + i - j - 1); } int ans = 0x3f3f3f3f; for (int i = 1; i <= n; i++) ans = min(ans, f[i] + n - i); printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; int n; long long a[5000 + 5]; int dp[5000 + 5]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); } for (int i = n - 1; i >= 0; i--) dp[i] = n - i - 1; int ans = n; for (int i = n - 1; i >= 0; i--) { long long _t = a[i]; ans = min(ans, dp[i] + i); int cc = 0; for (int j = i - 1; j >= 0; j--) { int flag = 0; if (_t % 2 == 0) { if (a[j] % _t == _t / 2) flag = 1; _t /= 2; } else { if (a[j] % _t == 0) { flag = 1; } } if (flag) { dp[j] = min(dp[i] + cc, dp[j]); } cc++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 5005; int N, p2[MAX], dp[MAX]; long long A[MAX]; int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; p2[i] = 0; while (A[i] % 2 == 0) { p2[i]++; A[i] /= 2; } } dp[0] = 0; for (int i = 1; i < N; i++) { dp[i] = i; for (int j = 0; j < i; j++) { if (A[j] % A[i] == 0) if (p2[i] < i - j or p2[i] - p2[j] == i - j) dp[i] = min(dp[i], dp[j] + (i - j - 1)); } } int res = MAX; for (int i = 0; i < N; i++) { res = min(res, dp[i] + N - 1 - i); } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } template <typename T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T, class T1> inline void gn(T &first, T1 &second) { gn(first); gn(second); } template <class T, class T1, class T2> inline void gn(T &first, T1 &second, T2 &z) { gn(first); gn(second); gn(z); } template <typename T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <typename T> inline void println(T first) { print(first), putchar('\n'); } template <typename T> inline void printsp(T first) { print(first), putchar(' '); } template <class T, class T1> inline void print(T first, T1 second) { printsp(first), println(second); } template <class T, class T1, class T2> inline void print(T first, T1 second, T2 z) { printsp(first), printsp(second), println(z); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } long long a[5050]; int b[5050]; int dp[5050]; int main() { int n; gn(n); for (int i = 1; i <= n; i++) { gn(a[i]); while (~a[i] & 1) a[i] >>= 1, b[i]++; } for (int i = 1; i <= n; i++) { dp[i] = i - 1; for (int j = 1; j < i; j++) { if (a[j] % a[i] == 0) { if (i - j > b[i] || i - j == b[i] - b[j]) smin(dp[i], dp[j] + i - j - 1); } } } int ans = n - 1; for (int i = 1; i <= n; i++) smin(ans, dp[i] + n - i); println(ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200010; const int mod = 1000000007; long long a[N], b[N], c[N]; int dp[N]; int main() { int n, q; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); long long x = a[i]; while (x % 2 == 0) b[i]++, x /= 2; c[i] = x; } dp[0] = 0; for (int i = 1; i <= n; i++) dp[i] = 100000; for (int i = 1; i <= n; i++) { dp[i] = min(dp[i], (dp[0] + (i - 1))); for (int j = 1; j < i - 1; j++) { if ((b[i] == 0 || (b[i] - b[j]) == i - j || b[i] <= i - j - 1) && (c[j] % c[i] == 0)) { dp[i] = min(dp[i], dp[j] + (i - j - 1)); } } if ((b[i] == 0 || (b[i] - b[i - 1]) == 1) && (c[i - 1] % c[i] == 0)) { dp[i] = min(dp[i], dp[i - 1]); } } int ans = 1000000; for (int i = 1; i <= n; i++) { ans = min(ans, dp[i] + (n - i)); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int dp[5010], p[5010]; long long a[5010], b[5010]; bool check(int i, int j) { if (a[j] & 1) { return (a[i] % a[j] == 0); } else { if (p[j] == j - i + p[i] && b[i] % b[j] == 0) return true; else return (p[j] <= j - i - 1) && (b[i] % b[j] == 0); } } int main() { int n, i, j; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%I64d", &a[i]); b[i] = a[i]; while (b[i] % 2 == 0) { b[i] /= 2; p[i]++; } } int res = 0; for (i = 1; i <= n; i++) { dp[i] = 1; for (j = 1; j < i; j++) { if (check(j, i)) { dp[i] = max(dp[j] + 1, dp[i]); } } res = max(res, dp[i]); } printf("%d\n", n - res); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5001; const int mod = 1000000009; int dp[N], v[N]; long long A[N]; int main() { int n, res; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%I64d", A + i); dp[i] = 1; while (!(A[i] & 1)) { A[i] >>= 1; ++v[i]; } } res = n - 1; for (int i = n - 1; i >= 1; --i) { for (int j = i + 1; j <= n; ++j) { if (A[i] % A[j] == 0 && (v[j] - v[i] == j - i || v[j] + 1 <= j - i)) { dp[i] = max(dp[i], dp[j] + 1); } } res = min(res, n - dp[i]); } printf("%d\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5010; int n; int a[N], f[N]; long long b[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { long long x; scanf("%lld", &x); while (x % 2 == 0) x /= 2, a[i]++; b[i] = x; } int ans = n; for (int i = 1; i <= n; i++) { f[i] = 1; for (int j = 1; j < i; j++) if ((a[i] <= i - j - 1 || a[i] - a[j] == i - j) && b[j] % b[i] == 0) f[i] = max(f[i], f[j] + 1); ans = min(ans, n - f[i]); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; namespace IO { const int MAXIOSIZE = 1 << 24 | 1; unsigned char buf[MAXIOSIZE], *p1, *p2; template <typename T> void read(T& x) { x = 0; char ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++); bool flg = false; for (; ch < '0' || '9' < ch; ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)) if (ch == '-') flg |= true; for (; '0' <= ch && ch <= '9'; ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)) x = x * 10 + ch - '0'; flg ? x = -x : 0; } template <typename T> void out(const T& x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); } template <typename T> inline void write(const T& x, const char& ed = ' ') { if (x < 0) putchar('-'), out(-x); else out(x); putchar(ed); } } // namespace IO const int MAXN = 5000 + 10; const int MAXLOG = 50 + 10; int n, f[MAXN]; long long a[MAXN], pow2[MAXN], e2[MAXN], odd[MAXN]; int main() { IO::read(n); pow2[0] = 1; for (int i = (1), iend = (50); i <= iend; ++i) pow2[i] = pow2[i - 1] * 2; for (int i = (1), iend = (n); i <= iend; ++i) { IO::read(a[i]); for (int j = (50), jend = (0); j >= jend; --j) { if (a[i] % pow2[j] == 0) { e2[i] = j, odd[i] = a[i] / pow2[j]; break; } } } int ans = 0; for (int i = (1), iend = (n); i <= iend; ++i) { f[i] = 1; for (int j = (1), jend = (i - 1); j <= jend; ++j) { if (odd[j] % odd[i] == 0 && (e2[j] == e2[i] - i + j || i - j > e2[i])) { f[i] = max(f[i], f[j] + 1); } } ans = max(ans, f[i]); } IO::write(n - ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T BigMod(T b, T p, T m) { if (p == 0) return 1; if (p % 2 == 0) { T s = BigMod(b, p / 2, m); return ((s % m) * (s % m)) % m; } return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; } template <typename T> T ModInv(T b, T m) { return BigMod(b, m - 2, m); } template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { n = n * 10 + (ch - '0'); ch = getchar(); if (ch < '0' || ch > '9') break; } return (ng ? -n : n); } template <typename T> T POW(T B, T printf) { if (printf == 0) return 1; if (printf & 1) return B * POW(B, printf - 1); else return (POW(B, printf / 2) * POW(B, printf / 2)); } template <typename T> T Bigmod(T b, T p, T m) { if (p == 0) return 1; else if (!(p & 1)) return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m; else return ((b % m) * Bigmod(b, p - 1, m)) % m; } template <typename T> T Dis(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); } template <typename T> T Angle(T x1, T y1, T x2, T y2) { return atan(double(y1 - y2) / double(x1 - x2)); } template <typename T> T DIFF(T a, T b) { T d = a - b; if (d < 0) return -d; else return d; } template <typename T> T ABS(T a) { if (a < 0) return -a; else return a; } template <typename T> T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <typename T> T lcm(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } template <typename T> T euclide(T a, T b, T &x, T &y) { if (a < 0) { T d = euclide(-a, b, x, y); x = -x; return d; } if (b < 0) { T d = euclide(a, -b, x, y); y = -y; return d; } if (b == 0) { x = 1; y = 0; return a; } else { T d = euclide(b, a % b, x, y); T t = x; x = y; y = t - (a / b) * y; return d; } } template <typename T> void ia(T a[], int n) { for (int i = 0; i < n; i++) cin >> a[i]; } template <typename T> void pa(T a[], int n) { for (int i = 0; i < n - 1; i++) cout << a[i] << " "; cout << a[n - 1] << endl; } template <typename T> long long int isLeft(T a, T b, T c) { return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); } int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } template <class T, class first> inline T togglebit(T a, first i) { T t = 1; return (a ^ (t << i)); } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } int toLlint(string s) { long long int sm; stringstream ss(s); ss >> sm; return sm; } int cdigittoint(char ch) { return ch - '0'; } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); } double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); } long long int f[5007], pow2[5007]; long long int a[5007]; int DP[5007]; int main() { int(n); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); long long int x = a[i]; while (!(x & 1)) { pow2[i]++; x >>= 1; } f[i] = x; } int ans = 0; for (int i = n; i >= 1; i--) { DP[i] = 1; for (int j = i + 1; j <= n; j++) { if (f[i] % f[j] == 0 && (pow2[i] + j - i == pow2[j] || pow2[j] < j - i)) DP[i] = max(DP[i], 1 + DP[j]); } ans = max(ans, DP[i]); } cout << n - ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; long long a[5000]; int n, dp[5000], two[5000]; int main() { cin >> n; for (int i = 0; i < (int)n; i++) { cin >> a[i]; two[i] = __builtin_ctzll(a[i]); } int ans = n; for (int i = 0; i < (int)n; i++) { dp[i] = i; for (int j = 0; j < (int)i; j++) { long long c = a[i] >> min(two[i], i - j - 1); if (c % 2 == 0 && a[j] % c == c / 2 || c % 2 && a[j] % c == 0) dp[i] = min(dp[i], dp[j] + i - j - 1); } ans = min(ans, dp[i] + n - i - 1); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> int max(int a, int b) { return a < b ? b : a; } long long num[5005]; int dp[5005] = {}, bit[5005] = {}, n, i, j, ret = 0; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%lld", &num[i]); while (num[i] % 2 == 0) { bit[i]++; num[i] /= 2; } } for (i = 1; i <= n; i++) { for (j = 0; j < i; j++) { if (!j) { dp[i] = 1; continue; } int dif = bit[i] - bit[j]; if (num[j] % num[i] == 0 && ((i - j) == dif || (i - j) > bit[i])) { dp[i] = max(dp[i], dp[j] + 1); } } ret = max(ret, dp[i]); } return !printf("%d", n - ret); }
#include <bits/stdc++.h> using namespace std; int n; int p[10000], dp[10000]; long long a[10000]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; p[i]++; } } for (int i = 0; i < n; i++) { dp[i] = 1; for (int j = 0; j < i; j++) { if (a[j] % a[i] != 0) continue; if ((i - j <= p[i]) && ((i - j) != p[i] - p[j])) continue; dp[i] = max(dp[i], dp[j] + 1); } } cout << (n - *max_element(dp, dp + n)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool Up(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool Down(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } inline int getus() { int tmp, c; while (tmp = fgetc(stdin), tmp < '0' || tmp > '9') ; tmp -= '0'; while (c = fgetc(stdin), '0' <= c && c <= '9') tmp = tmp * 10 + c - '0'; return tmp; } inline int getint() { int tmp, c, flag; while (flag = fgetc(stdin), flag != '-' && (flag < '0' || flag > '9')) ; if (flag == '-') tmp = 0; else tmp = flag - '0'; while (c = fgetc(stdin), '0' <= c && c <= '9') tmp = tmp * 10 + c - '0'; return flag == '-' ? -tmp : tmp; } int N, opt[50023]; long long A[50023], V[50023]; int main() { ios::sync_with_stdio(false); cin >> N; for (int i = (1); i <= (N); ++i) { cin >> A[i]; for (V[i] = 0; !(A[i] & 1); A[i] >>= 1, ++V[i]) ; } int ans = N; opt[0] = 0; for (int i = (1); i <= (N); ++i) { int &res = opt[i] = i - 1; for (int j = (1); j < (i); ++j) if (A[j] % A[i] == 0) if (V[j] - j == V[i] - i || V[i] <= i - j - 1) { Down(res, opt[j] + i - j - 1); } Down(ans, opt[i] + N - i); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5050; int n, ans = 0, f[N] = {}, t[N] = {}; long long a[N] = {}; int main() { cin >> n; ans = n; for (int i = 1; i <= n; ++i) { cin >> a[i]; while (a[i] % 2 == 0) a[i] /= 2, ++t[i]; } for (int i = 1; i <= n; ++i) { f[i] = 1; for (int j = 1; j < i; ++j) if (a[j] % a[i] == 0 && (t[i] + j - i == t[j] || t[i] <= i - j - 1)) f[i] = max(f[i], f[j] + 1); ans = min(ans, n - f[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 5000 + 100; long long a[maxn], p2[maxn], dp[maxn]; long long n, ans; int main() { cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; p2[i]++; } } for (long long i = n - 1; i >= 0; i--) { for (long long j = i + 1; j < n; j++) if (a[i] % a[j] == 0 && (p2[j] - p2[i] == j - i || p2[j] <= j - i - 1)) dp[i] = max(dp[i], dp[j]); dp[i]++; ans = max(ans, dp[i]); } cout << n - ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, a[10100], p[10100], f[10100]; bool Check(int x, int y) { if (a[x] % a[y]) return 0; return p[y] - p[x] == y - x || p[y] < y - x; } int main(void) { scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); while (!(a[i] & 1)) { a[i] >>= 1; p[i]++; } } f[1] = 0; for (int i = 2; i <= n; i++) { f[i] = i - 1; for (int j = 1; j < i; j++) if (Check(j, i)) f[i] = min(f[i], f[j] + i - j - 1); } long long ans = n - 1; for (int i = 2; i <= n; i++) ans = min(ans, n + f[i] - i); printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> const int N = 5000 + 10; long long a[N]; int n, d[N]; int two[N]; bool ok(int i, int j) { if (a[i] % a[j] != 0) return false; if (two[j] == 0) return true; if (two[j] - two[i] == j - i) return true; if (two[j] - two[i] < j - i && j - two[j] > i) return true; if (two[j] - two[i] < j - i && j - two[j] == i && a[i] == a[j] && two[i] == 0) return true; return false; } int main() { std::cin >> n; for (int i = 0; i < n; ++i) { d[i] = i; std::cin >> a[i]; while (a[i] % 2 == 0) { ++two[i]; a[i] /= 2; } } d[n] = n; a[n] = 1; for (int i = 1; i <= n; ++i) for (int j = 0; j < i; ++j) if (ok(j, i)) d[i] = std::min(d[i], d[j] + i - j - 1); std::cout << d[n] << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[5010]; int dp[5010]; bool func(long long x, long long y, int step, int ord) { long long z; if (step <= ord) { z = (x >> step); return (y % (2 * z) == z); } z = (x >> ord); return (y % z == 0); } int main(void) { int N, i, j; cin >> N; for ((i) = 0; (i) < (int)(N); (i)++) cin >> a[i]; for ((i) = 0; (i) < (int)(N); (i)++) { dp[i] = 1; long long tmp = a[i]; int ord = 0; while (tmp % 2 == 0) { tmp /= 2; ord++; } for ((j) = 0; (j) < (int)(i); (j)++) if (func(a[i], a[j], i - j, ord)) dp[i] = max(dp[i], dp[j] + 1); } int ans = 0; for ((i) = 0; (i) < (int)(N); (i)++) ans = max(ans, dp[i]); ans = N - ans; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 5e3 + 5; long long a[maxn], p[maxn], dp[maxn]; int32_t main() { long long n; cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { p[i]++; a[i] /= 2; } } for (long long i = n - 1; i >= 0; i--) { for (long long j = i + 1; j < n; j++) { if (a[i] % a[j] == 0 && (p[j] == p[i] + j - i || j - p[j] > i)) { dp[i] = max(dp[i], dp[j]); } } dp[i]++; } cout << n - *max_element(dp, dp + n); }
#include <bits/stdc++.h> using namespace std; int n, i, j, k, ans; int b[5005], f[5005]; long long a[5005]; bool cal(int x, int y) { int delta = y - x; if (a[x] % a[y] == 0) { if (delta > b[y]) return true; else return b[x] + delta == b[y]; } return false; } int main() { scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%I64d", &a[i]); for (; !(a[i] & 1); a[i] >>= 1) ++b[i]; } ans = n; for (i = 1; i <= n; ++i) { f[i] = i - 1; for (j = 1; j < i; ++j) if (cal(j, i) && f[j] + i - j - 1 < f[i]) f[i] = f[j] + i - j - 1; if (f[i] + n - i < ans) ans = f[i] + n - i; } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; const long long N = 5010; long long ans = 1 << 28, n, seq[N], p2s[N], dp[N]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; n - i; ++i) { cin >> seq[i]; while (!(seq[i] & 1)) { seq[i] /= 2; ++p2s[i]; } dp[i] = i; for (int j = i - 1; j + 1; --j) if (!(seq[j] % seq[i]) && ((p2s[i] <= i - j - 1) || (p2s[i] - p2s[j] == i - j))) dp[i] = min(dp[i], dp[j] + i - j - 1); ans = min(ans, dp[i] + n - i - 1); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 10001; long long a[MAXN]; int f[MAXN]; long long c[MAXN], b[MAXN]; int n; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) { long long X = a[i], cnt = 0; while (!(X & 1)) { ++cnt; X >>= 1; } b[i] = X; c[i] = cnt; } int ans = 0; for (int i = 0; i < n; ++i) { f[i] = 1; for (int j = 0; j < i; ++j) { if (a[j] % a[i] == 0 && a[i] % 2 == 1) f[i] = max(f[i], f[j] + 1); if (a[i] % 2 == 0) { long long t1 = b[j], t2 = b[i]; if (t1 % t2 == 0) { if (c[i] - c[j] == i - j || c[i] <= i - j - 1) f[i] = max(f[i], f[j] + 1); } } } if (f[i] > ans) ans = f[i]; } cout << n - ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long linf = 1e18; const int inf = 1e9; const int N = 1e5 + 5; int n, m, dp[N]; long long a[N], cnt[N]; bool cool(long long x, long long y, long long l) { long long t1 = a[x], t2 = a[y]; t2 = max(t2 / (1ll << l), cnt[y]); if ((t2 % 2 && t1 % t2 == 0) || (t2 % 2 == 0 && 2 * t1 % t2 == 0 && t1 % t2)) return true; return false; } int f(int x) { if (x >= n) return 0; int &r = dp[x]; if (r != -1) return r; r = n - x; for (int i = x + 1; i <= n; i++) if (cool(x, i, i - x - 1)) r = min(r, f(i) + i - x - 1); return r; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= n; i++) { long long x = a[i], s = 0; while (x % 2 == 0) { s++; x /= 2; } cnt[i] = x; } memset(dp, -1, sizeof dp); int ans = inf; for (int i = 1; i <= n; i++) ans = min(ans, f(i) + i - 1); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; int N; long long seq[MAXN]; int dp[MAXN]; void solve() { long long ret = N - 1; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= N; i++) { if (i == 1) { dp[i] = 0; continue; } dp[i] = i - 1; long long y = seq[i]; for (int j = i - 1; j >= 1; j--) { long long x = seq[j]; if (y & 1LL) { if (x % y == 0) dp[i] = min(dp[i], dp[j] + i - j - 1); } else { if (x % y == y / 2LL) dp[i] = min(dp[i], dp[j] + i - j - 1); y >>= 1LL; } } ret = min(ret, dp[i] + ((long long)N - (long long)i)); } cout << ret << endl; } int main(int argc, const char* argv[]) { while (cin >> N) { for (int i = 1; i <= N; i++) { cin >> seq[i]; } solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; ifstream fin("in.in"); ofstream fout("out.out"); const int N = 5000 + 10; int n, dp[N], p2[N], res; long long a[N]; map<long long, int> lg; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); for (int i = 0; i <= 50; i++) lg[1LL << i] = i; cin >> n; res = n; for (int i = 0; i < n; i++) { cin >> a[i]; p2[i] = lg[a[i] & (-a[i])]; a[i] >>= p2[i]; dp[i] = i; for (int j = 0; j < i; j++) if (a[j] % a[i] == 0 && (p2[j] + i == p2[i] + j || p2[i] < i - j)) dp[i] = min(dp[i], dp[j] + i - j - 1); res = min(res, dp[i] + n - i - 1); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int N; long long A[5010]; int cc[5010][5010]; int dp(int n, int p) { if (n == -1) return 0; int &ret = cc[n][p]; if (ret != -1) return ret; long long x = A[p]; if (p - n - 1 > 50) x /= (x & -x); else x /= min(x & -x, 1LL << (p - n - 1)); if (x % 2) { ret = 1 + dp(n - 1, p); if (A[n] % x) return ret; else { ret = min(ret, dp(n - 1, n)); return ret; } } else { ret = 1 + dp(n - 1, p); if (A[n] % x != x / 2) return ret; else { ret = min(ret, dp(n - 1, n)); return ret; } } } int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } A[N] = 1; memset(cc, -1, sizeof(cc)); printf("%d", dp(N - 1, N)); }
#include <bits/stdc++.h> using namespace std; long long const inf = 1e9 + 7, MAXN = 2e5 + 5; long long n, m, tc; long long a[MAXN]; long long dp[MAXN]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; long long mx = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; dp[i] = 1; long long cur = a[i]; for (int j = i - 1; j >= 1; j--) { if (cur % 2 == 1) { if (a[j] % cur == 0) { dp[i] = max(dp[i], dp[j] + 1); } } else { cur /= 2; if (a[j] % cur == 0 && (a[j] / cur) % 2 == 1) { dp[i] = max(dp[i], dp[j] + 1); } } } mx = max(mx, dp[i]); } cout << n - mx; }
#include <bits/stdc++.h> using namespace std; int dp[5000], b[5000]; long long a[5000]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", a + i); while (a[i] % 2 == 0) { b[i]++; a[i] >>= 1; } } int ans = n; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[j] % a[i] == 0 && (b[j] + i - j == b[i] || b[i] < i - j)) dp[i] = max(dp[i], dp[j]); } dp[i]++; ans = min(ans, n - dp[i]); } printf("%d\n", ans); }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:200000000") using namespace std; template <typename T> inline T Abs(T x) { return (x >= 0) ? x : -x; } template <typename T> inline T sqr(T x) { return x * x; } template <typename T> string toStr(T x) { stringstream st; st << x; string s; st >> s; return s; } inline int nextInt() { int x; if (scanf("%d", &x) != 1) throw; return x; } inline long long nextInt64() { long long x; if (scanf("%I64d", &x) != 1) throw; return x; } inline double nextDouble() { double x; if (scanf("%lf", &x) != 1) throw; return x; } const int INF = (int)1E9; const long long INF64 = (long long)1E18; const long double EPS = 1E-9; const long double PI = 3.1415926535897932384626433832795; const int MAXN = 51000; int n, d[MAXN], s[MAXN]; long long a[MAXN], c[MAXN]; bool check(long long n, long long y) { return (2 * n % y == 0 || (n - y * y + y) % (2 * y) == 0); } int main() { n = nextInt(); for (int i = 0; i < (int)(n); i++) { c[i] = a[i] = nextInt64(); while (c[i] % 2 == 0) { c[i] /= 2; s[i]++; } } int ans = INF; memset(d, 60, sizeof d); for (int i = 0; i < (int)(n); i++) { d[i] = min(d[i], i); for (int j = i + 1; j < n; j++) { if (c[i] % c[j] != 0) continue; if (s[i] + j - i == s[j] || s[j] < j - i) d[j] = min(d[j], d[i] + j - i - 1); } ans = min(ans, d[i] + n - i - 1); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); } template <typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); } int n; long long a[5005]; int dp[5005]; bool legal(long long x, long long y, int gap, int cnt) { long long z; if (gap <= cnt) { z = (x >> gap); return (y % (2 * z) == z); } z = (x >> cnt); return (y % z == 0); } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { dp[i] = 1; long long val = a[i]; int cnt = 0; while (val % 2 == 0) { val /= 2; cnt++; } for (int j = 0; j < i; ++j) { if (legal(a[i], a[j], i - j, cnt)) { dp[i] = max(dp[i], dp[j] + 1); } } } int maxv = *max_element(dp, dp + n); int ans = n - maxv; cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void splitstr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } static void redirect(int argc, const char **argv) { if (argc > 1) { static filebuf f; f.open(argv[1], ios::in); cin.rdbuf(&f); if (!cin) { cerr << "Failed to open '" << argv[1] << "'" << endl; exit(1); } } if (argc > 2) { static filebuf f; f.open(argv[2], ios::out | ios::trunc); cout.rdbuf(&f); if (!cout) { cerr << "Failed to open '" << argv[2] << "'" << endl; } } } static bool cool(long long x, long long y) { if (y % 2 == 1) return x % y == 0; else return llabs(x % y) == y / 2; } int main(int argc, const char **argv) { redirect(argc, argv); int N; cin >> N; vector<long long> a(N + 1); for (int i = 0; i < N; i++) cin >> a[i]; a[N] = 1; vector<int> dp(N + 1, INT_MAX / 2); vector<long long> shifted = a; dp[N] = 0; for (int i = N - 1; i >= 0; i--) { int best = INT_MAX / 2; for (int j = i + 1; j <= N; j++) { if (cool(a[i], shifted[j])) best = min(best, dp[j]); if ((shifted[j] & 1) == 0) shifted[j] >>= 1; dp[j]++; } dp[i] = best; } cout << *min_element((dp).begin(), (dp).end()) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int n, f[N]; long long a[N], b[N], c[N]; int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf("%I64d", a + i); a[n + 1] = 1; long long t = a[i]; while (t % 2 == 0) t /= 2, b[i]++; c[i] = t; } for (int i = 1; i <= n + 1; i++) { f[i] = i - 1; for (int j = 0; j <= i - 1; j++) { if (a[i] % 2 == 0) { long long l = c[j], r = c[i]; int cl = b[j], cr = b[i]; if (l % r != 0) continue; if (cr > cl && cr - cl == i - j) { f[i] = min(f[j] + i - j - 1, f[i]); } if (cr - 1 + (cl > 0) <= i - j - 1) { f[i] = min(f[j] + i - j - 1, f[i]); } } if (a[i] % 2 == 1) if (a[j] % a[i] == 0) { f[i] = min(f[j] + i - j - 1, f[i]); continue; } } } cout << f[n + 1]; }
#include <bits/stdc++.h> const int MAXN = 5000 + 10; long long a[MAXN]; int n; namespace solver1 { int v[MAXN]; int f[MAXN]; void main() { for (int i = 1; i <= n; i++) { while (!(a[i] & 1)) { a[i] >>= 1; v[i]++; } } for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) { if (a[j] % a[i] == 0 && (v[j] + i - j == v[i] || v[i] <= i - j - 1)) { f[i] = std::max(f[i], f[j]); } } f[i]++; } int ans = n; for (int i = 1; i <= n; i++) { ans = std::min(ans, n - f[i]); } printf("%d\n", ans); } } // namespace solver1 int main() { std::cin >> n; for (int i = 1; i <= n; i++) { std::cin >> a[i]; } solver1::main(); }
#include <bits/stdc++.h> using namespace std; int n; long long a[10010]; int dp[10010], b[10010]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { b[i]++; a[i] /= 2; } } dp[0] = 1; int ans = n; for (int i = 0; i < n; i++) { dp[i] = i; for (int j = 0; j < i; j++) if (a[j] % a[i] == 0 && (b[j] + i - j == b[i] || b[i] <= i - j - 1)) dp[i] = min(dp[j] + i - j - 1, dp[i]); ans = min(ans, n - i - 1 + dp[i]); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long n; const long long N = 5005; long long two[N], res[N]; long long dp[N]; bool ch(long long x, long long y) { return res[x] % res[y] == 0 && (two[y] <= y - x - 1 || two[y] - two[x] == y - x); } signed main() { cin >> n; for (long long i = 1; i <= n; i++) { cin >> res[i]; while (res[i] % 2 == 0) two[i]++, res[i] /= 2; } memset(dp, 10, sizeof(dp)); dp[0] = 0; for (long long i = 1; i <= n; i++) { dp[i] = i - 1; for (long long j = 1; j < i; j++) { if (ch(j, i)) { dp[i] = min(dp[i], dp[j] + (i - j - 1)); } } } long long minv = n; for (long long i = 1; i <= n; i++) minv = min(minv, dp[i] + n - i); cout << minv << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int f[6000], b[6000]; long long a[6000]; int n, ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", a + i); while (a[i] % 2 == 0) a[i] >>= 1, b[i]++; } for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) if (a[j] % a[i] == 0 && (b[i] <= i - j - 1 || b[j] + i - j == b[i])) f[i] = max(f[i], f[j]); ans = max(ans, ++f[i]); } printf("%d\n", n - ans); }
#include <bits/stdc++.h> using namespace std; long long pow2[10000]; long long dp[10000]; long long a[10000]; long long n, ans; inline bool check(long long x, long long y, long long l) { if (a[x] % a[y] != 0) return false; if (pow2[y] - pow2[x] == l) return true; if (pow2[y] < l) return true; return false; } int main() { scanf("%I64d", &n); ans = n; for (long long i = 1; i <= n; i++) { scanf("%I64d", &a[i]); while (a[i] % 2 == 0) { pow2[i]++; a[i] /= 2; } dp[i] = 1; for (long long j = 1; j < i; j++) { if (check(j, i, i - j)) { dp[i] = max(dp[i], dp[j] + 1); } } ans = min(ans, n - dp[i]); } printf("%I64d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long a[5010]; int cnt[5010], f[5010]; bool solve(int i, int j) { int len = i - j; if (a[j] % a[i] != 0) return 0; if (cnt[i] < len) return 1; return cnt[i] == cnt[j] + len; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%I64d", a + i); cnt[i] = 0; while (a[i] % 2 == 0) { cnt[i]++; a[i] /= 2; } } int ans = 0; for (int i = 0; i < n; ++i) { f[i] = 0; for (int j = 0; j < i; ++j) if (solve(i, j)) f[i] = max(f[i], f[j]); f[i]++; ans = max(ans, f[i]); } printf("%d\n", n - ans); }
#include <bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; const int maxn = 5111; const int mod = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-10; long long a[maxn]; int cnt[maxn]; long long odd[maxn]; int f[maxn]; int main() { ios::sync_with_stdio(false); int n, i, j, ans; cin >> n; long long t; for (i = 1; i <= n; ++i) { f[i] = 1; cin >> a[i]; t = a[i]; while ((t + 1) & 1) { t >>= 1; ++cnt[i]; } odd[i] = t; } int diff; for (i = 1; i <= n; ++i) { for (j = i + 1; j <= n; ++j) { diff = j - i; if (cnt[j] < diff || cnt[j] == cnt[i] + diff) { if (odd[i] % odd[j] == 0) { f[j] = max(f[j], f[i] + 1); } } } } ans = n - *max_element(f + 1, f + n + 1); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long a[5005]; int dp[5005], tdp[5005]; int main() { int i, j, tot; long long tmp; bool flag = true; cin >> n; cin >> a[0]; for (i = 1; i < n; i++) { cin >> a[i]; dp[i] = dp[i - 1] + 1; tdp[i] = i; tmp = a[i]; for (j = i - 1; j >= 0; j--) { if (tmp % 2 == 0) { tmp /= 2; flag = true; } else flag = false; if (a[j] % tmp == 0) { if (flag && (a[j] / tmp) % 2 == 0) continue; else tdp[i] = min(tdp[i], tdp[j] + i - j - 1); } } dp[i] = min(dp[i], tdp[i]); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, dp[10001], v[10001]; long long f[10001]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> f[i]; while (f[i] % 2 == 0) f[i] /= 2, v[i]++; } int ans = 0; for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) if (f[j] % f[i] == 0 && (v[i] - i == v[j] - j || v[i] <= i - j - 1)) dp[i] = max(dp[i], dp[j] + 1); ans = max(ans, dp[i]); } cout << n - ans << '\n'; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5000 + 10; long long dp[maxn]; long long pw[maxn]; long long a[maxn]; long long n, ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] >>= 1; pw[i]++; } } for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) if (a[j] % a[i] == 0 && (pw[i] <= i - j - 1 || pw[j] + i - j == pw[i])) dp[i] = max(dp[i], dp[j]); dp[i]++; ans = max(ans, dp[i]); } cout << n - ans; }
#include <bits/stdc++.h> using namespace std; const int N = 5005; long long a[N]; int c[N], f[N]; bool check(int i, int j) { if (a[i] % a[j] != 0) return false; if (j - 1 - i >= c[j]) return true; if (c[i] + j - i != c[j]) return false; return true; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", a + i); while (!(a[i] & 1)) { a[i] >>= 1; c[i]++; } } int ans = n; for (int i = 1; i <= n; i++) { f[i] = i - 1; for (int j = i - 1; j > 0; j--) { if (!check(j, i)) continue; f[i] = min(f[i], f[j] + i - 1 - j); } ans = min(ans, f[i] + n - i); } printf("%d", ans); }
#include <bits/stdc++.h> using namespace std; int n; long long cd[5001] = {0}; int dp[5001] = {0}; int cnt[5001] = {0}; long long rest[5001] = {0}; int MIN(int a, int b) { if (a < b) return a; return b; } int MAX(int a, int b) { if (a > b) return a; return b; } int can(int j, int i) { if (!j) return 1; if (cd[i] % 2) { if (!(cd[j] % cd[i])) return 1; return 0; } else { int cnti = cnt[i]; int cntj = cnt[j]; long long ki = rest[i], kj = rest[j]; int len = i - j; if (cnti >= len) { if (len + cntj != cnti) return 0; if (kj % ki) return 0; if ((kj / ki) % 2) return 1; return 0; } if (cnti < len) { if (!(cd[j] % ki)) return 1; return 0; } if (kj % ki) return 0; if ((kj / ki) % 2) return 1; return 0; } return 0; } void prepare() { long long k; for (int i = 1; i <= n; i++) { k = cd[i]; while (!(k % 2)) { k /= 2; cnt[i]++; } rest[i] = k; } } int main() { int ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &cd[i]); prepare(); for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) if (can(j, i)) dp[i] = MAX(dp[i], dp[j] + 1); ans = MAX(ans, dp[i]); } printf("%d\n", n - ans); return 0; }