这题假得不行啊…一直感觉 O ( n l o g n ) O(nlogn) O(nlogn)是过不了的,结果TC评测机太强了啊,1e7带个log才200+ms就跑过去了。。所以说要有信仰啊。。。
好的其实这题就是纯暴力,复杂度证明我就直接从网上粘一个过来了。。
Let’s look at the max number in the range. Then, we have a recurrence T(n) = min(a,b) + T(a) + T(b), where a+b+1=n. By the “small-to-large” principle, T(n) is O(n log n), so the answer is always small.
#include <bits/stdc++.h>
#define ll long long
#define fr(i,x,y) for(int i=x;i<=y;i++)
#define rf(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
const int p=1e9+7;
template<class T> void checkmin(T &a,const T &b) { if (b<a) a=b; }
template<class T> void checkmax(T &a,const T &b) { if (b>a) a=b; }
class LimitedMemorySeries2 {
public:
int getSum( int n, long long x0, long long a, long long b ) ;
};
inline void Add(int &x,int y){
x+=y;
while(x>=p) x-=p;
}
inline ll nxt(ll x0,ll a,ll b){
return ((x0^a)+b)&((1LL<<50)-1);
}
inline ll pre(ll x,ll a,ll b){
return x<b?(x+(1LL<<50)-b)^a:(x-b)^a;
}
int LimitedMemorySeries2::getSum(int n, long long x0, long long a, long long b) {
ll x=x0;
int ans=0;ll q,w;
for(int i=1;i<=n;i++){
int s=0;
for(q=pre(x,a,b),w=nxt(x,a,b);i-s>1&&i+s<n&&q<x&&w<x;q=pre(q,a,b),w=nxt(w,a,b)) s++;
//cout<<pre(x,a,b)<<' '<<x<<endl;
Add(ans,s);
x=nxt(x,a,b);
}
return ans;
}