Atcoder AGC003D :Anticube(Pollard-Rho)

传送门

题解:
注意到不能同时选的数是一一对应的。
那么直接Pollard-Rho即可。

#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
typedef long long LL;
typedef __int128 IL;
typedef map <LL,LL> Map;
typedef Map :: iterator it_m;

const int RLEN=1<<18|1; const LL mod=998244353998244353LL;
inline char nc() {
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob) ? -1 : *ib++;
} 
inline LL rd() {
    char ch=nc(); LL i=0,f=1;
    while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();}
    while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}
    return i*f;
}
inline LL mul(LL a,LL b,LL md) {return (IL)a*b%md;}
inline LL power(LL a,LL b,LL md,LL rs=1) {for(;b;b>>=1,a=mul(a,a,md)) if(b&1) rs=mul(rs,a,md); return rs;}

Map s1,s2; int bz=0;
namespace RHO {
    const int pr[]={2,3,5,7,11,13,17,19,23};
    vector <LL> vec;
    inline LL gcd(LL a,LL b) {return b ? gcd(b,a%b) : a;}
    inline LL rho(LL nn,LL c) {
        LL x=rand()%nn+1, y=x, k=2;
        for(int i=1;;i++) {
            x=(mul(x,x,nn)+c)%nn;
            LL p=gcd(abs(y-x),nn);
            if(p!=1) return p;
            if(i==k) k<<=1, y=x;
        }
    }
    inline bool mr(LL nn) {
        if(nn<=3) return true;
        if((nn%6)!=1 && (nn%6)!=5) return false;
        LL s1=nn-1, s2=0;
        while(!(s1&1)) s1>>=1, ++s2;
        for(int i=0;i<=8;i++) {
            if(nn==pr[i]) return true;
            LL p=power(pr[i],s1,nn);
            for(int j=1;j<=s2 && p!=1;++j) {
                LL t=mul(p,p,nn);
                if(t==1 && p!=nn-1 && p!=1) return false;
                p=t;
            }
            if(p!=1) return false;
        } return true;
    }
    inline void solve(LL nn) {
        if(mr(nn)) {vec.push_back(nn%mod); return;}
        LL g=rho(nn,rand()%nn); 
        while(g==nn) g=rho(nn,rand()%nn);
        solve(nn/g); solve(g);
    }
    inline void fac(LL nn) {
        if(nn==1) return (void)(bz++);
        vec.clear();
        solve(nn);
        sort(vec.begin(),vec.end());
        LL a1=1, a2=1;
        for(int i=0,lst=0;i<vec.size();++i) {
            ++lst;
            if(i!=vec.size()-1 && vec[i+1]==vec[i]) continue;
            lst%=3;
            if(lst) a1=mul(a1,power(vec[i]%mod,lst,mod),mod), a2=mul(a2,power(vec[i]%mod,3-lst,mod),mod);
            lst=0;
        } 
        if(a1==1) return (void)(bz++);
        s2[a1]=a2; s2[a2]=a1; ++s1[a1];
    }
}

int main() {
    int n=rd();  srand(time(0));
    for(int i=1;i<=n;i++) RHO::fac(rd());
    int ans=0;
    for(it_m it=s1.begin();it!=s1.end();++it) 
        if((it->second)>s1[s2[it->first]] || ((it->second)==s1[s2[it->first]] && it->first>=s2[it->first])) ans+=it->second;
    cout<<ans+(bz>0)<<'\n'; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值