| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:dfs+剪枝 125ms左右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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator