题意:给L,R , 问【L,R】中最长的不含bad number 数的连续区间最长是多少
思路:枚举所有的n,因为是幂次,2^60≈1e18。 复杂度也就在3600不到的样子。有一点要注意的是枚举可能会爆LONG LONG,看了dalao们1A的代码。发现都有防爆LONG LONG的意识。
#include<bits/stdc++.h> #define PI acos(-1.0) #define pb push_back using namespace std; typedef long long ll; const int N=1e5+5; const int MOD=1e9+7; const int INF=0x3f3f3f3f; ll x,y,l,r; set<ll> st; vector<ll> te; ll mypow(ll x,int b){ ll ans=1; while(b){ if(b&1) ans*=x; b/=2; x*=x; } return ans; } int main(void){ cin >>x>>y>>l>>r; for(int a=0;a<=100;a++){ ll t1=mypow(x,a); if(t1>r) break; for(int b=0;b<=100;++b){ ll t2=mypow(y,b); if(t2>r) break; ll n=mypow(x,a)+mypow(y,b); if(n>r) break; else if(n>=l) st.insert(n); // cout<<"r="<<r<<" n="<<n<<endl; if(1e18/y<t2) break; } if(1e18/x<t1) break; } // st.insert(l); // st.insert(r); for(auto t: st) te.pb(t); ll mx=0; for(int i=0;i<(int)te.size()-1;++i) mx = max (te [i + 1] -te [i] -1, mx); if(te.size()>0){ if(st.find(r)==st.end()) mx=max(mx,r-*te.rbegin()); if(st.find(l)==st.end()) mx=max(mx,te[0]-l); } else{ mx=r-l+1; } cout << mx << endl; return 0; }