Divisibility
Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, -21, 15. There are eight possible expressions: 17 + 5 + -21 + 15 = 16
17 + 5 + -21 - 15 = -14
17 + 5 - -21 + 15 = 58
17 + 5 - -21 - 15 = 28
17 - 5 + -21 + 15 = 6
17 - 5 + -21 - 15 = -24
17 - 5 - -21 + 15 = 48
17 - 5 - -21 - 15 = 18
We call the sequence of integers divisible by K if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5.
You are to write a program that will determine divisibility of sequence of integers.
Input
The first line of the input file contains two integers, N and K (1 <= N <= 10000, 2 <= K <= 100) separated by a space.
The second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.
Output
Write to the output file the word "Divisible" if given sequence of integers is divisible by K or "Not divisible" if it's not.
Sample Input
4 7
17 5 -21 15
Sample Output
Divisible
Source
题目类型:线性DP
算法分析:dp[i][j]表示是否能够使用前i个数字对K取模为j,初始化dp[1][a[1]%K] = true,状态转移方程为if:dp[i-1][j] == true,则dp[i][ (j+a[i])%K] = dp[i][(j-a[i])%K] = true,最后判断dp[N][0]的值即可
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
/************************************************** filename :b.cpp author :maksyuki created time :2017/2/12 17:58:15 last modified :2017/2/12 18:54:43 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CPPFF ifstream cin ("in") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; bool dp[2][200+66]; int a[10000+66]; int N, K; int check(int aa, int bb, int flag) { if(flag == 1) aa += bb; else aa -= bb; aa %= K; if(aa < 0) return aa + K; else return aa; } int main() { //CFF; //CPPFF; while(scanf("%d%d", &N, &K) != EOF) { for(int i = 1; i <= N; i++) scanf("%d", &a[i]); memset(dp, false, sizeof(dp)); dp[1&1][check(0,a[1],1)] = true; for(int i = 2; i <= N; i++) { memset(dp[i&1], false, sizeof(dp[i&1])); for(int j = 0; j <= K - 1; j++) if(dp[(i-1)&1][j]) { dp[i&1][check(j,a[i],1)] = true; dp[i&1][check(j,a[i],-1)] = true; } } if(dp[N&1][0]) puts("Divisible"); else puts("Not divisible"); } return 0; } |