poj 1745 Divisibility dp
来源:互联网 发布:俄罗斯留学生活 知乎 编辑:程序博客网 时间:2024/05/17 10:26
Divisibility
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9693 Accepted: 3380
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.
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.
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 717 5 -21 15
Sample Output
Divisible
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;int dp[11111][101];int num[11111];int main(){ int n,k; cin>>n>>k; int i,j; for(i=0;i<n;i++) { scanf("%d",&num[i]); if(num[i]<0) num[i]=-num[i]; num[i]=num[i]%k; } memset(dp,0,sizeof(dp)); dp[0][num[0]]=1; for(i=1;i<n;i++) { for(j=0;j<=k;j++) { if(dp[i-1][j]) { dp[i][(j+num[i])%k]=1; dp[i][(j-num[i]+k)%k]=1; } } } if(dp[n-1][0]) cout<<"Divisible\n"; else cout<<"Not divisible\n"; return 0;}
滚动数组
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n,m,a[10010],f[2][110],i,j;int main(){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]<0) a[i]=-a[i]; a[i]=a[i]%m; } memset(f,0,sizeof(f)); f[0][a[1]]=1; for(i=2;i<=n;i++) { for(j=0;j<=m;j++) { if(f[0][j]) { f[1][(j+a[i])%m]=1; f[1][(j-a[i]+m)%m]=1; } } if(flag) for(j=0;j<=m;j++) { f[0][j]=f[1][j]; f[1][j]=0; } } if(f[0][0]) { printf("Divisible\n"); } else { printf("Not divisible\n"); } return 0;}
- poj 1745 Divisibility dp
- POJ 1745 Divisibility DP
- poj 1745 Divisibility (dp)
- poj 1745 Divisibility dp
- 【DP】POJ-1745 Divisibility
- poj 1745 Divisibility 【DP】
- poj 1745 Divisibility dp
- poj 1745 Divisibility(DP)
- poj - 1745 - Divisibility(dp)
- POJ 1745 Divisibility (线性dp)
- POJ 1745 Divisibility (DP)
- POJ Problem 1745 Divisibility 【dp】
- POJ 题目1745 Divisibility(DP)
- poj 1745 Divisibility(DP + 数学)
- POJ 题目1745 Divisibility(DP,数学)
- 1745 Divisibility DP
- POJ 1745 Divisibility 类似0-1背包DP
- POJ 1745:Divisibility 枚举某一状态的DP
- 第六章 AXF (ActXmlFromFramework) 活动ANDROID页面展现框架 android动态加载页面框架
- SilkTest武林外史之2-自动复原系统
- jquery 获取 form radio 值
- 使用 Visual Basic 设计 Python 的 GUI:Visual Tkinter
- SilkTest武林外史之3-测试Java AWT应用
- poj 1745 Divisibility dp
- Solution to Articulation Points, Bridges, and Biconnected Components Problem
- 今天所学(9.3)
- P64E2anwer
- 转自老赵:SICP书荐,及函数复用的一些解释
- poj 1755 Triathlon(半平面交解不等式)
- Java采用readInt()方法直接读取特定的ZIP压缩文件出现了奇怪的问题
- VIM查看文件编码 文件编码格式转换 文件名编码转换.
- 开源免费的C/C++网络库(c/c++ sockets library)