Advertisement
a53

GCDnot1_OFICIALA

a53
Jan 17th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4. #define ll long long
  5. using namespace std;
  6. ll m, n, k, i, j, a[10001], b[10001], h, f, u, w, nr, prod[10001], no, x,q;
  7. ll p[10001], viz[10001], pr[10001], vx[10001],vizz[10001],sir[10001];
  8. ll v[10001],nu,la,lb,temp[10001],tra, rest, ok;
  9. ll c[95][95];
  10.  
  11. void gcd(ll &xa, ll &ya, ll ac, ll bc)
  12. {
  13. if (!bc)
  14. xa = 1, ya = 0;
  15. else
  16. {
  17. gcd(xa, ya, bc, ac % bc);
  18. ll aux = xa;
  19. xa = ya;
  20. ya = aux - ya * (ac / bc);
  21. }
  22. }
  23.  
  24. ll invers(ll xa, ll N)
  25. {
  26. ll ins, inv=0;
  27. gcd(inv, ins, xa, N);
  28. if (inv <= 0)
  29. inv = N + inv % N;
  30. return inv;
  31. }
  32.  
  33. void prime()
  34. {
  35. p[1]=1;
  36. nr = 0;
  37. for(i = 2; i <= 10000; i++)
  38. if(p[i]==0)
  39. {
  40. nr++;
  41. pr[nr] = i;
  42. j = i+i;
  43. while(j <= 10000)
  44. {
  45. p[j] = 1;
  46. j = j+i;
  47. }
  48. }
  49. }
  50.  
  51. ll ab(ll e)
  52. {
  53. if(e>0)return e;
  54. else return -e;
  55. }
  56.  
  57. void produs(ll y)
  58. {
  59. ll t = 0, h, cif;
  60. for(h = 1; h <= no; h++)
  61. {
  62. cif = (prod[h]*y+t)%10;
  63. t = (prod[h]*y+t)/10;
  64. prod[h] = cif;
  65. }
  66. while(t > 0)
  67. {
  68. no++;
  69. prod[no] = t%10;
  70. t = t/10;
  71. }
  72. }
  73.  
  74. void produs2(ll y)
  75. {
  76. ll t = 0, h, cif;
  77. for(h = 1; h <= nu; h++)
  78. {
  79. cif = (temp[h]*y+t)%10;
  80. t = (temp[h]*y+t)/10;
  81. temp[h] = cif;
  82. }
  83. while(t > 0)
  84. {
  85. nu++;
  86. temp[nu] = t%10;
  87. t = t/10;
  88. }
  89. }
  90.  
  91. void aduna1()
  92. {
  93. ll t = 0, h, cif;
  94. if(la <= nu)
  95. {
  96. for(h = 1; h <= la; h++)
  97. {
  98. cif = (a[h]+temp[h]+t)%10;
  99. t = (a[h]+temp[h]+t)/10;
  100. a[h] = cif;
  101. }
  102. for(h = la+1; h <= nu; h++)
  103. {
  104. cif = (temp[h]+t)%10;
  105. t = (temp[h]+t)/10;
  106. a[h] = cif;
  107. }
  108. la = nu;
  109. while(t>0)
  110. {
  111. la++;
  112. a[la] = t%10;
  113. t = t/10;
  114. }
  115. }
  116. else
  117. {
  118. for(h = 1; h <= nu; h++)
  119. {
  120. cif = (a[h]+temp[h]+t)%10;
  121. t = (a[h]+temp[h]+t)/10;
  122. a[h] = cif;
  123. }
  124. for(h = nu+1; h <= la; h++)
  125. {
  126. cif = (a[h]+t)%10;
  127. t = (a[h]+t)/10;
  128. a[h] = cif;
  129. }
  130.  
  131. while(t>0)
  132. {
  133. la++;
  134. a[la] = t%10;
  135. t = t/10;
  136. }
  137. }
  138. }
  139.  
  140. void aduna2()
  141. {
  142. ll t = 0, h, cif;
  143. if(lb <= nu)
  144. {
  145. for(h = 1; h <= lb; h++)
  146. {
  147. cif = (b[h]+temp[h]+t)%10;
  148. t = (b[h]+temp[h]+t)/10;
  149. b[h] = cif;
  150. }
  151. for(h = lb+1; h <= nu; h++)
  152. {
  153. cif = (temp[h]+t)%10;
  154. t = (temp[h]+t)/10;
  155. b[h] = cif;
  156. }
  157. lb = nu;
  158. while(t>0)
  159. {
  160. lb++;
  161. b[lb] = t%10;
  162. t = t/10;
  163. }
  164. }
  165. else
  166. {
  167. for(h = 1; h <= nu; h++)
  168. {
  169. cif = (b[h]+temp[h]+t)%10;
  170. t = (b[h]+temp[h]+t)/10;
  171. b[h] = cif;
  172. }
  173. for(h = nu+1; h <= lb; h++)
  174. {
  175. cif = (b[h]+t)%10;
  176. t = (b[h]+t)/10;
  177. b[h] = cif;
  178. }
  179.  
  180. while(t>0)
  181. {
  182. lb++;
  183. b[lb] = t%10;
  184. t = t/10;
  185. }
  186. }
  187. }
  188.  
  189. void scad_prod_din_a()
  190. {
  191. ll h;
  192. while(la > no)
  193. {
  194. for(h = 1; h <= la; h++)
  195. if(a[h]>=prod[h])
  196. a[h] = a[h]-prod[h];
  197. else
  198. {
  199. a[h] = a[h]+10-prod[h];
  200. a[h+1]--;
  201. }
  202. while(a[la]==0)la--;
  203. }
  204. ok = 1;
  205. while((la==no)and(ok==1))
  206. {
  207. h = la;
  208. while((a[h]==prod[h])and(h>=1))h--;
  209. if(a[h]>prod[h])
  210. {
  211. ok = 1;
  212. for(h = 1; h <= no; h++)
  213. if(a[h]>=prod[h])
  214. a[h] = a[h]-prod[h];
  215. else
  216. {
  217. a[h] = a[h]+10-prod[h];
  218. a[h+1]--;
  219. }
  220. while(a[la]==0)la--;
  221. }else ok = 0;
  222. }
  223. }
  224.  
  225. void scad_prod_din_b()
  226. {
  227. while(lb > no)
  228. {
  229. ll h;
  230. for(h = 1; h <= lb; h++)
  231. if(b[h]>=prod[h])
  232. b[h] = b[h]-prod[h];
  233. else
  234. {
  235. b[h] = b[h]+10-prod[h];
  236. b[h+1]--;
  237. }
  238. while(b[lb]==0)lb--;
  239. }
  240. ok = 1;
  241. while((lb==no)and(ok==1))
  242. {
  243. h = lb;
  244. while((b[h]==prod[h])and(h>=1))h--;
  245. if(b[h]>prod[h])
  246. {
  247. ok = 1;
  248. for(h = 1; h <= no; h++)
  249. if(b[h]>=prod[h])
  250. b[h] = b[h]-prod[h];
  251. else
  252. {
  253. b[h] = b[h]+10-prod[h];
  254. b[h+1]--;
  255. }
  256. while(b[lb]==0)lb--;
  257. }else ok = 0;
  258. }
  259. }
  260.  
  261. int main()
  262. {
  263. prime();
  264. cin >> m >> n;
  265. k = 0;
  266. for(i = 0; i < m; i++)
  267. for(j = 0; j < n; j++)
  268. if(c[i][j]==0)
  269. {
  270. k++;
  271. for(h = 0; h < m; h++)
  272. for(u = 0; u < n; u++)
  273. if((c[h][u]==0)and((ab(i-h)%pr[k])==0)and((ab(j-u)%pr[k])==0))
  274. {c[h][u] = pr[k];q++;}
  275. }
  276. no = 1;
  277. prod[1] = 1;
  278. for(j = 0; j < n; j++)
  279. for(i = 0; i < m; i++)
  280. if(viz[c[i][j]]==0)
  281. {
  282. produs(c[i][j]);
  283. viz[c[i][j]]=1;
  284. }
  285. la = 1;
  286. a[1] = 0;
  287. for(j = 1; j < n; j++)
  288. for(i = 0; i < m; i++)
  289. {
  290. w = c[i][j];
  291. if(v[w]==0)
  292. {
  293. v[w] = 1;
  294. nu = no;
  295. tra = 0;
  296. for(h = no; h >= 1; h--)
  297. {
  298. tra = tra*10+prod[h];
  299. temp[h] = tra/w;
  300. tra = tra%w;
  301. }
  302. while(temp[nu]==0)nu--;
  303. rest = 0;
  304. for(h = nu; h >= 1; h--)
  305. {
  306. rest = rest*10+temp[h];
  307. rest = rest%w;
  308. }
  309. x = invers(rest,w);
  310. q = (w-j%w)*x;
  311. produs2(q);
  312. aduna1();
  313.  
  314. }
  315. }
  316. scad_prod_din_a();
  317. lb = 1;
  318. b[1] = 0;
  319. for(i = 1; i < m; i++)
  320. for(j = 0; j < n; j++)
  321. {
  322. w = c[i][j];
  323. if(vx[w]==0)
  324. {
  325. vx[w] = 1;
  326. nu = no;
  327. tra = 0;
  328. for(h = no; h >= 1; h--)
  329. {
  330. tra = tra*10+prod[h];
  331. temp[h] = tra/w;
  332. tra = tra%w;
  333. }
  334. while(temp[nu]==0)nu--;
  335. rest = 0;
  336. for(h = nu; h >= 1; h--)
  337. {
  338. rest = rest*10+temp[h];
  339. rest = rest%w;
  340. }
  341. x = invers(rest,w);
  342. q = (w-i%w)*x;
  343. produs2(q);
  344. aduna2();
  345.  
  346. }
  347. }
  348. scad_prod_din_b();
  349.  
  350. for(i = lb; i >= 1; i--)
  351. cout << b[i];
  352. cout << " ";
  353. for(i = la; i >= 1; i--)
  354. cout << a[i];
  355. return 0;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement