Advertisement
a53

cmmdc5

a53
Mar 20th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("cmmdc.in");
  4. ofstream fout("cmmdc.out");
  5. int T,N;
  6. long long a[100005],prefix[100005],suffix[100005];
  7.  
  8. int main()
  9. {
  10. fin>>T>>N;
  11. for(int i=1;i<=N;++i)
  12. fin>>a[i],prefix[i]=__gcd(prefix[i-1],a[i]);
  13. for(int i=N;i>=1;--i)
  14. suffix[i]=__gcd(suffix[i+1],a[i]);
  15. if(T==1)
  16. {
  17. fout<<prefix[N]<<'\n';
  18. return 0;
  19. }
  20. if(T==2)
  21. {
  22. long long ans=max(suffix[2],prefix[N-1]);
  23. for(int i=2;i<N;++i)
  24. ans=max(ans,__gcd(prefix[i-1],suffix[i+1]));
  25. fout<<ans;
  26. return 0;
  27. }
  28. if(T==3)
  29. {
  30. static long long M=0,ans=0,c=0;
  31. for(int i=1;i<N;++i,c=0)
  32. for(int j=i+1;j<=N;++j)
  33. M=__gcd(__gcd(prefix[i-1],suffix[j+1]),c),c=__gcd(c,a[j]),ans=max(ans,M);
  34. fout<<ans;
  35. }
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement