# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
605321 | YassineBenYounes | A Huge Tower (CEOI10_tower) | C++17 | 1093 ms | 1856 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: yassine11
TASK: angry
LANG: C++
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 1005;
const int mod = 1e9+7;
const int inf = 1e9;
int arr[mx];
int d;
vi graph[mx];
int value[mx];
int n;
bool vis[mx];
ll dfs(int node, int c){
if(c == n){
return 1;
}
vis[node] = 1;
ll x = 0;
for(int adj : graph[node]){
if(vis[adj])continue;
x+=dfs(adj, c+1);
}
vis[node] = 0;
return x;
}
ll fact[mx];
ll solve(vii v){
for(int i = 1; i <= mx;i++){
graph[i].clear();
value[i] = 0;
}
n = v.size();
int k = 1;
vii v1;
ll ans = 1;
for(int i = 0; i < n;i++){
int c = 0;
while(i < n-1 && v[i].ff == v[i+1].ff){
i++;
c++;
}
ans *= fact[c+1];
v1.pb({v[i].ff, k});
k++;
ans %= mod;
}
n = v1.size();
for(int i = 0; i < n;i++){
for(int j = i+1;j < n;j++){
if(v1[j].ff - v1[i].ff <= d){
graph[v1[i].ss].pb(v1[j].ss);
}
}
}
for(int i = 0; i < n;i++){
for(int j = 0;j < i;j++){
graph[v1[i].ss].pb(v1[j].ss);
}
}
/*for(int i = 1; i <= 3;i++){
cout << i << ": ";
for(int adj : graph[i]){
cout << adj << " ";
}
cout << endl;
}*/
ll ans1 = 0;
for(int i = 1; i <= n;i++){
memset(vis, 0, sizeof vis);
ans1 += dfs(i, 1);
ans1 %= mod;
}
return (ans1 * ans) % mod;
}
int main(){
//ofstream fout ("angry.out");
//ifstream fin ("angry.in");
speed;
fact[0] = 1;
for(int i = 1; i <= 70;i++){
fact[i] = fact[i-1] * i;
fact[i] %= mod;
}
int n;cin >> n >> d;
for(int i = 0; i < n;i++){
cin >> arr[i];
}
sort(arr, arr+n);
ll ans = 1;
for(int i = 0; i < n;i++){
vii v;
int c = 1;
while(i < n-1 && arr[i+1] - arr[i] <= d){
v.pb({arr[i], c});
c++;
i++;
}
v.pb({arr[i], c});
ll x = solve(v);
//cout << x << endl;
ans *= x;
ans %= mod;
}
cout << ans << endl;
}
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |