Algorithm
Problem link- https://www.spoj.com/problems/JOKER1/
JOKER1 - Knifes Are Fun
"Do you know, why I use a knife? Guns are too quick. You can't savor all the little emotions. You see, in their last moments, people show you who they really are. So in a way, I know your friends better than you ever did. Would you like to know which of them were cowards?"
Joker has many knifes, and he wants to assign a distinct integer to each knife so he can easily identify them. The i-th knife can have an integer between 1 and maxNumber[i], inclusive.
Return the number of ways he can assign numbers to his knifes, modulo 1,000,000,007. If it's impossible to assign distinct integers to the knifes, print 0.
Input
The first line contains the number of test cases T (1 <= T <= 666)
Each test case has 2 lines - 1st line denotes number of knifes N (1 <= N <= 66) Joker has and the 2nd line denotes the numbers {maxNumber[0]....maxNumber[N-1]} Joker has.
1 <= maxNumber[i] <= 3000
Output
Print the number of ways Joker can assign numbers to his knifes, modulo 1,000,000,007. If it's impossible to assign distinct integers to the knifes, print 0. In last line print the string "KILL BATMAN". Don't print any extra spaces.
Example
Input: 3 1 7 2 5 8 3 2 1 2 Output: 7 35 0 KILL BATMAN
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
T = int(input())
for i in range(T):
N = int(input())
data = sorted(list(map(int, input().split(' '))))
result = 1
for i in range(N):
result *= max(data[i] - i, 0)
result %= 1000000007
print(result)
print("KILL BATMAN")
Copy The Code &
Try With Live Editor
Input
1
7
2
5 8
3
2 1 2
Output
0
KILL BATMAN
#2 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;
int t;
cin >> t;
while(t--){
int n;
cin >> n;
ll arr[n];
for(int i= 0;i < n; i++)
cin >> arr[i];
sort(arr, arr+n);
bool possible = true;
ll ans = 1;
for(int i = 0;i < n; i++){
ll mult = (arr[i]-i);
if(mult <= 0) possible = false;
else ans = (ans*mult) % MOD;
}
if(!possible)
cout << 0 << endl;
else
cout << ans << endl;
}
cout << "KILL BATMAN" << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
1
7
2
5 8
3
2 1 2
Output
0
KILL BATMAN
Demonstration
SPOJ Solution-Knifes Are Fun-Solution in C, C++, Java, Python