Panda has become a scientist recently. In his laboratory, there are infinite number of chambers where a chamber number K is connected to a chamber number K-1.
The numbering of chambers start from 0. Initially, X number of particles are present in the chamber number 0. The number of particles present in chamber K is K times the number of particles present in chamber K-1. You are supposed to help Panda in finding out the number of particles in a given chamber number N.
Note: The number of particles in chamber K cannot be calculated until the number of particles in chamber K-1 are calculated.
Example:
Input: n = 1, x = 3
Output: 3
Approach
C++
#include <bits/stdc++.h>using namespace std;#define MOD 1000003long long arr[1000004];void initialize(){arr[0] = 0;arr[1] = 1;for (long long i = 2; i < 1000004; i++)arr[i] = ((arr[i - 1] % MOD) * (i % MOD)) % MOD;}long long chainReaction(long long n, long long x){long long ans;if (n >= MOD)ans = 0;elseans = (arr[n] * x) % MOD;return ans;}int main(){initialize();long long n = 1, x = 3;cout << chainReaction(n, x) << "\n";return 0;}
No comments:
Post a Comment