Count ways to express even number â €˜ nâ €™ as sum of even integers | C ++ program to count ways to write number as sum of even integers ; Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Return number of ways to write ' n ' as sum of even integers ; Driver code
#include <iostream> NEW_LINE using namespace std ; const int MOD = 1e9 + 7 ; int power ( int x , unsigned int y , int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( 1LL * res * x ) % p ; x = ( 1LL * x * x ) % p ; } return res ; } int countEvenWays ( int n ) { return power ( 2 , n / 2 - 1 , MOD ) ; } int main ( ) { int n = 6 ; cout << countEvenWays ( n ) << " STRNEWLINE " ; n = 8 ; cout << countEvenWays ( n ) ; return 0 ; }