Use Fast Input Output(I/O)
Just use “\n” instead of endl. I know this is ridiculous, as I had been stuck with this damn tle for 30 mins, later to realise that I have to use \n.
Believe me i first got WA ,then i optimize my solution then submit again got WA ,then i used fast i/o,result still WA.But once one of my friend told me when you got TLE due to printing only then you should use ‘\n’ instead of endl ,in this case TLE comes down to AC(0.02sec).
AC solution.
I think time limit was too constrained.
I applied same as editorial, still tle.
Well I used a very different method though.
I precomputed for answers till 10^6 and stored in a array.
If we carefully observe for a particular n the ans is (n!)^n/(1^1×2^2×3^3…n^n).
Thus I made an array to get factorial and (1^1×2^2×3^3…n^n) , so that I can get the value for any n in O(1).
Then I make a 3rd array which stored all the anwers for every n by applying power function and modulo inverse of 2nd array nth element for which I m searching ans.
Here’s my solution. ![]()
![]()
https://www.codechef.com/viewsolution/26693126
What do u think @s5960r ![]()
Use log2l instead of log2
why dividing into three loops which you could just done in one loop?
Nice Bro ! ![]()
I saw your answer ! Its awesome but modulo Inverse was too much for this question! ![]()
because for short contest, i believe it’s optimal to find the fast to implement answer!
Also a quicker observation would be that, for any given n answer is =n!*(n-1)!....1
This is my solution CodeChef: Practical coding for everyone
It’s short (only 24 lines). ![]()
and also works in O(n)
lol . i never knew that the diff between an AC and TLE could be so naive !
@istillovebravp
You can do
#define endl '\n'
I have added this to my template, to keep me protected against these situation🤣
dude as it was a 2.5 hour contest the first idea i get i implement it as fast as i can , if i would have time to think more i would have done in 1 loop as well.
Nice observation though mate 
yeah but that contest is yesterday night but you solved today ?? 
Lol. Gonna include it in my temp as well.
no im reading that editorial now . im not that level though
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
#define mod 1000000007
typedef long long ll;
int main() {
int t;
cin>>t;
ll fact[MAX+1];
ll dp[MAX+1];
fact[0] = 1;
dp[0] = 1;
for(ll i=1;i<=MAX;i++) {
fact[i] = (i*fact[i-1])%mod;
dp[i] = (fact[i]*dp[i-1])%mod;
}
while(t--) {
int n;
cin>>n;
cout<<dp[n]<<'\n';
}
}
whats wrong with this,it gives TLE
Use fast I/O (20 chars)
Another way to think about it in terms of f(x):
f(x) = f(x-1)*(1*2*3...*x) = f(x-1)*(x!)
