Sanath12345

Untitled

Aug 20th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. //Problem link :
  2. //CREDITS : sanath kumar
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long int
  6. #define ld long double
  7. #define mod 1000000007
  8. #define inf 1e9
  9. #define endl "\n"
  10. #define pb push_back
  11. #define vi vector<ll>
  12. #define vs vector<string>
  13. #define pii pair<ll,ll>
  14. #define ump unordered_map
  15. #define mp make_pair
  16. #define pq_max priority_queue<ll>
  17. #define pq_min priority_queue<ll,vector<ll>,greater<ll>>
  18. #define all(v) v.begin(),v.end()
  19. #define ff first
  20. #define ss second
  21. #define mid(l,r) (l+(r-l))/2
  22. #define bitc(x) __builtin_popcount(x)
  23. #define read(a,n) for(int i=0;i<n;i++) cin>>a[i];
  24. #define loop(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define looprev(i,a,b) for(int i=(a);i>=(b);i--)
  26. #define iter(c,it) for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
  27. #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  28. #define log(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
  29. #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
  30. template<typename T> void shiftr(T *arr,int n){for(int i=n;i>=1;i--) arr[i]=arr[i-1];arr[0]=0;}
  31. template<typename T> T gcd(T a,T b) {if(b==0) return a; return gcd(b,a%b);}
  32. template<typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
  33. template<class T> T add(T a,T b) {return ((a%mod)+(b%mod))%mod;}
  34. template<class T> T sub(T a,T b) {return ((a%mod)-(b%mod)+mod)%mod;}
  35. template<class T> T mul(T a,T b) {return ((a%mod)*(b%mod)*(1LL))%mod;}
  36. template<class T> void debug(set<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  37. template<class T> void debug(vector<ll> v){cout<<"[ ";for(auto i:v) cout<<i<<" ";cout<<"] "<<endl;}
  38. template<class T> void debug(multiset<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  39. template<class T> void debug(unordered_set<T> S){cout<<"[ ";for(auto i:S) cout<<i<<" ";cout<<"] "<<endl;}
  40. template<class T,class X> void debug(T *arr,X s,X e){cout<<"[ ";loop(i,s,e) cout<<arr[i]<<" ";cout<<"] "<<endl;}
  41. template<class T,class V> void debug(pair<T,V> p){cout<<"{";cout<<p.ff<<" "<<p.ss<<"}"<<endl;}
  42. template<class T,class V> void debug(vector<pair<T,V>> v){cout<<"[ "<<endl;for(auto i:v) debug(i);cout<<"]"<<endl;}
  43.  
  44. void err(istream_iterator<string> it){}
  45. template<typename T,typename... Args>
  46. void err(istream_iterator<string> it,T a,Args... args){
  47. cout<<*it<<" = "<<a<<endl;
  48. err(++it,args...);
  49. }
  50. ll *getfactorial(){
  51. int m=1e6;
  52. ll *fact=new ll[m+1];
  53. fact[0]=1;
  54. loop(i,1,m){
  55. fact[i]=(fact[i-1]*i)%mod;
  56. }
  57. // time complexity O(n)
  58. return fact;
  59. }
  60. vi *getfactors(){
  61. int n=2e5;
  62. vi *factors=new vi[n+5];
  63. loop(i,1,n){
  64. for(int j=i;j<=n;j+=i){
  65. factors[j].pb(i);
  66. }
  67. }
  68. // time complexity O(nlogn)
  69. return factors;
  70. }
  71. ll power(ll a,ll b,ll m=mod){
  72. if(b==0) return 1;
  73. ll smallans=power(a,b/2,m);
  74. ll ans=(smallans*smallans)%m;
  75. if(b%2==1){
  76. ans=(ans*a)%m;
  77. }
  78. return ans;
  79. // time complexity O(logb)
  80. }
  81. bool *isprime(){
  82. int m=1e6;
  83. bool *p=new bool[m];
  84. loop(i,0,m) p[i]=true;
  85. p[0]=false;
  86. p[1]=false;
  87. loop(i,2,sqrt(m)){
  88. for(int j=2*i;j<=m;j+=i){
  89. p[j]=false;
  90. }
  91. }
  92. // time complexity O(nlogn)
  93. return p;
  94. }
  95. ll nCr(ll n,ll r,int m=mod){
  96. if(n<r) return 0;
  97. ll *fac=getfactorial();
  98. ll ans=fac[n];
  99. ans=(ans*power(fac[n-r],m-2))%m;
  100. ans=(ans*power(fac[r],m-2))%m;
  101. return ans;
  102. }
  103.  
  104. //==================START==================
  105. ll arr[200005],b[200005];
  106. int n;
  107. struct seg{
  108. ll tree[800005];
  109. void build(int s,int e,int tn){
  110. if(s==e){
  111. tree[tn]=b[s];
  112. return ;
  113. }
  114. int m=(s+e)/2;
  115. build(s,m,2*tn);
  116. build(m+1,e,2*tn+1);
  117. tree[tn]=gcd(tree[2*tn],tree[2*tn+1]);
  118. }
  119. ll query(int s,int e,int l,int r,int tn){
  120. if(s>=l and e<=r) return tree[tn];
  121. if(s>r or e<l) return 0;
  122. int m=(s+e)/2;
  123. ll a=query(s,m,l,r,2*tn);
  124. ll b=query(m+1,e,l,r,2*tn+1);
  125. return gcd(a,b);
  126. }
  127. };
  128. ll get(int i,struct seg S){
  129. ll ans=0;
  130. ll l=i,r=n-1;
  131. while(l<=r){
  132. ll m=(l+r)/2;
  133. ll x=S.query(1,n-1,i,m,1);
  134. if(x>1){
  135. ans=max(ans,m-i+2);
  136. l=m+1;
  137. }
  138. else{
  139. r=m-1;
  140. }
  141. }
  142. return ans;
  143. }
  144. int main(){
  145. boost
  146. int t;
  147. cin>>t;
  148. while(t--){
  149. cin>>n;
  150. read(arr,n);
  151. shiftr(arr,n);
  152. loop(i,1,n-1) b[i]=abs(arr[i+1]-arr[i]);
  153. struct seg S;
  154. S.build(1,n-1,1);
  155. ll ans=0;
  156. loop(i,1,n-1){
  157. ans=max(ans,get(i,S));
  158. }
  159. cout<<ans<<endl;
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment