POJ 1745:Divisibility 枚举某一状态的DP
2015-08-04 22:47
330 查看
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
Sample Output
题意就是给了N个数,在N-1个位置变换+ -号,问得到的结果中有没有能够整除K的,如果有,输出Divisible。没有,输出Not Divisible。
DP真是一片很深的海。
越做DP越觉得DP的花样很多,这个是我做了POJ1837觉得DP是可以做这道题的。觉得DFS也应该可以,没试。。。
POJ1837和这道题都是固定枚举其中的某个状态或者变量,这里的可以枚举的状态就是余数,给了K,所以我只需对0到K-1这些余数做枚举,然后从i的余数状态推i+1的余数状态。
就是这样:
dp[i][(j+value[i])%mod] +=dp[i-1][j];
dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j];
然后这样做可能是因为数目比较大了溢出还是怎样WA了一次,于是我控制了一下数值。这样:
dp[i][(j+value[i])%mod] +=dp[i-1][j];
dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j];
if(dp[i][(j+value[i])%mod]>10)
dp[i][(j+value[i])%mod]=10;
if(dp[i][(j-value[i]+mod)%mod]>10)
dp[i][(j+value[i])%mod]=10;
。。。很幼稚的方法,但还是涨姿势长见识了。。。
代码:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11001 | Accepted: 3933 |
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个数,在N-1个位置变换+ -号,问得到的结果中有没有能够整除K的,如果有,输出Divisible。没有,输出Not Divisible。
DP真是一片很深的海。
越做DP越觉得DP的花样很多,这个是我做了POJ1837觉得DP是可以做这道题的。觉得DFS也应该可以,没试。。。
POJ1837和这道题都是固定枚举其中的某个状态或者变量,这里的可以枚举的状态就是余数,给了K,所以我只需对0到K-1这些余数做枚举,然后从i的余数状态推i+1的余数状态。
就是这样:
dp[i][(j+value[i])%mod] +=dp[i-1][j];
dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j];
然后这样做可能是因为数目比较大了溢出还是怎样WA了一次,于是我控制了一下数值。这样:
dp[i][(j+value[i])%mod] +=dp[i-1][j];
dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j];
if(dp[i][(j+value[i])%mod]>10)
dp[i][(j+value[i])%mod]=10;
if(dp[i][(j-value[i]+mod)%mod]>10)
dp[i][(j+value[i])%mod]=10;
。。。很幼稚的方法,但还是涨姿势长见识了。。。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> using namespace std; int num,mod; int dp[10005][102]; int value[10005]; int main() { int temp,i,j; cin>>num>>mod; cin>>value[1]; value[1]=abs(value[1])%mod; for(i=2;i<=num;i++) { cin>>temp; value[i]=abs(temp); value[i]=value[i]%mod; } memset(dp,0,sizeof(dp)); dp[1][value[1]]=1; for(i=2;i<=num;i++) { for(j=0;j<mod;j++) { dp[i][(j+value[i])%mod] +=dp[i-1][j]; dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j]; if(dp[i][(j+value[i])%mod]>10) dp[i][(j+value[i])%mod]=10; if(dp[i][(j-value[i]+mod)%mod]>10) dp[i][(j+value[i])%mod]=10; } } if(dp[num][0]) { cout<<"Divisible"<<endl; } else { cout<<"Not divisible"<<endl; } return 0; }
相关文章推荐
- Pet(dfs+vector)
- python中的shelve模块,可以提供一些简单的数据操作
- Android Studio 使用遇到的各种问题及解决方案汇总
- c++ 迭代器与下标访问效率问题
- 集成运放的几个简单应用
- hive学习笔记——Hive表中数据的导入和导出
- ios对于枚举的使用
- c++ 计算程序运行时间
- 如何使flash中数据流声音的音质提高
- 日期操作
- iOS中的数组操作大全
- 服务器启动时Webapp的web.xml中配置的加载顺序
- Java:进程与线程
- Windows下mysql忘记root密码的解决方法
- 4000 python中的zlib模块提供了压缩和解压缩的方法
- linux运维笔记day6--基础命令
- ITK与VTK数据转换
- Attempting to initialize hardware acceleration outside of the main thread 处理
- 关于位运算
- 深入理解C++中的初始化