ACM: 动态规划题 poj&nb…
2016-05-19 23:25
393 查看
Divisibility
Description
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
题意: 给出n个数, 每两个数之间可以用+或则-操作, 结果又2^n种, 判断是否存在一种结果
可以整除K. 输出 Divisible, 否则输出 Not divisible
解题思路:
1. 思路很简单, 设状态dp[i][j]: 前i个数进行 + 或则 - 操作之后, 对K进行求余
运算是否得到结果j.
2. 其实从公式: (a+b)mod K = ( (a mod
K) + (b mod K) ) mod K
(a-b)mod K = ( (a mod
K ) - (b mod K) ) mod K
只有上一个数被求余为0才可以判断下一个数进行操作后是否整除.
if( dp[i-1][j]
== true)
dp[i][ (j-a[i])mod
K ] = dp[i][ (j+a[i])mod K ] =
true
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 10005
#define MAXSIZE 105
int n, K;
int a[MAX];
bool dp[MAX][MAXSIZE];
inline int mod(int a)
{
return a > 0 ? a%K : (-a)%K;
}
bool DP()
{
memset(dp, false, sizeof(dp));
dp[1][ mod(a[1]) ] = true;
for(int i = 2; i <= n; ++i)
{
for(int j = K; j
>= 0; --j)
{
if( dp[i-1][
j ] )
dp[i][
mod(j-a[i]) ] = dp[i][ mod(j+a[i]) ] = true;
}
}
return dp
[0];
}
int main()
{
// freopen("input.txt", "r", stdin);
while(scanf("%d %d", &n,
&K) != EOF)
{
for(int i = 1; i
<= n; ++i)
scanf("%d",&a[i]);
if( DP() )
printf("Divisible\n");
else printf("Not
divisible\n");
}
return 0;
}
Description
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
题意: 给出n个数, 每两个数之间可以用+或则-操作, 结果又2^n种, 判断是否存在一种结果
可以整除K. 输出 Divisible, 否则输出 Not divisible
解题思路:
1. 思路很简单, 设状态dp[i][j]: 前i个数进行 + 或则 - 操作之后, 对K进行求余
运算是否得到结果j.
2. 其实从公式: (a+b)mod K = ( (a mod
K) + (b mod K) ) mod K
(a-b)mod K = ( (a mod
K ) - (b mod K) ) mod K
只有上一个数被求余为0才可以判断下一个数进行操作后是否整除.
if( dp[i-1][j]
== true)
dp[i][ (j-a[i])mod
K ] = dp[i][ (j+a[i])mod K ] =
true
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 10005
#define MAXSIZE 105
int n, K;
int a[MAX];
bool dp[MAX][MAXSIZE];
inline int mod(int a)
{
return a > 0 ? a%K : (-a)%K;
}
bool DP()
{
memset(dp, false, sizeof(dp));
dp[1][ mod(a[1]) ] = true;
for(int i = 2; i <= n; ++i)
{
for(int j = K; j
>= 0; --j)
{
if( dp[i-1][
j ] )
dp[i][
mod(j-a[i]) ] = dp[i][ mod(j+a[i]) ] = true;
}
}
return dp
[0];
}
int main()
{
// freopen("input.txt", "r", stdin);
while(scanf("%d %d", &n,
&K) != EOF)
{
for(int i = 1; i
<= n; ++i)
scanf("%d",&a[i]);
if( DP() )
printf("Divisible\n");
else printf("Not
divisible\n");
}
return 0;
}
相关文章推荐
- ACM: 动态规划题 poj 3093 0-1背包
- ACM: 动态规划题 poj&nb…
- ipvsadm-2 DR模式及自动检测后端服务器状态
- ACM: 动态规划题 poj 3211 背包变…
- ACM: 动态规划题 poj&nb…
- ACM: 动态规划题 poj 3659
- ACM: 动态规划题 poj 1192 树形DP
- ACM: 动态规划题 poj&nb…
- ACM: 动态规划题 poj 3140
- ACM: 动态规划题 poj 2486
- ACM: 动态规划题 poj 1947
- ACM: 动态规划题 poj 2057 树状DP
- ACM: 动态规划题 poj 1185 想了两…
- 兴趣学习: 摆脱“三天打渔两天晒网”…
- ACM: 动态规划题 poj 2411
- ACM: 动态规划题 poj 3254 学习状…
- ES使用脚本进行局部更新的排错记录 推荐
- ACM: 动态规划题 poj 3034
- (OK)(OK) Fedora23——NS3(MANETs)——Docker(busybox)——genymotion(android)——All is OK
- windows.h