1143B - Nirvana - CodeForces Solution


brute force math number theory *1200

Please click on ads to support us..

Python Code:

from re import sub


n = int(input())

digits = len(str(n))
result = 0
for i in range(digits):
    sub_result = 9 ** i
    sub_n = n // (10 ** i)
    remainder_n = n % (10 ** i)
    should_be_reduce = False
    temp_n = n
    for j in range(i):
        if (temp_n % 10 != 9):
            should_be_reduce = True
        temp_n /= 10
    while (sub_n > 0): 
        if (should_be_reduce):
            if (sub_n % 10 == 0):
                sub_result *= 9
            else:    
                should_be_reduce = False
                sub_result *= max(1, ((sub_n % 10) - 1))
        else:
            sub_result *= ((sub_n % 10))
        sub_n //= 10
    if (sub_result > result):
        result = sub_result
print(result)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int solve(int n)
{
	if(n<10)
	{
		return max(1, n);
	}
	
    return max(n%10*solve(n/10), solve(n/10-1)*9);
}

int main()
{
	int n;
	cin>>n;
	cout<<solve(n)<<endl;
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses