QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#749885#9619. 乘积,欧拉函数,求和crychicWA 846ms7104kbC++173.4kb2024-11-15 11:09:042024-11-15 11:09:04

Judging History

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

  • [2024-11-15 11:09:04]
  • 评测
  • 测评结果:WA
  • 用时:846ms
  • 内存:7104kb
  • [2024-11-15 11:09:04]
  • 提交

answer

#include<bits/stdc++.h>

using  namespace std;
using ll = long long;
const int P = 998244353;
const int limit = 15;
const int N = 3010;
vector<int> p = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
int invp[limit];
vector<int> G[N];
int sumval[1 << limit];
ll qpow(ll a,ll b){
    ll res = 1;
    while(b){
        if(b & 1) res = res * a % P;
        a = a * a % P;
        b >>= 1;
    }
    return res;
}
void solve(){
    // 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53
    int n;
    cin >> n;
    vector<int> a(n);
    vector dp(1 << limit,vector<int>(2,0));
    for(int i = 0;i < n ;i ++){
        cin >> a[i];
    }
    for(int i = 0;i < n;i ++){
        int val = a[i];
        // if(val == 1) continue;
        for(int j = 0;j < limit;j ++){
            while(val % p[j] == 0) val /= p[j];
        }
        G[val].push_back(i);
    }    
    dp[0][0] = 1;
    vector todp = dp;
    for(int x : G[1]){
        todp = dp;
        int tmp = 0;
        int val = a[x];
        for(int i = 0;i < limit;i ++){
            if(val % p[i] == 0) tmp |= (1 << i);
        } 
        for(int i = 0;i < (1 << limit);i ++){
            todp[i | tmp][0] += (ll)dp[i][0] * val % P * sumval[tmp & (~i)] % P; 
            // cout << "! " << tmp << ' ' << i << ' ' << (tmp & (~i)) << ' ' << sumval[tmp & (~i)] << '\n'; 
            // if(todp[i][0])cout << a[x] << ' ' << i << ' ' << todp[i][0] << '\n';
            if(todp[i | tmp][0] >= P) todp[i | tmp][0] -= P;
        }
        dp = todp;
        // dp[0][0] --;
    }
    for(int k = 2;k < 3000;k ++){
        int tval = qpow(k, P - 2) * (k - 1) % P;
        for(int x : G[k]){
            todp = dp;    
            int tmp = 0;
            int val = a[x];
            // cout << (ll)tval * val % P << '\n';
            for(int i = 0;i < limit;i ++){
                if(val % p[i] == 0) tmp |= (1 << i);
            } 
            for(int i = 0;i < (1 << limit);i ++){
                todp[i | tmp][1] += (ll)dp[i][0] * tval % P * val % P * sumval[tmp & (~i)] % P;
                // cout << "! " << tmp << ' ' << i << ' ' << (tmp & (~i)) << ' ' << sumval[tmp & (~i)] << '\n'; 
                if(todp[i | tmp][1] >= P) todp[i | tmp][1] -= P;
                todp[i | tmp][1] += (ll)dp[i][1] * val % P * sumval[tmp & (~i)] % P;
                if(todp[i | tmp][1] >= P) todp[i | tmp][1] -= P;
                // cout << (i | tmp) << ' ' << i << ' ' << tmp << ' ' << todp[i | tmp][0] << ' ' << todp[i | tmp][1] << '\n'; 
                // todp[i | tmp][1] %= P;
            }
            dp = todp;
        }
        if(G[k].size()){
            for(int i = 0;i < (1 << limit);i ++){
                dp[i][0] += dp[i][1];
                dp[i][1] = 0;
                if(dp[i][0] >= P) dp[i][0] -= P;
            }
        }
    }
    ll ans = 0;
    for(int i = 0;i < (1 << limit);i ++){
        ans += dp[i][0];
        if(ans >= P) ans -= P;
    }
    cout << ans << '\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t = 1;
    // cin >> t;
    // cout << 57 * 53 << '\n';
    for(int i = 0;i < limit;i ++){
        invp[i] = qpow(p[i], P - 2);
    }
    for(int i = 0;i < (1 << limit);i ++){
        sumval[i] = 1;
        for(int j = 0;j < limit;j ++){
            if(i >> j & 1){
                sumval[i] = (ll)sumval[i] * (p[j] - 1) % P * invp[j] % P;
            }
        }
    }
    while(t --){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 6996kb

input:

5
1 6 8 6 2

output:

892

result:

ok single line: '892'

Test #2:

score: 0
Accepted
time: 7ms
memory: 6992kb

input:

5
3 8 3 7 8

output:

3157

result:

ok single line: '3157'

Test #3:

score: 0
Accepted
time: 842ms
memory: 7104kb

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:

ok single line: '50965652'

Test #4:

score: -100
Wrong Answer
time: 846ms
memory: 7036kb

input:

2000
1 1 1 1 1 1 1 1 353 1 1 1 557 1 1 1 1 1 1 1 1 1 1 1 1423 1 1 1327 1 1 1 907 1 1 1 1 1 2971 1 1 1 2531 1 1 1 1 1 1 1 1 1 2099 1 1 1 1 1 1 1 1 1 1 1 1 1 1 199 2999 1 727 1 1 1 1 1 1 1 71 1 1 1 1 1 1 2503 1 176 1 1 1 1 1 1 1361 1013 1 1 1 1 1 1 1 2699 1 1 1 1 1 1 1 1 1 2897 1 1 1 1 1637 1 1 1367 1...

output:

637200514

result:

wrong answer 1st lines differ - expected: '420709530', found: '637200514'