C. Even Subarrays
题解
易得当区间异或和不为完全平方数的时候合法
朴素做法:
遍历所有区间,看看异或和是不是完全平方数
优化:
异或是可以交换运算顺序的,如果区间 \([l,r]\) 异或和为完全平方数,那么代表 \(pre[r] \oplus pre[l-1]==k\) 其中k为完全平方数
也就是说,\(pre[r] \oplus k== pre[l-1]\),这样的性质指引我们遍历r和所有的完全平方数,然后存储所有的 \(pre[l]\)
时间复杂度为 \(O(n\sqrt{n})\)
细节
1.用map会多一层log,所以要用数组,而不超过n的数的异或再怎么样也不会超过2n
2.异或符号优先级
code
#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) ((x)&(-x))
using namespace std;
const ll inf=1e18;
const ll mod=1e9+7;
void solve()
{
ll n;
cin>>n;
vector<ll> cnt(2LL*n+10000,0);
ll pre=0;
ll ans=0;
cnt[0]=1;
for(ll i=1;i<=n;i++)
{
ll x;
cin>>x;
pre^=x;
for(ll j=0;j*j<=2*n;j++)
{
if((pre^(j*j))>2*n) continue;
ans+=cnt[pre^(j*j)];
}
cnt[pre]++;
}
cout<<(n+1LL)*n/2LL-ans<<'\n';
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int TT=1;
cin>>TT;
while(TT--) solve();
return 0;
}