质因数分解+状态压缩求完全平方数

例题https://www.luogu.com.cn/problem/P10724

小性质:完全平方数的质因子出现数量应该为偶数,因此可以用异或去判断是否为偶数

前缀异或和性质:
因为:axora=0,而且异或满足交换律。
所以当前的前缀异或sxor之前出现过,说明中间的那些x都被抵消掉了,也就是中间子段的异或为0了。
如:5 = 5 xor 2 xor 3 xor 4 xor 2 xor 3 xor 4;

#include<bits/stdc++.h>
#define endl '\n'
#define lowbit(x) (x&-x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double pi=acos(-1);

int num[15]={2,3,5,7,11,13,17,19,23,29};
int n;

ll ck(int x){
    ll res=0;
    for(int i=0;i<10;i++){
        if(num[i]>x) break;
        while(x%num[i]==0){
            res^=(1<<i);
            x/=num[i];
        }
    }
    return res;
}

void solve(){
    cin>>n;
    vector<ll> pre(n+1);
    map<ll,ll> mp;
    ll ans=0;
    mp[0]=1;
    for(int i=1;i<=n;i++){
        cin>>pre[i];
        pre[i]=ck(pre[i])^pre[i-1];
        ans+=mp[pre[i]];
        mp[pre[i]]++;
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}

posted on   TaopiTTT  阅读(18)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示