Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:dfs+剪枝 125ms左右

Posted by Dwayne at 2014-07-13 10:30:04 on Problem 1745
In Reply To:这题用dfs没戏,保准超时,只能dp或者滑动数组 Posted by:Hins at 2010-01-11 15:53:18
int a[10001];
int v[10001][101];
void dfs(int cur, int x)
{
	if (jud)
		return;
	if (x==n-1&&cur%m==0)
	{
		jud=1;
		return;
	}
	else if (x==n-1)
		return;
	v[x][cur]=1;
	int plus=(cur+a[x+1])%m;
	if (x==n-2&&!plus)
	{
		jud=1;
		return;
	}
	else if (!v[x+1][plus])
		dfs(plus, x+1);
	int minus=(cur-a[x+1]+m)%m;
	if (x==n-2&&!minus)
	{
		jud=1;
		return;
	}
	else if (!v[x+1][minus])
		dfs(minus, x+1);
}
int main()
{
	scanf("%d %d", &n, &m);
	for (int i=0; i<n; i++)
		scanf("%d", &a[i]), a[i]=(a[i]+m)%m;
	dfs(a[0], 0);
	printf("%s\n", jud?"Divisible":"Not divisible");
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator