PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Setter: Daanish Mahajan
Tester: Tejas Pandey
Editorialist: Kanhaiya Mohan
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Print the value of f(N) = (0.143 \cdot N)^N rounded to the nearest integer. That is, if the actual value of f(N) is x,
- Print ⌊x⌋ if, x - ⌊x⌋ < 0.5
- Otherwise, print ⌊x⌋+1
EXPLANATION:
In this problem, we just need to implement the problem statement. The focus is on our ability to translate the problem statement into functioning error-free code.
Calculating f(N)
We can calculate f(N) either by running a loop N times or by using the power function from the library. Since the value of N is very small, it would take constant time to calculate f(N) using either way.
Round off to nearest integer
f(N) can be rounded to nearest integer either by checking the value of f(N) - ⌊f(N)⌋ or using the round function. It would take constant time to do this.
Most languages have power and round functions available. In languages like C++, we need to take special care of the floating point number.
TIME COMPLEXITY:
The time complexity is O(1) per test case.
SOLUTION:
Setter's Solution
#include<bits/stdc++.h>
using namespace std;
# define ld long double
int main()
{
int t; cin >> t;
int n;
while(t--){
cin >> n;
ld ans = 1, mul = (ld)0.143 * n;
for(int i = 1; i <= n; i++){
ans *= mul;
}
cout << round(ans) << endl;
}
}
Tester's Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
cout << (int)(pow(0.143*n, n) + 0.5) << "\n";
}
return 0;
}
Editorialist's Solution
testcases = int(input())
for testcase_idx in range(testcases):
n = int(input())
x = 0.143 * n
res = x ** n
ans = round(res)
print(ans)