Submission #63232

# Submission time Handle Problem Language Result Execution time Memory
63232 2018-08-01T06:37:22 Z qkxwsm Boat (APIO16_boat) C++17
100 / 100
1405 ms 11336 KB
/*
PROG: source
LANG: C++11
    _____
  .'     '.
 /  0   0  \
|     ^     |
|  \     /  |
 \  '---'  /
  '._____.'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

struct chash
{
        int operator()(int x) const
        {
                x ^= (x >> 20) ^ (x >> 12);
                return x ^ (x >> 7) ^ (x >> 4);
        }
        int operator()(long long x) const
        {
                return x ^ (x >> 32);
        }
};

template<typename T> using orderedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using hashtable = gp_hash_table<T, U, chash>;

template<class T>
void readi(T &x)
{
	T input = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
        {
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		input = input * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		input = -input;
	}
	x = input;
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int aout[20];
	int ilen = 0;
	while(output)
	{
		aout[ilen] = ((output % 10));
		output /= 10;
		ilen++;
	}
	for (int i = ilen - 1; i >= 0; i--)
	{
		putchar(aout[i] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
template<class T, class U>
T normalize(T x, U mod = 1000000007)
{
	return (((x % mod) + mod) % mod);
}
long long randomizell(long long mod)
{
	return ((1ll << 45) * rand() + (1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;
}
int randomize(int mod)
{
	return ((1ll << 15) * rand() + rand()) % mod;
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define debug(x) cerr << #x << " = " << x << endl;

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-10;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 513

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

int N;
pii arr[MAXN];
vector<int> compress;
ll dp[MAXN][2 * MAXN], pref[MAXN][2 * MAXN];
ll modinv[MAXN], ifact[MAXN];
ll nchoose[MAXN][MAXN], pchoose[MAXN];
ll prec[MAXN];
ll ans;

ll getpow(ll a, ll e)
{
        if (e == 1)
        {
                return a;
        }
        ll was = getpow(a, e / 2);
        if (e & 1)
        {
                return was * was % INF * a % INF;
        }
        return was * was % INF;
}
int getidx(int v)
{
        return UB(compress.begin(), compress.end(), v) - compress.begin() - 1;
}
ll choose(ll n, ll k)
{
        if (k < 0 || k > n) return 0;
        ll res = ifact[k];
        for (int i = 0; i < k; i++)
        {
                res *= (n - i);
                res %= INF;
        }
        return res;
}
void precchoose(ll k)
{
        ll prod = 1;
        pchoose[0] = 1;
        for (int i = 1; i <= N; i++)
        {
                prod *= (k - i + 1);
                prod %= INF;
                pchoose[i] = prod * ifact[i] % INF;
                // cerr << k << " choose " << i << " = " << pchoose[i] << endl;
        }
        return;
}
ll f(ll m, ll k)
{
        //put m guys into k slots
        ll res = 0;
        for (int i = 0; i <= m; i++)
        {
                // res += nchoose[m][i] * pchoose[i];
                res += nchoose[m][i] * pchoose[i];
                res %= INF;
        }
        // cerr << "fit " << m << " into " << k << ' ' << res << endl;
        return res;
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	srand(time(0));
	//	cout << fixed << setprecision(10);
	//	cerr << fixed << setprecision(10);
	if (fopen("input.in", "r"))
	{
		freopen ("input.in", "r", stdin);
		freopen ("output.out", "w", stdout);
	}
        cin >> N;
        ifact[0] = 1; ifact[1] = 1; modinv[1] = 1;
        for (int i = 2; i <= N; i++)
        {
                modinv[i] = getpow(i, INF - 2);
                ifact[i] = ifact[i - 1] * modinv[i];
                ifact[i] %= INF;
        }
        for (int i = 0; i <= N; i++)
        {
                nchoose[i][0] = 1;
                nchoose[i][i] = 1;
                for (int j = 1; j < i; j++)
                {
                        nchoose[i][j] = nchoose[i - 1][j - 1] + nchoose[i - 1][j];
                        nchoose[i][j] %= INF;
                }
        }
        for (int i = 0; i < N; i++)
        {
                cin >> arr[i].fi >> arr[i].se;
                arr[i].se++;
                compress.PB(arr[i].fi);
                compress.PB(arr[i].se);
                // cerr << arr[i].fi << ',' << arr[i].se << endl;
        }
        compress.PB(0);
        sort(compress.begin(), compress.end());
        compress.erase(unique(compress.begin(), compress.end()), compress.end());
        // cerr << "positions:";
        // for (int x : compress)
        // {
        //         cerr << ' ' << x;
        // }
        // cerr << endl;
        dp[0][0] = 1;
        pref[0][0] = 1;
        for (int k = 1; k < compress.size() - 1; k++)
        {
                ll spots = compress[k + 1] - compress[k];
                precchoose(spots);
                for (int i = 0; i <= N; i++)
                {
                        prec[i] = f(i, spots);
                }
                for (int i = 1; i <= N; i++)
                {
                        if (compress[k] < arr[i - 1].fi || compress[k] >= arr[i - 1].se)
                        {
                                continue;
                        }
                        int oks = 0;
                        for (int j = i - 1; j >= 0; j--)
                        {
                                if (arr[j].fi <= compress[k] && compress[k] < arr[j].se)
                                {
                                        oks++;
                                }
                                //you finish at j, but at any position less than k
                                //j...i - 1 are all in, and i - 1 MUST be active!
                                ll net = pref[j][k - 1];
                                // cerr << "person " << j - 1 << " finishes less than " << k << ' ' << net << endl;
                                // ll fits = f(oks, spots) - f(oks - 1, spots);
                                ll fits = (prec[oks] - prec[oks - 1] + INF) % INF;
                                dp[i][k] += net * fits;
                                dp[i][k] %= INF;
                                // cerr << "inside " << compress[k] << " oks " << oks << endl;
                                // cerr << "oks " << oks << " spots " << spots << endl;
                                // cerr << "good ways to fit " << j << " to " << i - 1 << " in " << k << " is " << fits << endl;
                        }
                        // cerr << "guy " << i - 1 << " finish " << k << ' ' << dp[i][k] << endl;
                        // pref[i][k] = pref[i][k - 1] + dp[i][k]; //person i finishes at positoin k
                        // pref[i][k] %= INF;
                }
                for (int i = 0; i <= N; i++)
                {
                        pref[i][k] = pref[i][k - 1] + dp[i][k];
                        pref[i][k] %= INF;
                }
        }
        for (int i = 1; i <= N; i++)
        {
                for (int j = 1; j < compress.size() - 1; j++)
                {
                        ans += dp[i][j];
                        ans %= INF;
                }
        }
        cout << ans << '\n';
	//	cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
	return 0;
}

Compilation message

boat.cpp: In function 'int32_t main()':
boat.cpp:255:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int k = 1; k < compress.size() - 1; k++)
                         ~~^~~~~~~~~~~~~~~~~~~~~
boat.cpp:300:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int j = 1; j < compress.size() - 1; j++)
                                 ~~^~~~~~~~~~~~~~~~~~~~~
boat.cpp:215:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen ("input.in", "r", stdin);
   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:216:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen ("output.out", "w", stdout);
   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 756 ms 8428 KB Output is correct
2 Correct 764 ms 8548 KB Output is correct
3 Correct 754 ms 8548 KB Output is correct
4 Correct 753 ms 8548 KB Output is correct
5 Correct 721 ms 8700 KB Output is correct
6 Correct 751 ms 8708 KB Output is correct
7 Correct 744 ms 8776 KB Output is correct
8 Correct 742 ms 8816 KB Output is correct
9 Correct 722 ms 8816 KB Output is correct
10 Correct 728 ms 8816 KB Output is correct
11 Correct 737 ms 8816 KB Output is correct
12 Correct 766 ms 8816 KB Output is correct
13 Correct 771 ms 8816 KB Output is correct
14 Correct 740 ms 8816 KB Output is correct
15 Correct 735 ms 8816 KB Output is correct
16 Correct 142 ms 8816 KB Output is correct
17 Correct 151 ms 8816 KB Output is correct
18 Correct 140 ms 8816 KB Output is correct
19 Correct 156 ms 8816 KB Output is correct
20 Correct 148 ms 8816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 756 ms 8428 KB Output is correct
2 Correct 764 ms 8548 KB Output is correct
3 Correct 754 ms 8548 KB Output is correct
4 Correct 753 ms 8548 KB Output is correct
5 Correct 721 ms 8700 KB Output is correct
6 Correct 751 ms 8708 KB Output is correct
7 Correct 744 ms 8776 KB Output is correct
8 Correct 742 ms 8816 KB Output is correct
9 Correct 722 ms 8816 KB Output is correct
10 Correct 728 ms 8816 KB Output is correct
11 Correct 737 ms 8816 KB Output is correct
12 Correct 766 ms 8816 KB Output is correct
13 Correct 771 ms 8816 KB Output is correct
14 Correct 740 ms 8816 KB Output is correct
15 Correct 735 ms 8816 KB Output is correct
16 Correct 142 ms 8816 KB Output is correct
17 Correct 151 ms 8816 KB Output is correct
18 Correct 140 ms 8816 KB Output is correct
19 Correct 156 ms 8816 KB Output is correct
20 Correct 148 ms 8816 KB Output is correct
21 Correct 1059 ms 9732 KB Output is correct
22 Correct 961 ms 9964 KB Output is correct
23 Correct 922 ms 9964 KB Output is correct
24 Correct 943 ms 10028 KB Output is correct
25 Correct 1000 ms 10028 KB Output is correct
26 Correct 1175 ms 10664 KB Output is correct
27 Correct 1113 ms 10664 KB Output is correct
28 Correct 1186 ms 10724 KB Output is correct
29 Correct 1123 ms 10724 KB Output is correct
30 Correct 739 ms 10724 KB Output is correct
31 Correct 723 ms 10724 KB Output is correct
32 Correct 729 ms 10724 KB Output is correct
33 Correct 784 ms 10724 KB Output is correct
34 Correct 758 ms 10724 KB Output is correct
35 Correct 783 ms 10724 KB Output is correct
36 Correct 739 ms 10724 KB Output is correct
37 Correct 747 ms 10724 KB Output is correct
38 Correct 763 ms 10724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 10724 KB Output is correct
2 Correct 15 ms 10724 KB Output is correct
3 Correct 14 ms 10724 KB Output is correct
4 Correct 13 ms 10724 KB Output is correct
5 Correct 13 ms 10724 KB Output is correct
6 Correct 14 ms 10724 KB Output is correct
7 Correct 14 ms 10724 KB Output is correct
8 Correct 14 ms 10724 KB Output is correct
9 Correct 14 ms 10724 KB Output is correct
10 Correct 14 ms 10724 KB Output is correct
11 Correct 16 ms 10724 KB Output is correct
12 Correct 12 ms 10724 KB Output is correct
13 Correct 16 ms 10724 KB Output is correct
14 Correct 12 ms 10724 KB Output is correct
15 Correct 13 ms 10724 KB Output is correct
16 Correct 10 ms 10724 KB Output is correct
17 Correct 9 ms 10724 KB Output is correct
18 Correct 10 ms 10724 KB Output is correct
19 Correct 11 ms 10724 KB Output is correct
20 Correct 9 ms 10724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 756 ms 8428 KB Output is correct
2 Correct 764 ms 8548 KB Output is correct
3 Correct 754 ms 8548 KB Output is correct
4 Correct 753 ms 8548 KB Output is correct
5 Correct 721 ms 8700 KB Output is correct
6 Correct 751 ms 8708 KB Output is correct
7 Correct 744 ms 8776 KB Output is correct
8 Correct 742 ms 8816 KB Output is correct
9 Correct 722 ms 8816 KB Output is correct
10 Correct 728 ms 8816 KB Output is correct
11 Correct 737 ms 8816 KB Output is correct
12 Correct 766 ms 8816 KB Output is correct
13 Correct 771 ms 8816 KB Output is correct
14 Correct 740 ms 8816 KB Output is correct
15 Correct 735 ms 8816 KB Output is correct
16 Correct 142 ms 8816 KB Output is correct
17 Correct 151 ms 8816 KB Output is correct
18 Correct 140 ms 8816 KB Output is correct
19 Correct 156 ms 8816 KB Output is correct
20 Correct 148 ms 8816 KB Output is correct
21 Correct 1059 ms 9732 KB Output is correct
22 Correct 961 ms 9964 KB Output is correct
23 Correct 922 ms 9964 KB Output is correct
24 Correct 943 ms 10028 KB Output is correct
25 Correct 1000 ms 10028 KB Output is correct
26 Correct 1175 ms 10664 KB Output is correct
27 Correct 1113 ms 10664 KB Output is correct
28 Correct 1186 ms 10724 KB Output is correct
29 Correct 1123 ms 10724 KB Output is correct
30 Correct 739 ms 10724 KB Output is correct
31 Correct 723 ms 10724 KB Output is correct
32 Correct 729 ms 10724 KB Output is correct
33 Correct 784 ms 10724 KB Output is correct
34 Correct 758 ms 10724 KB Output is correct
35 Correct 783 ms 10724 KB Output is correct
36 Correct 739 ms 10724 KB Output is correct
37 Correct 747 ms 10724 KB Output is correct
38 Correct 763 ms 10724 KB Output is correct
39 Correct 11 ms 10724 KB Output is correct
40 Correct 15 ms 10724 KB Output is correct
41 Correct 14 ms 10724 KB Output is correct
42 Correct 13 ms 10724 KB Output is correct
43 Correct 13 ms 10724 KB Output is correct
44 Correct 14 ms 10724 KB Output is correct
45 Correct 14 ms 10724 KB Output is correct
46 Correct 14 ms 10724 KB Output is correct
47 Correct 14 ms 10724 KB Output is correct
48 Correct 14 ms 10724 KB Output is correct
49 Correct 16 ms 10724 KB Output is correct
50 Correct 12 ms 10724 KB Output is correct
51 Correct 16 ms 10724 KB Output is correct
52 Correct 12 ms 10724 KB Output is correct
53 Correct 13 ms 10724 KB Output is correct
54 Correct 10 ms 10724 KB Output is correct
55 Correct 9 ms 10724 KB Output is correct
56 Correct 10 ms 10724 KB Output is correct
57 Correct 11 ms 10724 KB Output is correct
58 Correct 9 ms 10724 KB Output is correct
59 Correct 1121 ms 10724 KB Output is correct
60 Correct 1115 ms 10724 KB Output is correct
61 Correct 1096 ms 10724 KB Output is correct
62 Correct 1135 ms 10724 KB Output is correct
63 Correct 1210 ms 10724 KB Output is correct
64 Correct 1405 ms 11228 KB Output is correct
65 Correct 1331 ms 11228 KB Output is correct
66 Correct 1278 ms 11228 KB Output is correct
67 Correct 1373 ms 11336 KB Output is correct
68 Correct 1308 ms 11336 KB Output is correct
69 Correct 1093 ms 11336 KB Output is correct
70 Correct 1091 ms 11336 KB Output is correct
71 Correct 1049 ms 11336 KB Output is correct
72 Correct 1068 ms 11336 KB Output is correct
73 Correct 1146 ms 11336 KB Output is correct
74 Correct 205 ms 11336 KB Output is correct
75 Correct 199 ms 11336 KB Output is correct
76 Correct 261 ms 11336 KB Output is correct
77 Correct 201 ms 11336 KB Output is correct
78 Correct 247 ms 11336 KB Output is correct