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没戏,保准超时,只能dp或者滑动数组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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator