标签:dp
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10485 | Accepted: 3738 |
Description
Input
Output
Sample Input
4 7 17 5 -21 15
Sample Output
Divisible
Source
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 10010; const int M = 110; // const int inf = 0x3f3f3f3f; bool dp[N][M]; int num[N]; int main() { int n, k; while (~scanf("%d%d", &n, &k)) { for (int i = 1; i <= n; ++i) { scanf("%d", &num[i]); } memset (dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j < k; ++j) { for (int l = 0; l < k; ++l) { if (dp[i - 1][l]) { dp[i][j] = (((l + k) % k + (num[i] + k) % k) % k) == j ? 1 : 0 || dp[i][j]; dp[i][j] = ((((l + k) % k - (num[i] + k) % k) + k) % k) == j ? 1 : 0 || dp[i][j]; } } } if (i == n) { break; } } bool flag = dp[n][0]; if (flag) { printf("Divisible\n"); continue; } printf("Not divisible\n"); } return 0; }
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 10010; const int M = 110; // const int inf = 0x3f3f3f3f; bool dp[N][M]; int num[N]; int main() { int n, k; while (~scanf("%d%d", &n, &k)) { for (int i = 1; i <= n; ++i) { scanf("%d", &num[i]); } memset (dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j < k; ++j) { if (dp[i - 1][j]) { dp[i][(j + abs(num[i]) % k) % k] = 1; dp[i][(j - abs(num[i]) % k + k) % k] = 1; } } } bool flag = dp[n][0]; if (flag) { printf("Divisible\n"); continue; } printf("Not divisible\n"); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/41411335