解题思路:
注意事项:
参考代码:
#include <stdio.h> #include <iostream> #define N 20000 using namespace std; int a[N]; int n; bool dp[N]; // TLE的搜索 bool solve_dfs(int res) { if (res == 0) return true; else if (res < 0) return false; else { bool ans = false; for (int i = 0; i < n; i++) if(solve_dfs(res - a[i])) ans = true; return ans; } } // 很简单的DP int solve_dp(int target) { dp[0] = true; for (int i = 1; i <= target; i++) for (int j = 0; j < n; j++) if (i - a[j] >= 0) dp[i] |= dp[i - a[j]]; int ans = 0; for (int i = target; i >= 1; i--) if (!dp[i]) ans += 1; return ans; } // 求最大公因数 int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; // 判断互质 int d = a[0]; for (int i = 1; i < n; i++) d = gcd(d, a[i]); bool rp = (d == 1 ? true : false); // 不互质就会有无限个凑不出 if (!rp) cout << "INF"; else cout << solve_dp(10000) << endl; // 只要够暴力 return 0; }
0.0分
0 人评分
Tom数 (C++代码)浏览:825 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:594 |
最小公倍数 (C语言代码)浏览:861 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:451 |
【排队买票】 (C语言代码)浏览:899 |
用筛法求之N内的素数。 (C语言代码)浏览:802 |
【矩阵】 (C++代码)浏览:936 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:262 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:901 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:579 |