Advertisement
Saleh127

UVA 10717

Nov 7th, 2020
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5.  
  6. ll lcmm(ll a,ll b)
  7. {
  8. ll ans;
  9.  
  10. ans=(a*b)/__gcd(a,b);
  11.  
  12. return ans;
  13. }
  14.  
  15. int main()
  16. {
  17. ios_base::sync_with_stdio(0);
  18. cin.tie(0);
  19. cout.tie(0);
  20.  
  21. ll a[1000],b,c,d,e,f,mx,mn,i,j,k,l;
  22. while(cin>>b>>c && b && c)
  23. {
  24. for(i=0; i<b; i++)
  25. {
  26. cin>>a[i];
  27. }
  28. while(c--)
  29. {
  30. cin>>d;
  31. mx=10000000000;
  32. mn=10000000000;
  33. for(i=0; i<b; i++)
  34. {
  35. for(j=i+1; j<b; j++)
  36. {
  37. for(k=j+1; k<b; k++)
  38. {
  39. for(l=k+1; l<b; l++)
  40. {
  41. e=lcmm(a[i],lcmm(a[j],lcmm(a[k],a[l])));
  42.  
  43. ll rem=d%e;
  44. ll val=e-rem;
  45.  
  46. if(rem==0)
  47. {
  48. i=j=k=l=b;
  49. mx=0;
  50. mn=0;
  51. }
  52. else
  53. {
  54. mn=min(rem,mn);
  55. mx=min(val,mx);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. cout<<d-mn<<" "<<d+mx<<endl;
  62. }
  63. }
  64.  
  65. return 0;
  66. }
  67.  
Advertisement
RAW Paste Data Copied
Advertisement