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没戏,保准超时,只能dp或者滑动数组

Posted by 731371663 at 2011-07-24 23:28:53 on Problem 1745
In Reply To:这题用dfs没戏,保准超时,只能dp或者滑动数组 Posted by:Hins at 2010-01-11 15:53:18
谁说的啊,我DFS 300MS 过的

#include<iostream>
using namespace std;
int num[10001];
 
bool f[10001][101],flag;
int k,n;

bool dfs(int i,int sum)
{
	if(i==n) 
	{
		if(sum%k==0)
		{

			flag=1;
			return 1;
		}
		else return 0;
	}
    if(!f[i][sum]) 
	{
		f[i][sum]=1;
		dfs(i+1,(sum+num[i+1])%k);
		dfs(i+1,(k+sum-num[i+1])%k);
	}
	return flag;
}


int main()
{
	freopen("in.txt","r",stdin);
	int i,a;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a);
		num[i]=a%k;
	}
    if(dfs(1,num[1]))
		printf("Divisible\n");
    else
	    printf("Not divisible\n");
    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