# PROBLEM LINK:

Author: Jyothi Surya Prakash Bugatha
Tester: Aryan Choudhary
Editorialist: Nandini Kapoor

Cakewalk

# PREREQUISITES:

Math, Fermats’s Little Theorem, Combinatorics

# PROBLEM:

Given a sequence A of length N, the weight of this sequence is defined as weight(A)=\sum_{i=1}^{N}i⋅A_i. Given an integer N, we pick a permutation A of [1,2…N] uniformly at random. What is the expected weight of this permutation? It can be shown that this can be represented as P/Q where P and Q are co prime integers and Q≠0 mod 998244353. Print the value of P⋅Q−1 modulo 998244353.

# EXPLANATION:

weight(A) for a permutation A from 1 to N as mentioned in the problem is given by -

weight(A) = 1\times A_1 + 2\times A_2 + \dots + n\times A_n

This in turn is calculated for all possible permutations A, let the evaluation of the value weight(A) for the i_{th} permutation be w_i (where 0\lt i\leq N!). We are required to calculate the expected value of weight of such a randomly picked permutation, this will be given by -

E = (w_1+w_2+\dots +w_{n!})/(n!)

If we fix the i_{th} element of A to a value j (where 0\lt j\leq N), there will be (N-1)! permutations possible by changing positions of other elements. In simpler words, there will be (N-1)! weights in E whose corresponding permutation A had the number j at the i_{th} position, meaning i\times j is contributing its value (N-1)! times to E.

For example consider N=3, in this case if we fix A_i=j for j=1 and i from 1 to N we observe -

1. i=1, j=1
A_1=1 -
A=[1, 2, 3], weight(A)=1+2\times 2+3\times 3
A=[1, 3, 2], weight(A)=1+2\times 3+3\times 2

2. i=2, j=1
A_2=1 -
A=[2, 1, 3], weight(A)=2+2\times 1+3\times 3
A=[3, 1, 2], weight(A)=3+2\times 1+3\times 2

3. i=3, j=1
A_3=1 -
A=[2, 3, 1], weight(A)=2+2\times 3+3\times 1
A=[3, 2, 1], weight(A)=3+2\times 2+3\times 1

We can observe that for each value of i from 1 to N, i\times j= i\times 1 is present in the weight of (N-1)!=(3-1)!=2 permutations of A.

Now we’ve established that each number j will appear at a position i (0<i\leq N), (N-1)! times. Each number j from 1 to N can contribute i\times j to the expected value (N-1)! times. We can write it as follows -

1\times i\times (N-1)! + 2\times i\times (N-1)! + 3\times i\times (N-1)! + \dots N\times i\times (N-1)!
=i\times (N-1)!\times (1+2+3+\dots +N)
=i\times (N-1)!\times ((N)(N+1)/2)

where i can assume values between 1 to N for each value of j as well, we can write this as -

1\times (N-1)!\times ((N)(N+1)/2) + 2\times (N-1)!\times ((N)(N+1)/2) + 3\times (N-1)!\times ((N)(N+1)/2) + \dots N\times (N-1)!\times ((N)(N+1)/2)
= (1+2+3+\dots +N)\times (N-1)!\times ((N)(N+1)/2)
= (N\times (N+1)/2)\times (N-1)!\times ((N)(N+1)/2)
= (N!\times N\times (N+1)^2)/4

We have above obtained the sum of weights of all N! permutations of A, to calculate the expected value E now we need to divide this sum by the number of permutations possible -

(N!\times N\times (N+1)^2)/(4\times N!)
=(N\times (N+1)^2)/4

Out of the components of the answer above, N\times (N+1)/2 will always yield an integral quotient which is why we won’t be taking the modular multiplicative inverse of 4 that is currently in the denominator. We can divide it into 2 parts -

1. The part yielding integral quotient
2. The part that may or may not leave a remainder on division

as follows -

(N\times (N+1)/2)
\times
(N+1)/2

If we compare it to P/Q form where P and Q are co prime integers, we can claim that either

1. P=N\times (N+1)^2/2 and Q=2 or
2. P=N\times (N+1)^2/4 and Q=1

Now if N+1 is found to be divisible by 2, we can simply multiply P by 1^{−1} modulo (998244353). On the other hand if P is not divisible by 2, we will have to multiply it with 2^{−1} modulo (998244353). This leads us to our final answer -

(N(N+1)^2/2)\times 2^{-1}modulo(998244353) if N is even, or
(N(N+1)^2/4)\times 1^{-1}modulo(998244353) if N is odd

# TIME COMPLEXITY:

O(1) per test case, because computing modular multiplicative inverse i.e. Q^{-1}modulo(998244353) takes O(log(998244353)) time which is a constant.

# SOLUTIONS:

Setter
Tester
/* in the name of Anton */

/*
Compete against Yourself.
Author - Aryan (@aryanc403)
Atcoder library - https://atcoder.github.io/ac-library/production/document_en/
*/

#ifdef ARYANC403
#include <header.h>
#else
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma GCC optimize ("-ffloat-store")
#include<bits/stdc++.h>
#define dbg(args...) 42;
#endif

// y_combinator from @neal template https://codeforces.com/contest/1553/submission/123849801
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template<class Fun> class y_combinator_result {
Fun fun_;
public:
template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }

using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"

typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403()
{
#ifdef ARYANC403
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time-start_time;
cerr<<"Time Taken : "<<diff.count()<<"\n";
#endif
}

long long readInt(long long l, long long r, char endd) {
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true) {
char g=getchar();
if(g=='-') {
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g&&g<='9') {
x*=10;
x+=g-'0';
if(cnt==0) {
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd) {
if(is_neg) {
x=-x;
}
assert(l<=x&&x<=r);
return x;
} else {
assert(false);
}
}
}
string readString(int l, int r, char endd) {
string ret="";
int cnt=0;
while(true) {
char g=getchar();
assert(g!=-1);
if(g==endd) {
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt&&cnt<=r);
return ret;
}
long long readIntSp(long long l, long long r) {
return readInt(l,r,' ');
}
long long readIntLn(long long l, long long r) {
return readInt(l,r,'\n');
}
string readStringLn(int l, int r) {
return readString(l,r,'\n');
}
string readStringSp(int l, int r) {
return readString(l,r,' ');
}

void readEOF(){
assert(getchar()==EOF);
}

vi readVectorInt(int n,lli l,lli r){
vi a(n);
for(int i=0;i<n-1;++i)
a[i]=readIntSp(l,r);
a[n-1]=readIntLn(l,r);
return a;
}

// #include<atcoder/dsu>
// vector<vi> readTree(const int n){
//     vector<vi> e(n);
//     atcoder::dsu d(n);
//     for(lli i=1;i<n;++i){
//         const lli u=readIntSp(1,n)-1;
//         const lli v=readIntLn(1,n)-1;
//         e[u].pb(v);
//         e[v].pb(u);
//         d.merge(u,v);
//     }
//     assert(d.size(0)==n);
//     return e;
// }

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
auto jt=m.find(x);
if(jt==m.end())         m.insert({x,cnt});
else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
auto jt=m.find(x);
if(jt->Y<=cnt)            m.erase(jt);
else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;
// const lli maxN = 1000000007L;

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}

struct barrett {
unsigned int _m;
unsigned long long im;

explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

unsigned int umod() const { return _m; }

unsigned int mul(unsigned int a, unsigned int b) const {

unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned int v = (unsigned int)(z - x * _m);
if (_m <= v) v += _m;
return v;
}
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}

constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};

long long s = b, t = a;
long long m0 = 0, m1 = 1;

while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0) m0 += b / s;
return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}

unsigned long long y_max = a * n + b;
if (y_max < m) break;
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;

public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}

static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}

unsigned int val() const { return _v; }

mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}

mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }

mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}

friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}

private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;

public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}

dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}

unsigned int val() const { return _v; }

mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}

mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }

mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}

friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}

private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using namespace atcoder;
using mint = modint998244353;
//using mint = modint1000000007;
using vm = vector<mint>;
std::ostream& operator << (std::ostream& out, const mint& rhs) {
return out<<rhs.val();
}

lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
lli m;
string s;
vi a;
//priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

int main(void) {
ios_base::sync_with_stdio(false);cin.tie(NULL);
// freopen("txt.in", "r", stdin);
// freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
T=readIntLn(1,2e5);
while(T--)
{

const int n=readIntLn(1,1e9);
mint ans=1;
ans*=n;
ans*=n+1;
ans*=n+1;
ans/=4;
cout<<ans<<endl;
}   aryanc403();
readEOF();
return 0;
}

Editorialist
#include<bits/stdc++.h>
using namespace std;

#define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
#define mod 998244353
#define int long long

//______________________________z_____________________________

int power(int x, int y, int m)
{
if(y == 0) return 1;
int p = power(x, y / 2, m) % m;
p = (p * p) % m;
if(y % 2 == 0) return p;
else return (x * p) % m;
}

int modInverse(int a, int m)
{
int g = __gcd(a, m);
return power(a, m - 2, m);
}

void solve() {
int n, k;
cin >> n;
int num = n * (n + 1) / 2;
num %= mod;
if((n + 1) % 2 != 0) num *= (n + 1);
else num *= (n + 1) / 2;
num %= mod;
int modinv = modInverse((n + 1) % 2 + 1, mod);
cout << (modinv * num) % mod << endl;
}

int32_t main()
{
_z;
int t=1;
cin>>t;
while(t--) {
solve();
}
}


6 Likes

Beautiful Explanation

2 Likes

why is this WA, i did the same thing.
https://www.codechef.com/viewsolution/54417400

2 Likes

The maximum value of long in java is 2^{63} which is 10^{19} for n=10^8 you numerator won’t have correct values. Print its value and see. This type of bug is often called int overflow.

To get an AC replace that num calculation line into 3 lines taking mod each time. Like -

long num=1;
num=(num*n)%mod;
num=(num*n)%mod;
num=(num*(n+1))%mod;

4 Likes

Nice Explanation buddy !

1 Like

This problem could be solved in a less tedious manner by using Linearity of Expectation.

my c++ solution still gives wrong answer even after handling these overflows
solution:
my code

You have got an AC so I’m assuming you were able to fix the issue.

ya i guess i got it … sorry for the bother

I reckon it will yield a long expression with cancellations or summation formulas to be applied as well, I may be wrong though since I didn’t think of it from that perspective yet. Anywhooo I think it’s always wonderful when people propose different solutions, almost always insightful

1 Like

Can anyone please tell me why I am getting wrong answer.
My Solution link:- Solution: 54566593 | CodeChef

Hi @aryanc403 I modified my submission as you suggested but still getting WA on Task 2.
Can you let me know why.

1 Like

Lol I did not even come close to the Author’s solution. I solved it during practice and the first 10 mins I was just thinking how is this the First problem ??!!

So I made some observations and found a pattern in the answer.

For N = 1, 2, 3, 4, 5, 6, 7…
The Answer was 1, 4.5, 12, 25, 45, 73.5, 112…

Then I found that this is coming out to be a Cubic Progression.

1---------- 4.5---------- 12---------- 25 ---------- 45 ---------- 73.5 ---------- 112
----3.5 ---------- 7.5 ---------- 13 ---------- 20---------- 28.5 ---------- 38.5
-------------- 4 ---------- 5.5---------- 7---------- 8.5------------- 10
-------------------1.5---------- 1.5 ---------- 1.5 ---------- 1.5 <----- Final Difference

And with that I found how to solve it with the image below.

My Solution :- Solution

3 Likes