#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
typedef long long ll;
const int M = (int)1e9 + 7;
const int FACTN = 300000 + 7;
int add(int a, int b) {
a += b;
if (a >= M) return a - M;
if (a < 0) return a + M;
return a;
}
int mul(int a, int b) {
return a * (ll)b % M;
}
int add(int a, int b, int c) {
return add(add(a, b), c);
}
int mul(int a, int b, int c) {
return mul(mul(a, b), c);
}
int add(int a, int b, int c, int d) {
return add(add(a, b, c), d);
}
int mul(int a, int b, int c, int d) {
return mul(mul(a, b, c), d);
}
int add(int a, int b, int c, int d, int e) {
return add(add(a, b, c, d), e);
}
int mul(int a, int b, int c, int d, int e) {
return mul(mul(a, b, c, d), e);
}
int add(int a, int b, int c, int d, int e, int f) {
return add(add(a, b, c, d, e), f);
}
int mul(int a, int b, int c, int d, int e, int f) {
return mul(mul(a, b, c, d, e), f);
}
int add(int a, int b, int c, int d, int e, int f, int g) {
return add(add(a, b, c, d, e, f), g);
}
int mul(int a, int b, int c, int d, int e, int f, int g) {
return mul(mul(a, b, c, d, e, f), g);
}
int add(int a, int b, int c, int d, int e, int f, int g, int h) {
return add(add(a, b, c, d, e, f, g), h);
}
int mul(int a, int b, int c, int d, int e, int f, int g, int h) {
return mul(mul(a, b, c, d, e, f, g), h);
}
int pw(int a, int b) {
int r = 1;
while (b) {
if (b & 1) r = mul(r, a);
a = mul(a, a);
b /= 2;
}
return r;
}
int dv(int a, int b) {
return mul(a, pw(b, M - 2));
}
void addup(int& a, int b) {
a = add(a, b);
}
void mulup(int& a, int b) {
a = mul(a, b);
}
void dvup(int& a, int b) {
a = dv(a, b);
}
int fact[FACTN], ifact[FACTN];
void computeFACT() {
fact[0] = 1;
for (int i = 1; i < FACTN; i++) fact[i] = mul(fact[i - 1], i);
ifact[FACTN - 1] = dv(1, fact[FACTN - 1]);
for (int i = FACTN - 2; i >= 0; i--) ifact[i] = mul(ifact[i + 1], i + 1);
}
int getCOMB(int n, int k) {
return mul(fact[n], mul(ifact[k], ifact[n - k]));
}
int fastComb(int n, int k)
{
if (k > n)
{
return 0;
}
// n is very big
// k is small
assert(0 <= k && k <= n);
int up = 1;
for (int i = 1; i <= k; i++)
{
mulup(up, n + 1 - i);
}
return mul(up, ifact[k]);
}
signed main()
{
computeFACT();
#ifdef ONPC
FILE* stream;
freopen_s(&stream, "input.txt", "r", stdin);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
int n;
cin >> n;
map<int, int> mp;
vector<pair<int, int>> v(n);
for (auto& it : v)
{
cin >> it.first >> it.second;
mp[it.first] = 0;
mp[it.second + 1] = 0;
}
vector<int> xs;
int y = 0;
for (auto& it : mp)
{
xs.push_back(it.first);
it.second = y++;
}
vector<int> dim;
for (int i = 0; i + 1 < (int)xs.size(); i++)
{
dim.push_back(xs[i + 1] - xs[i]);
}
assert((int)dim.size() == y - 1);
y = (int)dim.size();
for (auto& it : v)
{
assert(mp.count(it.first));
assert(mp.count(it.second + 1));
it.first = mp[it.first];
it.second = mp[it.second + 1] - 1;
}
vector<vector<int>> dp(y, vector<int>(n + 1, 0));
for (auto& seg : v)
{
int l = seg.first, r = seg.second;
auto ndp = dp; // nu fac nimic aici
// incep ceva nou aici
for (int i = l; i <= r; i++)
{
addup(ndp[i][1], +1);
}
for (int i = 0; i < y; i++)
{
int orice = 0;
for (int t = 1; t <= n; t++)
{
addup(orice, mul(dp[i][t], fastComb(dim[i], t)));
}
for (int j = i + 1; j < y; j++)
{
if (l <= j && j <= y)
{
addup(ndp[j][1], orice);
}
}
for (int t = 1; t <= n; t++)
{
addup(ndp[i][t], dp[i][t - 1]);
}
}
dp = ndp;
}
int sol = 0;
for (int i = 0; i < y; i++)
{
for (int t = 0; t <= n; t++)
{
addup(sol, mul(dp[i][t], fastComb(dim[i], t)));
}
}
cout << sol << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2041 ms |
6712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2041 ms |
6712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
523 ms |
2840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2041 ms |
6712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |