text: Count unimodal and non | C ++ program for the above approach ; Function to calculate the factorials up to a number ; Calculate the factorial ; Function to find power ( a , b ) ; Iterate until b exists ; If b is divisible by 2 ; Decrease the value of b ; Return the answer ; Function that counts the unimodal and non - unimodal permutations of a given integer N ; Function Call for finding factorials up to N ; Function to count unimodal permutations ; Non - unimodal permutation is N ! - unimodal permutations ; Driver Code ; Given Number N ; Function Call
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int mod = 1e9 + 7 ; const int mx = 1e6 ; int fact [ mx + 1 ] ; void Calculate_factorial ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i <= mx ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; fact [ i ] %= mod ; } } int UniModal_per ( int a , int b ) { long long int res = 1 ; while ( b ) { if ( b % 2 ) res = res * a ; res %= mod ; a = a * a ; a %= mod ; b /= 2 ; } return res ; } void countPermutations ( int n ) { Calculate_factorial ( ) ; int uni_modal = UniModal_per ( 2 , n - 1 ) ; int nonuni_modal = fact [ n ] - uni_modal ; cout << uni_modal << " ▁ " << nonuni_modal ; return ; } int main ( ) { int N = 4 ; countPermutations ( N ) ; return 0 ; }