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 |
一年半之后继续刷。。A的第一题。。还是有点水分。。。不小心看到了discuss里取模的标题。。尽管这样还是被TLE了一次。。 #include<iostream> //不容易啊。。背包。。取模(mod 60),用全局变量标志dfs是否结束,建立数组记录每个marble的val #include<queue> using namespace std; queue<bool> q; int mod[6]={60,30,20,15,12,10}; int marbles[6]; int val_rec[60+50+20+15+12+10]; bool result; bool finished; int getval(int x) //根据marble的序号获得其val { for(int i=0;i<6;i++) { if(marbles[i]!=0) { if(x<=marbles[i]) { return i+1; } else { x=x-marbles[i]; } } } } void dfs(int i,int n,int v_remain) { if(finished==true) return; if(i==n) { if(v_remain==0) { result=true; finished=true; return; } return; } else { dfs(i+1,n,v_remain-val_rec[i+1]); dfs(i+1,n,v_remain); } } int main() { while(1) { finished=false; bool flag=false; int num=0; int val=0; for(int i=0;i<6;i++) { cin>>marbles[i]; if(marbles[i]!=0) { flag=true; marbles[i]=marbles[i]%mod[i]; num+=marbles[i]; val+=(i+1)*marbles[i]; } } if(!flag) break; for(int i=1;i<=num;i++) { val_rec[i]=getval(i); } if(val%2!=0) result=false; else { result=false; dfs(0,num,val/2); } q.push(result); } int k=1; if(!q.empty()) { cout<<"Collection #"<<k<<":"<<endl; k++; bool temp=q.front(); if(temp) cout<<"Can be divided."<<endl; else cout<<"Can't be divided."<<endl; q.pop(); } while(!q.empty()) { cout<<endl; bool temp=q.front(); cout<<"Collection #"<<k<<":"<<endl; k++; if(temp) cout<<"Can be divided."<<endl; else cout<<"Can't be divided."<<endl; q.pop(); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator