結果

問題 No.2717 Sum of Subarray of Subsequence
ユーザー Shreyan RayShreyan Ray
提出日時 2024-04-05 21:58:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 2,435 bytes
コンパイル時間 1,829 ms
コンパイル使用メモリ 207,404 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-05 21:58:14
合計ジャッジ時間 4,176 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 103 ms
6,676 KB
testcase_10 AC 104 ms
6,676 KB
testcase_11 AC 104 ms
6,676 KB
testcase_12 AC 118 ms
6,676 KB
testcase_13 AC 115 ms
6,676 KB
testcase_14 AC 104 ms
6,676 KB
testcase_15 AC 102 ms
6,676 KB
testcase_16 AC 104 ms
6,676 KB
testcase_17 AC 106 ms
6,676 KB
testcase_18 AC 102 ms
6,676 KB
testcase_19 AC 119 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 105 ms
6,676 KB
testcase_23 AC 93 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int facN = 1e6 + 5;
const int mod = 998244353;
int ff[facN], iff[facN];
bool facinit = false;

int power(int x, int y){
	if (y == 0) return 1;

	int v = power(x, y / 2);
	v = 1LL * v * v % mod;

	if (y & 1) return 1LL * v * x % mod;
	else return v;
}

void factorialinit(){
	facinit = true;
	ff[0] = iff[0] = 1;

	for (int i = 1; i < facN; i++){
		ff[i] = 1LL * ff[i - 1] * i % mod;
	}

	iff[facN - 1] = power(ff[facN - 1], mod - 2);
	for (int i = facN - 2; i >= 1; i--){
		iff[i] = 1LL * iff[i + 1] * (i + 1) % mod;
	}
}

int C(int n, int r){
	if (!facinit) factorialinit();

	if (n == r) return 1;

	if (r < 0 || r > n) return 0;
	return 1LL * ff[n] * iff[r] % mod * iff[n - r] % mod;
}

int P(int n, int r){
	if (!facinit) factorialinit();

	assert(0 <= r && r <= n);
	return 1LL * ff[n] * iff[n - r] % mod;
}

int Solutions(int n, int r){
	//solutions to x1 + ... + xn = r 
	//xi >= 0

	return C(n + r - 1, n - 1);
}

void Solve() 
{
    int n; cin >> n;

    vector <int> a(n + 1);
    for (int i = 1; i <= n; i++){
    	cin >> a[i];
    }

    int ans = 0;

    auto get = [&](int x){
    	int v = x * power(2, x - 1) % mod;
    	v += power(2, x);
    	v %= mod;
    	return v;
    };

    for (int i = 1; i <= n; i++){
    	int v = get(n - i);

    	// for every element in left side add value 
    // 	int ways = v;
    // 	ways += (i - 1) * v % mod * (mod + 1) / 2;

    // 	ways %= mod;
    
        int ways = v * power(2, i - 1) % mod;
        if (i != 1)
        ways += (i - 1) * v % mod * power(2, i - 2) % mod;
        ways %= mod;
    	
    //	cout << ways << "\n";
    	ans += ways * a[i];
    	ans %= mod;
    }

    cout << ans << "\n";
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // freopen("in",  "r", stdin);
    // freopen("out", "w", stdout);
    
   // cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
0