-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path030-valuable-cards.cpp
More file actions
74 lines (68 loc) · 1.54 KB
/
Copy path030-valuable-cards.cpp
File metadata and controls
74 lines (68 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Valuable Cards [1992F]
* Problem: https://codeforces.com/problemset/problem/1992/F
* Verdict: ACCEPTED Solved: 2025-11-01
* Language: C++23 (GCC 14-64, msys2)
* Runtime: 468 ms Memory: 800 KB
* Tags: brute force, dp, greedy, number theory, two pointers
* Author: BidoTeima
* Source: https://codeforces.com/contest/1992/submission/346891743
*/
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
const int sz = 1e5 + 5, inf = 1e18 + 3, mod = 998244353;
int n, x, a[sz];
int f()
{
vector<int> dv;
for(int i = 1; i * i <= x; i++)
{
if(x % i) continue;
dv.push_back(i);
if(x / i != i) dv.push_back(x / i);
}
sort(all(dv));
int l = 1, cnt = 1;
vector<int> dp(x + 1, 0);
for(int i = 1; i <= n; i++)
{
if(x % a[i]) continue;
if(dp[x / a[i]])
{
l = i;
dp.assign(x + 1, 0);
dp[a[i]] |= 1;
cnt++;
}else{
for(int j = dv.size() - 1; j >= 0; j--)
{
if(x % (dv[j] * a[i]) == 0) dp[dv[j] * a[i]] |= dp[dv[j]];
}
dp[a[i]] |= 1;
}
}
return cnt;
}
void solve()
{
cin >> n >> x;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
int mn = f();
reverse(a + 1, a + 1 + n);
cout << min(mn, f()) << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
}