所选的数比当前的数大的话就失去意义了 所以要保证最小每一步所选的取余的数都不能大于当前被取余的数
数据范围很小 直接暴力dfs即可
#include<cstdio> #include<cstring> #include<stack> #include<vector> #include<queue> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; const int oo = 0x3f3f3f3f; const int maxn = 1e6+117; const int mod = 1e9+7; typedef unsigned long long LL; typedef double de; int b[maxn], ans, vis[maxn], n; int cmp(int a, int b) { return a > b; } void dfs(int x, int step, int m) { if(x == n+1) { if(m == 0) ans = min(ans, step); return ; } for(int i = x; i <= n; i++) { if(vis[i] == 0 && b[i] <= m) { vis[i] = 1; dfs(i+1, step+1, m%b[i]); vis[i] = 0; } } dfs(x+1, step, m); } int main() { int T, i, a; scanf("%d", &T); while(T--) { scanf("%d %d", &n, &a); ans = oo;memset(vis, 0, sizeof(vis)); for(i = 1; i <= n; i++) scanf("%d", &b[i]); sort(b+1, b+1+n, cmp); dfs(1, 0, a); if(ans != oo) printf("%d\n", ans); else printf("-1\n"); } return 0; }
时间: 2024-11-07 15:08:29