Advertisement
Guest User

eow

a guest
Oct 30th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #define gc getchar_unlocked
  5. #define pc putchar_unlocked
  6.  
  7. using namespace std;
  8.  
  9. #define MAX_NUM 10000000
  10.  
  11.  
  12. bool status[10000010]; //if status[i]=1 that means i is a prime num and if i is not prime status[i]=0
  13. int pos[10000010]; //if a num i is prime then pos[i] will store the serial num. of this prime num
  14. //like status[2]=1 and pos[2]=1
  15. //status[5]=1 and pos[5]=3
  16.  
  17. void gen_sieve_primes(void) //function to implement sieve of eratosthenes
  18. {
  19. int cnt=0;
  20. int c;
  21.  
  22. status[0]=0;
  23. status[1]=0; // Because 0 and 1 are not prime
  24.  
  25. for (int p=2; p<MAX_NUM; p++) // for all elements in array
  26. {
  27. if(status[p] == 1) // it is not multiple of any other prime
  28. {
  29. cnt++;
  30. pos[p]=cnt;
  31.  
  32. // mark all multiples of prime selected above as non primes
  33. c=2;
  34. int mul = p * c;
  35.  
  36. for(; mul < MAX_NUM;)
  37. {
  38. status[mul] = 0;
  39. c++;
  40. mul = p*c;
  41. }
  42.  
  43. }
  44.  
  45. }
  46. }
  47.  
  48.  
  49.  
  50. void scanint(int &x) //function to input integer using getchar function
  51. {
  52. char c = gc();
  53.  
  54. while(c<'0' || c>'9')
  55. c = gc();
  56.  
  57. x=0;
  58.  
  59. while(c>='0' && c<='9')
  60. {
  61. x = 10 * x + c - 48;
  62. c = gc();
  63. }
  64.  
  65. }
  66.  
  67.  
  68.  
  69. //recursive function to implement grid hacking mechanism
  70. //first it cracks x_s[i][j] then recursively crack the connected
  71. //servers which are x_s[i][j-1], x_s[i][j+1], x_s[i-1][j] and x_s[i+1][j]
  72.  
  73. void crackconnected(int x_s[][352],bool x_f[][352],int i,int j,int n,int o_e) // n is the no. of servers in a row and o_e=0 for even server
  74. { // and o_e=1 for odd server
  75. if((i>=1)&&(j>=1)&&(i<=n)&&(j<=n))
  76. { // constraints- bounds checking
  77. if((x_f[i][j]==0)&&(x_s[i][j]%2==o_e)) // checking if server is already cracked
  78. { // and check for odd/even server
  79. int r=x_s[i][j];
  80.  
  81. if(status[r]==0) // checking if the number is prime
  82. {
  83. x_f[i][j]=1;
  84. crackconnected(x_s,x_f,i,j+1,n,o_e);
  85. crackconnected(x_s,x_f,i+1,j,n,o_e);
  86. crackconnected(x_s,x_f,i-1,j,n,o_e);
  87. crackconnected(x_s,x_f,i,j-1,n,o_e);
  88. }
  89. }
  90. }
  91. }
  92.  
  93.  
  94.  
  95. void process(int x_s[][352],bool x_f[][352],int n) // function to crack all the servers
  96. {
  97. int i,j,b;
  98. long long int o=0; // o will store the no. of unsuccessful tries
  99.  
  100. for(i=1;i<=n;i++)
  101. {
  102. for(j=1;j<=n;j++)
  103. {
  104.  
  105. if(x_f[i][j]==0)
  106. {
  107.  
  108. b=x_s[i][j];
  109.  
  110. if(status[b]==1) //if x_s[i][j] is a prime num
  111. {
  112. //prime server
  113. o=o+(pos[b])-1;
  114. x_f[i][j]==1;
  115. }
  116.  
  117. else if(x_s[i][j]%2==0) //if x_s[i][j] is an even server
  118. {
  119. //even server
  120. o=o + ((x_s[i][j])/2);
  121. crackconnected(x_s,x_f,i,j,n,0);
  122. }
  123.  
  124. else //if x_s[i][j] is an odd server
  125. {
  126. //odd server
  127. o=o + (((x_s[i][j])+3)/2);
  128. crackconnected(x_s,x_f,i,j,n,1);
  129. }
  130.  
  131. //if condition (if(x_f[i][j]==0)) ends here
  132. }
  133.  
  134. }
  135. }
  136.  
  137. printf("%lld",o); //this is the result of the existing test case
  138. }
  139.  
  140.  
  141.  
  142.  
  143. int main()
  144. {
  145. int t,i,j,k,n;
  146.  
  147. memset(status,1,MAX_NUM);
  148. gen_sieve_primes();
  149.  
  150. int server_s[352][352]; //this stores the password for the server
  151. bool server_f[352][352]; //this stores the status of the server. Initially all zero indicating uncracked and f=0 indicates cracked server
  152.  
  153. scanf("%d",&t);
  154.  
  155. for(i=1;i<=t;i++)
  156. {
  157. scanf("%d",&n);
  158.  
  159. for(j=1;j<=n;j++)
  160. {
  161. for(k=1;k<=n;k++)
  162. {
  163. scanint(server_s[j][k]);
  164. server_f[j][k]=0;
  165. }
  166. }
  167.  
  168. process(server_s,server_f,n);
  169.  
  170. pc('\n');
  171.  
  172. }
  173.  
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement