結果

問題 No.2620 Sieve of Coins
ユーザー 👑 potato167potato167
提出日時 2024-01-26 23:06:57
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 524 ms / 2,000 ms
コード長 3,462 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 214,064 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2024-09-28 08:57:31
合計ジャッジ時間 11,782 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
const ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a,bool upp=0){if(a) cout<<(upp?"YES\n":"Yes\n"); else cout<<(upp?"NO\n":"No\n");}
template<class T> void vec_out(vector<T> &p,int ty=0){
if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}

//Mobius(n)=0  exists p n|p*p
//         =-1  if n=p_1*p_2*...*p_{2a+1}
//         =1   if n=p_1*p_2*...*p_{2a}
std::vector<int> Mobius(int N){
    std::vector<int> p(N+1,-2);
    p[1]=1;
    for(int i=2;i<=N;i++){
        if(p[i]!=-2) continue;
        p[i]=-1;
        if(i<=5) p[i]=0;
        for(int j=i*2;j<=N;j+=i){
            if(p[j]==-2) p[j]=p[i];
            else p[j]*=p[i];
        }
        if(N/i<i) continue;
        for(int j=i*i;j<=N;j+=i*i) p[j]=0;
    }
    for(int i=1;i<=N;i++){
        if(p[i]==-2) p[i]=-1;
    }
    return p;
}


void solve();
// oddloop
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t=1;
    //cin>>t;
    rep(i,0,t) solve();
}

void solve(){
    ll L,N;
    cin>>L>>N;
    int D=42;
    vector<ll> p={2,3,5};
    ll Z=1001001;
    auto B=Mobius(Z);
    vector dp(D,vector(D,vector<ll>(D)));
    rep(i,0,N){
        ll a;
        cin>>a;
        vector<ll> q(3);
        rep(j,0,3) while(a%p[j]==0) q[j]++,a/=p[j];
        rep(j,0,8){
            auto r=q;
            rep(k,0,3) if(j&(1<<k)) r[k]++;
            dp[r[0]][r[1]][r[2]]++;
        }
    }
    ll ans=0;
    auto mul=[&](ll a,ll b) -> ll {
        if(ILL/b<=a) return ILL;
        return a*b;
    };
    rep(i,0,D) rep(j,0,D) rep(k,0,D){
        if(dp[i][j][k]%2==0) continue;
        ll Y=1;
        vector<int> q={i,j,k};
        //vec_out(q);
        rep(l,0,3) rep(rprp,0,q[l]) Y=mul(Y,p[l]);
        rep(l,0,8){
            ll X=Y;
            ll s=1;
            rep(r,0,3){
                if(l&(1<<r)) X=mul(X,p[r]),s*=-1;
            }
            rep(r,1,Z+1){
                ll tmp=mul(mul(X,r),r);
                if(tmp>L) break;
                ans+=B[r]*s*(L/tmp);
            }
        }
    }
    cout<<ans<<"\n";
}
0