poj 1745 Divisibility dp

发布时间:2014-10-22 12:35:00编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"poj 1745 Divisibility dp",主要涉及到poj 1745 Divisibility dp方面的内容,对于poj 1745 Divisibility dp感兴趣的同学可以参考一下。

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total 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.  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 #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; }


上一篇:SilkTest武林外史之3-测试Java AWT应用
下一篇:Uboot启动流程图

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款