fork(1) download
  1. #include <bits/stdc++.h>
  2. #pragma comment(linker, "/stack:200000000")
  3. #pragma GCC optimize("Ofast")
  4. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  5. #define ll int
  6. #define pb push_back
  7. #define all(x) (x).begin(), (x).end()
  8. #define endl "\n"
  9. #define MAXN 100010
  10.  
  11. using namespace std;
  12.  
  13.  
  14.  
  15. ll used[MAXN];
  16. ll usedd[MAXN];
  17. ll check(ll n,ll sum)
  18. {
  19. ll s=0;
  20. while(n>0)
  21. {
  22. ll q=n%10;
  23. s+=q;
  24. n/=10;
  25. }
  26. if(s==sum)return 1;
  27. else return 0;
  28. }
  29.  
  30. ll checkprime(ll n)
  31. {
  32. if(n==2 || n==3)return 1;
  33. else if(n==1 || n%2==0 || n%3==0)return 0;
  34. for(ll i=5,p=2;i*i<=n;i+=p,p=6-p)
  35. {
  36. if(n%i==0)return 0;
  37. }
  38. return 1;
  39. }
  40.  
  41. vector<ll>primes;
  42. char table[8][8];
  43.  
  44.  
  45. void print()
  46. {
  47. for(ll i=1;i<=5;i++)
  48. {
  49. for(ll j=1;j<=5;j++)
  50. {
  51. printf("%d",table[i][j]);
  52. }
  53. printf("\n");
  54. }
  55. }
  56.  
  57.  
  58.  
  59.  
  60. vector<ll>massiv;
  61. ll kol=0;
  62. int main(){
  63. ll sum,x;
  64. scanf("%d%d",&sum,&x);
  65. for(ll i=10000;i<=99999;i++)
  66. {
  67. if(checkprime(i))
  68. {
  69. if(check(i,sum))
  70. {
  71. used[i]=1;
  72. primes.pb(i);
  73. }
  74. }
  75. }
  76. ll one,two,three,four,five;
  77. vector<ll>pos;
  78. for(ll i=0;i<primes.size();i++)
  79. {
  80. if(primes[i]/10000==x)
  81. {
  82. pos.pb(primes[i]);
  83. }
  84. }
  85.  
  86. if((ll)pos.size()==0 || (ll)primes.size()==0)
  87. {
  88. cout<<"NONE"<<endl;
  89. return 0;
  90. }
  91. ll sd=0;
  92. for(ll i=0;i<(ll)pos.size();i++)
  93. {
  94. ll diagonalone=pos[i];
  95. table[1][1]=diagonalone/10000;
  96. table[2][2]=diagonalone/1000%10;
  97. table[3][3]=diagonalone/100%10;
  98. table[4][4]=diagonalone/10%10;
  99. table[5][5]=diagonalone%10;
  100. vector<ll>onee;
  101. vector<ll>twoo;
  102. vector<ll>threee;
  103. vector<ll>fourr;
  104. vector<ll>fivee;
  105. for(ll j=0;j<primes.size();j++)
  106. {
  107. ll cis=primes[j];
  108. if(cis/10000==table[1][1])
  109. {
  110. onee.pb(cis);
  111. }
  112. if(cis/10000==table[1][1])
  113. {
  114. onee.pb(cis);
  115. }
  116. if(cis/1000%10==table[2][2])
  117. {
  118. twoo.pb(cis);
  119. }
  120. if(cis/100%10==table[3][3])
  121. {
  122. threee.pb(cis);
  123. }
  124. if(cis/10%10==table[4][4])
  125. {
  126. fourr.pb(cis);
  127. }
  128. if(cis%10==table[5][5])
  129. {
  130. fivee.pb(cis);
  131. }
  132. }
  133. for(ll ii=0;ii<onee.size();ii++)
  134. {
  135. one=onee[ii];
  136. table[1][2]=one/1000%10;
  137. table[1][3]=one/100%10;
  138. table[1][4]=one/10%10;
  139. table[1][5]=one%10;
  140. for(ll iii=0;iii<twoo.size();iii++)
  141. {
  142. two=twoo[iii];
  143. table[2][1]=two/10000;
  144. table[2][3]=two/100%10;
  145. table[2][4]=two/10%10;
  146. table[2][5]=two%10;
  147. for(ll iiii=0;iiii<threee.size();iiii++)
  148. {
  149. three=threee[iiii];
  150. table[3][1]=three/10000;
  151. table[3][2]=three/1000%10;
  152. table[3][4]=three/10%10;
  153. table[3][5]=three%10;
  154. for(ll iiiii=0;iiiii<fourr.size();iiiii++)
  155. {
  156. four=fourr[iiiii];
  157. table[4][1]=four/10000;
  158. table[4][2]=four/1000%10;
  159. table[4][3]=four/100%10;
  160. table[4][5]=four%10;
  161. for(ll iiiiii=0;iiiiii<fivee.size();iiiiii++)
  162. {
  163. five=fivee[iiiiii];
  164. table[5][1]=five/10000;
  165. table[5][2]=five/1000%10;
  166. table[5][3]=five/100%10;
  167. table[5][4]=five/10%10;
  168. ll ones=table[1][1]*10000+table[2][1]*1000+table[3][1]*100+table[4][1]*10+table[5][1];
  169. ll twos=table[1][2]*10000+table[2][2]*1000+table[3][2]*100+table[4][2]*10+table[5][2];
  170. ll threes=table[1][3]*10000+table[2][3]*1000+table[3][3]*100+table[4][3]*10+table[5][3];
  171. ll fours=table[1][4]*10000+table[2][4]*1000+table[3][4]*100+table[4][4]*10+table[5][4];
  172. ll fives=table[1][5]*10000+table[2][5]*1000+table[3][5]*100+table[4][5]*10+table[5][5];
  173. ll diagonaltwo=table[1][5]*10000+table[2][4]*1000+table[3][3]*100+table[4][2]*10+table[5][1];
  174. if(used[ones] && used[twos] && used[threes] && used[fours] && used[fives] && used[diagonaltwo] )
  175. {
  176. if(usedd[ones]!=1 || usedd[twos]!=1 || usedd[threes]!=1 || usedd[fours]!=1 || usedd[fives]!=1 || usedd[diagonaltwo]!=1)
  177. {
  178. usedd[ones]=1,usedd[twos]=1,usedd[threes]=1,usedd[fours]=1,usedd[fives]=1,usedd[diagonaltwo]=1;
  179. if(sd!=0)printf("\n");
  180. sd++;
  181. print();
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. return 0;
  191. }
Time limit exceeded #stdin #stdout 5s 4524KB
stdin
11 1
stdout
Standard output is empty