1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include<cstdio> #include<cstring> #include<cmath> #define BIG 1005 const bool LIE = true; struct node{ int num[6],val; }pri[BIG]; int K1,Lenp,TOTAL,FIRST,ranc[6][6],RANC[BIG][6][6],outtot,multi[]={0,1,10,100,1000,10000}; inline bool checkprime(const int &prime){ if(!(prime&1)) return false ; int left=1,right=Lenp,mid=(left+right)>>1; while(left<right){ if(prime>pri[mid].val){ left=mid+1; mid=(left+right)>>1; } else{ right=mid; mid=(left+right)>>1; } } if(prime==pri[left].val) return true ; return false; } inline int add(const bool &lie, const int &pos,const int &len){ int sum=0; if(lie) for(int i=1;i<=len;i++) sum+=ranc[i][pos]; else for (int i=1;i<=len;i++) sum+=ranc[pos][i]; return sum; } inline int multiply(const bool &lie, const int &pos){ int num=0; if(lie) for(int i=1;i<=5;i++) num+=ranc[i][pos]*multi[6-i]; else for (int i=1;i<=5;i++) num+=ranc[pos][i]*multi[6-i]; return num; } inline int multiply(const bool &lie, const int &wh,const int &pos){ int num=0; if(lie) for(int i=1;i<=5;i++) num+=RANC[wh][i][pos]*multi[6-i]; else for (int i=1;i<=5;i++) num+=RANC[wh][pos][i]*multi[6-i]; return num; } inline void lastcheck(){ int TOT2,TOT3,TOT4,TOT5,TOT6; TOT3=add(!LIE,2,5)-ranc[2][3]; TOT4=add(!LIE,4,5)-ranc[4][3]; if(TOT3>TOTAL || TOT4>TOTAL ) return; ranc[2][3]=TOTAL-TOT3;ranc[4][3]=TOTAL-TOT4; TOT2=multiply(LIE,3);TOT4=multiply(!LIE,2); TOT5=multiply(!LIE,3);TOT6=multiply(!LIE,4); if(!checkprime(TOT2) || !checkprime(TOT5) || ! checkprime(TOT4) || !checkprime(TOT6)) return; memcpy(RANC[++outtot],ranc, sizeof(ranc)); } bool midpane2(int lev){ if(lev>2){ lastcheck(); return true ; } switch(lev){ case(1): for(int i=1;i<=Lenp;i++) if(pri[i].num[1]==ranc[1][1] && pri[i].num[5]==ranc[5][1] && pri[i].num[2] && pri[i].num[3] && pri[i].num[4]){ for(int i1=2;i1<=4;i1++) ranc[i1][1]=pri[i].num[i1]; if(!midpane2(lev+1)) return false; } break; case(2): for(int i=1;i<=Lenp;i++) if(pri[i].num[1]==ranc[1][5] && pri[i].num[5]==ranc[5][5] && (pri[i].num[2]&1) && (pri[i].num[3]&1) && (pri[i].num[4]&1)){ for(int i1=2;i1<=4;i1++) ranc[i1][5]=pri[i].num[i1]; if(!midpane2(lev+1)) return false; } break; } return true; } bool midpane1(int lev){ if(lev>2){ int TOT1,TOT2; TOT1=add(LIE,2,5)-ranc[3][2]; TOT2=add(LIE,4,5)-ranc[3][4]; if(TOT1>TOTAL || TOT2>TOTAL) return true; ranc[3][2]=TOTAL-TOT1;ranc[3][4]=TOTAL-TOT2; TOT1=multiply(LIE,2);TOT2=multiply(LIE,4); if(!checkprime(TOT1) || !checkprime(TOT2)) return true; midpane2(1); return true ; } switch(lev){ case(1): for(int i=1;i<=Lenp;i++) if(pri[i].num[1]==ranc[1][1] && pri[i].num[5]==ranc[1][5] && pri[i].num[2] && pri[i].num[3] && pri[i].num[4]){ for(int i1=2;i1<=4;i1++) ranc[1][i1]=pri[i].num[i1]; if(!midpane1(lev+1)) return false; } break; case(2): for(int i=1;i<=Lenp;i++) if(pri[i].num[1]==ranc[5][1] && pri[i].num[5]==ranc[5][5] && (pri[i].num[2]&1) && (pri[i].num[3]&1) && (pri[i].num[4]&1)){ for(int i1=2;i1<=4;i1++) ranc[5][i1]=pri[i].num[i1]; if(!midpane1(lev+1)) return false; } break; } return true; } inline void crosspane(){ for(int i=1;i<=Lenp;i++) if(pri[i].num[1]==FIRST){ for(int i1=1;i1<=5;i1++) ranc[i1][i1]=pri[i].num[i1]; for(int j=1;j<=Lenp;j++) if(pri[j].num[3]==ranc[3][3]){ for(int j1=1;j1<=5;j1++) ranc[j1][6-j1]=pri[j].num[6-j1]; if(ranc[1][1]+ranc[1][5]>TOTAL || ranc[1][1]+ranc[5][1]>TOTAL || ranc[5][1]+ranc[5][5]>TOTAL || ranc[1][5]+ranc[5][5]>TOTAL || ranc[2][2]+ranc[2][4]>TOTAL || ranc[2][2]+ranc[4][2]>TOTAL || ranc[4][2]+ranc[4][4]>TOTAL || ranc[4][4]+ranc[4][2]>TOTAL) continue; midpane1(1); } } } inline bool cmp(const int &A, const int &B){ int ma,mb; for(int i=1;i<=5;i++){ ma=multiply(!LIE,A,i); mb=multiply(!LIE,B,i); if(ma<mb) return true; if(ma==mb) continue ; if(ma>mb) return false; } return false; } inline void sort(){ int ranc[6][6]; for(int i=1;i<outtot;i++) for(int j=1;j<=outtot-i;j++) if(cmp(j,j+1)){ memcpy(ranc,RANC[j], sizeof(RANC[j])); memcpy(RANC[j],RANC[j+1], sizeof(RANC[j+1])); memcpy(RANC[j+1],ranc, sizeof(ranc)); } } int main() { freopen("prime3.in", "r",stdin); freopen("prime3.out", "w",stdout); scanf("%d%d",&TOTAL,&FIRST); for(int i=10001;i<=99997;i+=2){ bool flag=false ; for(int j=2;j<=(int)sqrt(i);j++) if(i%j==0){flag=true ;break;} if(!flag){ int total=0,k=i,num[6],pos=0; while(k>0){ num[6-(++pos)]=k%10; total+=num[6-pos]; k/=10; } if(total==TOTAL){ memcpy(pri[++Lenp].num,num, sizeof(num)); pri[Lenp].val=i; } } } crosspane(); sort(); for(int k=outtot;k>=1;k--){ for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++) printf( "%d",RANC[k][i][j]); printf( "\n"); } if(k!=1) printf("\n" ); } if(!outtot) printf( "NONE\n"); return 0; }
|