QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#876481#9619. 乘积,欧拉函数,求和pengpeng_fudanTL 7ms5632kbC++232.4kb2025-01-30 21:49:332025-01-30 21:49:33

Judging History

你现在查看的是最新测评结果

  • [2025-01-30 21:49:33]
  • 评测
  • 测评结果:TL
  • 用时:7ms
  • 内存:5632kb
  • [2025-01-30 21:49:33]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int c=16;
int ct[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
ll dp[1<<16][2][2];
const ll M=998244353;
void solve() {
    int n;
    cin>>n;
    vector<pair<int,int>> a(n);
    for(int i=0;i<n;i++){
        ll x;
        cin>>x;
        a[i].second=x;
        for(int j=0;j<c;j++){
            while(x%ct[j]==0)   x/=ct[j];
        }
        if(x!=1)    a[i].first=x;
    }
    sort(a.begin(),a.end());
    dp[0][0][0]=1;
    for(int i=1;i<=n;i++){
        auto [u,x]=a[i-1];
        if(a[i-1].first==0||(i!=1&&a[i-1].first!=a[i-2].first)){
            for(int j=0;j<(1<<c);j++){
                dp[j][i&1][0]=dp[j][(i-1)&1][0]+dp[j][(i-1)&1][1];
                dp[j][i&1][1]=0;
            }
            auto dfs=[&](auto self,int j,int dep,int gx,int nxt)->void {
                if(dep==c)  {
                    dp[j|nxt][i&1][1]=(dp[j|nxt][i&1][1]+(dp[j][(i-1)&1][0]+dp[j][(i-1)&1][1])%M*((u==0?gx:gx/u*(u-1))%M)%M)%M;
                    return ;
                }
                self(self,j|(1<<dep),dep+1,gx,nxt);
                if(x%ct[dep]==0)    {
                    nxt|=(1<<dep);
                    gx=gx/ct[dep]*(ct[dep]-1);
                }
                self(self,j,dep+1,gx,nxt);
            };
            dfs(dfs,0,0,x,0);
            // if(i==2)    cerr<<dp[1][0][1]<<' '<<dp[1][0][0]<<'\n';
        }
        else{
            for(int j=0;j<(1<<c);j++){
                dp[j][i&1][0]=dp[j][(i-1)&1][0];
                dp[j][i&1][1]=dp[j][(i-1)&1][1];
            }
            auto dfs=[&](auto self,int j,int dep,int gx,int nxt)->void {
                if(dep==c)  {dp[j|nxt][i&1][1]=(dp[j|nxt][i&1][1]+dp[j][(i-1)&1][0]*((u==0?gx:gx/u*(u-1))%M+dp[j][(i-1)&1][1]*gx%M)%M)%M;return ;}
                self(self,j|(1<<dep),dep+1,gx,nxt);
                if(x%ct[dep]==0)    {
                    nxt|=(1<<dep);
                    gx=gx/ct[dep]*(ct[dep]-1);
                }
                self(self,j,dep+1,gx,nxt);
            };
            dfs(dfs,0,0,x,0);
        }
    }
    ll ans=0;
    for(int i=0;i<(1<<c);i++)   ans=(ans+dp[i][n&1][0]+dp[i][n&1][1])%M;
    cout<<(ans%M+M)%M;
}    	
signed main(){
    ios::sync_with_stdio(0),cin.tie(0); 
    int _ =1;
    // cin>>_;
    while(_--)  solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 5632kb

input:

5
1 6 8 6 2

output:

892

result:

ok single line: '892'

Test #2:

score: 0
Accepted
time: 6ms
memory: 5632kb

input:

5
3 8 3 7 8

output:

3157

result:

ok single line: '3157'

Test #3:

score: -100
Time Limit Exceeded

input:

2000
79 1 1 1 1 1 1 2803 1 1 1 1 1 1 1609 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2137 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 613 1 499 1 211 1 2927 1 1 1327 1 1 1123 1 907 1 2543 1 1 1 311 2683 1 1 1 1 2963 1 1 1 641 761 1 1 1 1 1 1 1 1 1 1 1 1489 2857 1 1 1 1 1 1 1 1 1 1 1 1 1 967 1 821 1 1 1 1 2143 1861...

output:

50965652

result: