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 |
我还想写个分治的代码,结果很不行,超时了,,调试了我一个小时,呃呃呃!!!纪念代码!!!#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; bool c[105]; int obj[10005]; int n,k; void sovle(int i,int j,bool *ar) { if(i>j) {ar[0]=true;return ;} if(i==j) {ar[obj[i]] = true;ar[k-obj[i]] = true;return ;} if(i+1==j) { int temp = obj[i] - obj[j]; temp %= k; if(temp<0) temp+=k; ar[temp]=true; ar[k-temp]=true; temp = obj[i]+obj[j]; temp %= k; ar[temp] = true; ar[k-temp]=true; return ; } bool a[105]; bool b[105]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int mid = (i+j)/2; sovle(i,mid,a); sovle(mid+1,j,b); for(int ii=0;ii<k;ii++) for(int jj=0;jj<k;jj++) { if(a[ii]&&b[jj]) { /*for(int ha=0;ha<k;ha++) cout<<a[ha]<<" "; cout<<endl;*/ //if(i==0&&j==2) //{ int temp =(ii+jj)%k; //cout<<"("<<ii<<" "<<jj<<" )"<<temp<<" "<<k-temp<<endl; ar[temp] = true; ar[k-temp]=true; temp =(ii-jj)%k; if(temp<0) temp +=k; // cout<<"("<<ii<<" "<<jj<<" )"<<temp<<" "<<k-temp<<endl; ar[temp] = 1; ar[k-temp]=true; //} } } return ; } void init() { while(scanf("%d%d",&n,&k)!=EOF) { int x; scanf("%d",&x); x %= k; for(int i=0;i<n-1;i++) { scanf("%d",&obj[i]); obj[i] %= k; if(obj[i]<0) obj[i]=-obj[i]; } int kk=0; for(int i=0;i<n-1;i++) if(obj[i]) obj[kk++]=obj[i]; memset(c,0,sizeof(c)); sovle(0,kk-1,c); if(x>0) if(c[k-x]) puts("Divisible"); else puts("Not divisible"); else if(x==0) if(c[0]) puts("Divisible"); else puts("Not divisible"); else if(c[-x]) puts("Divisible"); else puts("Not divisible"); } } int main() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); init(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator