Algorithm
Problem link- https://www.spoj.com/problems/GOODB/
GOODB - Good Predictions
Having arrived at the ACM-ICPC contest site in a fun-filled mood, The Team continues their important pre-contest preparations. Specifically, every world-class team knows the importance of making predictions about their upcoming submissions.
The Team knows that they'll get plenty of AC (Accepted) submissions, and they find those quite boring by now. As such, they'll focus on their incorrect ones. From their vast experience, The Team knows that they'll only get exactly N (1≤N≤300) submissions wrong throughout the upcoming contest - in fact, they predict that, of those, exactly W (0≤W≤100) will get WA (Wrong Answer), T (0≤T≤100) will get TLE (Time Limit Exceeded), and the remaining R (0≤R≤100) will get RE (Runtime Error). Note that W+T+R=N.
Assuming that their predictions will certainly be correct, the members of The Team are wondering in how many ways that might occur. In other words, how many different ordered combinations of N incorrect results (each being WA, TLE, or RE) exist which satisfy their predictions? Since The Team doesn't make many mistakes, surely you can calculate this value, right? However, since it can get quite large for you, compute it modulo (109+7).
Input
4 integers, N, W, T, and R
Output
1 integer, the number of valid ordered combinations of submission results, modulo (109+7).
Example
Input: 3 2 1 0 Output: 3
Explanation of Sample:
Out of 3 submissions, two are WA, while the third is TLE. The following 3 ordered combinations are then possible:
WA, WA, TLE
WA, TLE, WA
or
TLE, WA, WA
The answer is then 3 modulo (109+7) = 3.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007LL
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI 3.14159265358979323846
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
const int MAXN = 1e5+5;
int main(){
io;
ll fact[305];
fact[0] = 1;
for(int i = 1;i <= 300; i++){
fact[i] = (i*fact[i-1]) % MOD;
}
int n, w, t, r;
cin >> n >> w >> t >> r;
ll num = fact[w+t+r];
ll den = (fact[w]*fact[t]) % MOD;
den *= fact[r];
den %= MOD;
ll ans = num*invFermat(den, MOD);
ans %= MOD;
cout << ans << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
Demonstration
SPOJ Solution-Good Predictions-Solution in C, C++, Java, Python