Stay Hungry,Stay Foolish!

D - Another Sigma Problem

D - Another Sigma Problem

https://atcoder.jp/contests/abc353/tasks/abc353_d

 

思路

前缀和 + 快速幂

https://zhuanlan.zhihu.com/p/697255076

 

Code

https://atcoder.jp/contests/abc353/submissions/53514365

typedef long long ll;

ll pow(ll x, ll n)
{
    if(n == 0) return 1;
    if(n == 1) return x;
    ll half = pow(x, n / 2);
    ll rest = pow(x, n % 2);
    return half*half*rest;
}

ll BASE = 998244353;

int n;
vector<long long> a;
vector<long long> alen;
vector<long long> prefix_sum;

int main()
{
    cin >> n;
    
    a.resize(n);
    alen.resize(n);
    prefix_sum.resize(n);

    auto get = [&](ll x){
        ll cnt = 0;
        while(x){
            cnt ++;
            x /= 10;
        }
        return cnt;
    };

    for(int i=0; i<n; i++){
        cin >> a[i];
        
        alen[i] = get(a[i]);
        
        if (i==0){
            prefix_sum[i] = a[i];
        } else {
            prefix_sum[i] = prefix_sum[i-1] + a[i];
        }
    }

//    cout << "--------aaa------" << endl;
    
    long long ans = 0;

    for(int i=1; i<n; i++){
        long long contr = 0;
        
        /*
            aj+ai | 0 =< j < i
            ai contribute ai*i
            aj make prefix_sum[i-1]
            
            so for every ai, it contributes by two parts:
            prefix_sum[i-1] * 1e(strlen(ai))
            +
            ai * i
        */
        
        contr += ((i-0) * a[i]) % BASE;
        contr %= BASE;
        
        ll pw = pow(10, alen[i]);
        pw %= BASE;
        
        ll prefix_sum_mode = prefix_sum[i-1] % BASE;
        
        contr += (prefix_sum_mode * pw) % BASE;
        contr %= BASE;
        
        ans += contr;
        
        ans %= BASE;
    }

    cout << ans << endl;

    return 0;
}

 

posted @ 2024-05-16 20:30  lightsong  阅读(3)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel