Submission #1058954

# Submission time Handle Problem Language Result Execution time Memory
1058954 2024-08-14T15:21:02 Z Thanhs Magneti (COCI21_magneti) C++14
20 / 110
1000 ms 624 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
 
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
 
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 4e5 + 5;
const int LM = 1e4 + 5;
const int mod = 1e9 + 7;

struct mi
{
    int v;
    mi() : v(0){}
    mi(int v) : v(v)
    {
        if (v >= mod)
            v -= mod;
    }
    mi operator+(const mi& o)
    {
        return mi(v + o.v);
    }
    mi operator-(const mi& o)
    {
        return mi(v + mod - o.v);
    }
    mi operator*(const mi& o)
    {
        return mi(1ll * v * o.v % mod);
    }
    mi& operator+=(const mi &o)
    {
        v += o.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    mi& operator-=(const mi &o)
    {
        v += mod - o.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    mi& operator*=(const mi& o)
    {
        v = 1ll * v * o.v % mod;
        return *this;
    }
}fact[LM], invfact[LM];

mi C(int n, int k)
{
    if (k < 0 || k > n)
        return mi(0);
    return fact[n] * invfact[k] * invfact[n - k];
}

int n, k, a[NM];
vector<int> p;

mi bp(mi x, int y)
{
    mi res(1);
    while (y)
    {
        if (y & 1)
            res = res * x;
        x = x * x;
        y >>= 1;
    }
    return res;
}

void pre()
{
    fact[0].v = 1;
    for (int i = 1; i < LM; i++)
        fact[i] = fact[i - 1] * mi(i);
    invfact[LM - 1] = bp(fact[LM - 1], mod - 2);
    for (int i = LM - 1; i >= 1; i--)
        invfact[i - 1] = invfact[i] * mi(i);
}

void solve()
{
    if (n > 10)
        return;
    pre();
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        a[i]--;
    }
    for (int i = 1; i <= n; i++)
        p.push_back(i);
    mi ans(0);
    do
    {
        int t = k;
        for (int i = 0; i < n - 1; i++)
            t -= max(a[p[i]], a[p[i + 1]]);
        ans += C(t, n);
    }while(next_permutation(p.begin(), p.end()));
    cout << ans.v;
}

signed main()
{
    fastIO
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    int tc = 1;
    // cin >> tc;
    while (tc--)
        solve();
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1070 ms 604 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 65 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 56 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 25 ms 620 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 25 ms 624 KB Output is correct
8 Correct 27 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1008 ms 604 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1070 ms 604 KB Time limit exceeded
2 Halted 0 ms 0 KB -