[POJ 1745] Divisbility
[题目链接]
http://poj.org/problem?id=1745
[算法]
DP
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 10010 #define MAXK 110 int i,j,n,k,p; int a[MAXN]; bool f[MAXN][MAXK]; int main() { scanf("%d%d",&n,&k); for (i = 1; i <= n; i++) scanf("%d",&a[i]); f[1][(a[1] % k + k) % k] = true; for (i = 2; i <= n; i++) { p = ((a[i] % k) + k) % k; for (j = 0; j < k; j++) { f[i][j] |= f[i - 1][((j - p) % k + k) % k]; f[i][j] |= f[i - 1][((j + p) % k + k) % k]; } } if (f[n][0]) printf("Divisible\n"); else printf("Not divisible\n"); return 0; }