| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 11053 | Accepted: 3953 |
Description
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 second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.
Output
Sample Input
4 7 17 5 -21 15
Sample Output
Divisible
给一个长度为n的数组,自己可以加减运算,问能不能被k整除。
读入的时候处理,dp[i][j] == 1代表可以被k整除。
状态转移方程:dp[i][(j + num[i]) % k],dp[i][(k + j - num[i]) % k] ,表示加上或减去第i个数,所得数取模能否为j。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 1e4 + 5;
int main(int argc, char const *argv[])
{
int n, k;
while(cin >> n >> k) {
int dp[maxn][101] = {0}, num[maxn];
for(int i = 0; i < n; ++i) {
cin >> num[i];
if(num[i] < 0) num[i] = -num[i];
num[i] %= k;
}
dp[0][num[0]] = 1;
for(int i = 1; i < n; ++i)
for(int j = 0; j <= k; ++j)
if(dp[i - 1][j]) {
dp[i][(j + num[i]) % k] = 1;
dp[i][(k + j - num[i]) % k] = 1;
}
if(dp[n - 1][0]) cout << "Divisible" << endl;
else cout << "Not divisible" << endl;
}
return 0;
}
424




被折叠的 条评论
为什么被折叠?



