Submission #605321

#TimeUsernameProblemLanguageResultExecution timeMemory
605321YassineBenYounesA Huge Tower (CEOI10_tower)C++17
10 / 100
1093 ms1856 KiB
/*
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)

tower.cpp: In function 'void init()':
tower.cpp:33:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tower.cpp:35:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
tower.cpp: In function 'll solve(std::vector<std::pair<int, int> >)':
tower.cpp:74:18: warning: iteration 1004 invokes undefined behavior [-Waggressive-loop-optimizations]
   74 |         value[i] = 0;
      |         ~~~~~~~~~^~~
tower.cpp:72:22: note: within this loop
   72 |     for(int i = 1; i <= mx;i++){
      |                    ~~^~~~~
tower.cpp:74:18: warning: 'void* __builtin_memset(void*, int, long unsigned int)' forming offset [4020, 4023] is out of the bounds [0, 4020] of object 'value' with type 'int [1005]' [-Warray-bounds]
   74 |         value[i] = 0;
      |         ~~~~~~~~~^~~
tower.cpp:49:5: note: 'value' declared here
   49 | int value[mx];
      |     ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...