首页 > 其他 > 详细

poj 1745

时间:2015-03-01 17:14:18      阅读:242      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<cstdlib>
#include<stdio.h>

using namespace std;

const int maxn = 10017;
const int maxk = 117;

int n, k;
int d[maxn];
bool dp[maxn][maxk];
const char str[2][30] = {"Not divisible", "Divisible"};
int DP(){
	while(d[0] < 0){
		d[0] += k;
	}
	dp[0][d[0]%k] = true;
	for(int i = 1; i < n; ++i){
		for(int j = 0; j < k; ++j){
			if(dp[i-1][j]){
				int tmp = (j + d[i]) % k;
				while(tmp < 0){
					tmp += k;
				}
				tmp %= k;
				dp[i][tmp] = true;
				tmp = (j - d[i]) % k;
				while(tmp < 0){
					tmp += k;
				}
				tmp %= k;
				dp[i][tmp] = true;
			}
		}
	}
	return 0;
}
int main(){
	while(scanf("%d%d",&n,&k)!=EOF){
		for(int i = 0; i < n; ++i){
			scanf("%d", d + i);
			while(d[i] < 0){
				d[i] += k;
			}
		}
		memset(dp, 0, sizeof(dp));
		DP();
		if(dp[n-1][0]){
			printf("Divisible\n");
		}
		else{
			printf("Not divisible\n");
		}
	}
	return 0;
}

这道题开始wa了几次,看网上的参考代码才发现自己的问题在于:

对于负数取余的处理错了....

poj 1745

原文:http://my.oschina.net/u/1421373/blog/381018

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!