Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 0 Accepted Submission(s) : 0
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
打表代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int num[30],res[30]; int isOK(int k){ int t1=0,t2=0; for(int i=0;i<k;i++) t1+=num[i]; for(int i=k;i<2*k;i++) t2+=num[i]; if(t1==k && t2==0) return 1; return 0; } int Solve(int n,int m){ int cnt=0,tmp=0; int i=0; while(1){ if(num[i]) tmp++; if(tmp==m){ num[i]=0; tmp=0; cnt++; } i++; i%=n; if(cnt==n/2) break; } if(isOK(n/2)) return 1; return 0; } int main(){ //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); for(int k=1;k<14;k++){ int m=k+1; while(1){ for(int i=0;i<2*k;i++) num[i]=1; if(Solve(2*k,m)){ break; } m++; } printf("%d,",m); } return 0; }
再进行打表:
#include<stdio.h> int num[14]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881}; int main(){ int k; while(~scanf("%d",&k) && k){ printf("%d\n",num[k]); } return 0; }
解法二:(另外一种打表)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int res[20]; void Init(){ int sum; for(int k=1;k<14;k++) for(int m=k+1;;m++){ sum=k*2; int flag=0; for(int j=m;;j+=m-1){ j=j%sum?j%sum:sum; if(j<=k) break; sum--; if(sum==k){ flag=1; break; } } if(flag){ res[k]=m; break; } } } int main(){ //freopen("input.txt","r",stdin); Init(); int k; while(~scanf("%d",&k) && k){ printf("%d\n",res[k]); } return 0; }