classSolution { using ll = longlong; const ll MOD = 1e9 + 7; // https://www.geeksforgeeks.org/breaking-integer-to-get-maximum-product/ /* The main function that returns the max possible product */ ll mypow(ll a, ll b){ // a ^ b if (b == 0) { return1; } elseif (b % 2 == 1) { return (mypow(a, b - 1) * a) % MOD; } else { auto x = mypow(a, b/2); return (x * x) % MOD; } } intmaxProd(ll n) { // n equals to 2 or 3 must be handled explicitly if (n == 2 || n == 3) return n;
// Keep removing parts of size 3 while n is greater than 4 ll res = 1; // while (n > 4) // { // n -= 3; // res = (res * 3) % MOD; // Keep multiplying 3 to res // } // 4 0 // 5 1 // 6 1 // 7 1 // 8 2 if (n >= 5) { constint step = ((n - 5) / 3 + 1); res = mypow(3, step); n -= step * 3; } return (n * res) % MOD; // The last part multiplied by previous parts } public: intmaxNiceDivisors(int primeFactors){ returnmaxProd(primeFactors); } };