1 #include <iostream> 2 #include <string.h> 3 #include <string> 4 #include <fstream> 5 #include <algorithm> 6 #include <stdio.h> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 using namespace std; 11 const double eps = 1e-8; 12 #define MAXN 10000001 13 int n,m; 14 int ans[7],test[7]; 15 int stats=0; 16 int finish=0; 17 bool check(int *a,int *b) 18 { 19 int hash[404]; 20 memset(hash,0,sizeof(hash)); 21 for(int i=1;i<=m;i++) 22 hash[a[i]]++; 23 for(int i=1;i<=m;i++) 24 if(hash[b[i]]==0)return false; 25 return true; 26 } 27 int* multiply(int t,int *a) 28 { 29 int *b=new int[m+1],temp=0; 30 for(int i=m;i>=1;i--) 31 { 32 temp+=a[i]*t; 33 b[i]=temp%n; 34 temp=temp/n; 35 } 36 return b; 37 } 38 39 void print(int *tt) 40 { 41 printf("%d",tt[1]); 42 for(int i=2;i<=m;i++) 43 printf(" %d",tt[i]); 44 printf(" "); 45 } 46 47 void dfs(int c) 48 { 49 if(c==m){bool found=true; 50 for(int i=2;i<=m;i++) 51 if(!check(ans,multiply(i,test))){found=false;break;} 52 if(found){print(test);finish=1;} 53 return;} 54 for(int i=1;i<m;i++) 55 { 56 if(stats&(1<<i))continue; 57 test[c]=ans[i]; 58 stats|=(1<<i); 59 dfs(c+1); 60 stats^=(1<<i); 61 } 62 } 63 int main() 64 { 65 int i,j; 66 67 while(scanf("%d%d",&m,&n),m!=0&&n!=0) 68 { 69 finish=0; 70 for(int last=1;last<=n-1;last++) 71 { 72 memset(ans,0,sizeof(ans)); 73 ans[1]=last; 74 for(i=2;i<=m;i++) 75 { 76 int temp=(last*i)%n; 77 for(j=1;j<i;j++) 78 if(temp==ans[j])break; 79 if(j==i)ans[j]=temp; 80 else break; 81 } 82 if(ans[m]!=0) 83 { 84 swap(ans[1],ans[m]); 85 test[m]=ans[m]; 86 //print(ans); 87 //print(multiply(2,ans)); 88 stats=0; 89 dfs(1); 90 } 91 if(finish)break;} 92 if(!finish)printf("Not found. "); 93 } 94 95 return 0; 96 }
终于没参考别人的代码 --!