Codeforces 1009E

题意略。

思路:

比如现在n = 11。那么我们观察a[1.....n]的出现次数:

a[1]:2 ^ 10 + 10 * 2 ^ 9

a[2]:2 ^ 9 + 9 * 2 ^ 8

a[3]:2 ^ 8 + 8 * 2 ^ 7

.........

a[x]:2 ^ (n - x) + (n - x) * 2 ^ (n - x - 1)

凭借这个规律,我们可以通过快速幂求得答案。

详见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 998244353;
const LL maxn = 1e6 + 5;

LL an[maxn];

LL qmod(LL a,LL n){
    LL ret = 1;
    while(n){
        if(n & 1) ret = ret * a % mod;
        n = n>>1;
        a = a * a % mod;
    }
    return ret;
}

int main(){
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;++i) scanf("%lld",&an[i]);
    LL ans = 0;
    for(int i = 1;i < n;++i){
        LL temp1 = qmod(2,n - i);
        LL temp2 = LL(n - i) * qmod(2,n - i - 1) % mod;
        ans += (temp1 + temp2) % mod * an[i] % mod;
    }
    ans += an[n];
    ans %= mod;
    printf("%lld\n",ans);
    return 0;
}

现在反过来想想,为什么可以这么去计算呢?

就是因为它要求的是所有情况的总和,这样我们才可以一个个把它们单独拿出来考虑。

猜你喜欢

转载自www.cnblogs.com/tiberius/p/9383189.html