1877D - Effects of Anti Pimples - CodeForces Solution


combinatorics number theory sortings

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
using namespace std;
#define int               long long
#define ll               long long
#define uniq(v)           (v).erase(unique(all(v)),(v).end())

//const long long INF=1e18;
//const int32_t M=1e9+7;
const int32_t MM=998244353;
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
void solve()
{
	int n;
	cin>>n;
	vector<int> nums(n);
	for(int i=0;i<n;i++)
	{
		cin>>nums[i];
	}
	vector<int> v(n);
	for(int i=0;i<n;i++)
	{
		int ma=INT_MIN;
		for(int j=i;j<n;j=j+(i+1))
		{
			ma=max(ma,nums[j]);
		}
		v[i]=ma;

	}


	

	// for(int i=0;i<n;i++)
	// {
	// 	cout<<v[i]<<" ";
	// }
	int ans=0;
	sort(v.begin(),v.end());

	for(int i=0;i<n;i++)
	{
		int t=expo(2,i,MM);
		int g=mod_mul(v[i],t,MM);

		int k=mod_add(g,ans,MM);
		//cout<<t<<" "<<g<<" "<<k<<endl;
		ans=k;
	}
	cout<<ans<<endl;


}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets