Submission #522319

# Submission time Handle Problem Language Result Execution time Memory
522319 2022-02-04T15:13:36 Z NicolaAbusaad2014 Boat (APIO16_boat) C++14
100 / 100
1292 ms 10524 KB
/**
 * Prof.Nicola
**/
#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define mp make_pair
#define F first
#define S second
#define REP(i,l,r) for(int i=(l);i<(r);i++)
#define PER(i,l,r) for(int i=(r)-1;i>=(l);i--)
#define bitl __builtin_clz
#define bitr __builtin_ctz
#define bits __builtin_popcount
const long dx[4]={1,0,-1,0},dz[4]={0,1,0,-1};
const long double pi=3.14159265359;
const long long mod=1e9+7;
long long p(long long x){while(x&(x-1)){x=x&(x-1);}return x;}
long long squared(long long x){return (x*x)%mod;}
long long power(long long x,long long p){if(p==0){return 1;}if(p%2==1){return (power(x,p-1)*x)%mod;}return squared(power(x,p/2));}
long long inv(long long x){return power(x,mod-2);}
long long log(long long x,long long z){if(x<z){return 0;}return 1+log(x/z,z);}
template<class T>void re(T&x){cin>>x;}
template<class T1,class T2> void re(pair<T1,T2>&x){re(x.first);re(x.second);}
template<class T>void re(vector<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
template<class T>void re(deque<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
template<class T>void out(T x){cout<<x<<" ";}
template<class T1,class T2>void out(pair<T1,T2>x){out(x.first);out(x.second);cout<<endl;}
template<class T>void out(vector<T>x,long l=0,long r=0){if(!r){r=x.size();}for(long i=l;i<r;i++){out(x[i]);}cout<<endl;}
template<class T>void out(deque<T>x,long l=0,long r=0){if(!r){r=x.size();}for(long i=l;i<r;i++){out(x[i]);}cout<<endl;}
template<class T>void out(set<T>x){while(!x.empty()){out(*x.begin());x.erase(*x.begin());}cout<<endl;}
template<class T1,class T2>void out(map<T1,T2>x){while(!x.empty()){out(*x.begin());x.erase(x.begin()->first);}cout<<endl;}
template<class T>void out(queue<T>x){while(!x.empty()){out(x.front());x.pop();}cout<<endl;}
template<class T>void out(priority_queue<T>x){while(!x.empty()){out(x.top());x.pop();}cout<<endl;}
template<class T>void out(stack<T>x){while(!x.empty()){out(x.top());x.pop();}cout<<endl;}
template<class T>T cross(complex<T>x,complex<T>z){return (conj(x)*z).imag();}
template<class T>T dot(complex<T>x,complex<T>z){return (conj(x)*z).real();}
set<long long>::iterator T;
long long vLE(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>x){return -1;}long long z=p(r-l);while(z){if(l+z<=r&&VT[l+z]<=x){l+=z;}z/=2;}return l;}
long long vL(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>=x){return -1;}long long z=p(r-l);while(z){if(l+z<=r&&VT[l+z]<x){l+=z;}z/=2;}return l;}
long long vGE(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>=x){return l;}l=vL(x,VT,l,r);if(l==r){return -1;}return l+1;}
long long vG(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>x){return l;}l=vLE(x,VT,l,r);if(l==r){return -1;}return l+1;}
long long sLE(long long x,set<long long>&ST){if(ST.count(x)){return x;}if(*ST.begin()>x){return -1;}ST.insert(x);T=ST.find(x);T--;ST.erase(x);return *T;}
long long sL(long long x,set<long long>&ST){if(*ST.begin()>=x){return -1;}if(ST.count(x)){T=ST.find(x);T--;return *T;}ST.insert(x);T=ST.find(x);T--;ST.erase(x);return *T;}
long long sGE(long long x,set<long long>&ST){if(ST.count(x)){return x;}if(*ST.rbegin()<x){return -1;}ST.insert(x);T=ST.find(x);T++;ST.erase(x);return *T;}
long long sG(long long x,set<long long>&ST){if(*ST.rbegin()<=x){return -1;}if(ST.count(x)){T=ST.find(x);T++;return *T;}ST.insert(x);T=ST.find(x);T++;ST.erase(x);return *T;}
vector<long long>fact,inverse,der;
void init_fact(long x)
{
    fact.clear();
    inverse.clear();
    fact.resize(x+1);
    inverse.resize(x+1);
    fact[0]=1;
    for(long long i=1;i<=x;i++){
        fact[i]=(fact[i-1]*i)%mod;
    }
    inverse[x]=inv(fact[x]);
    for(long long i=x-1;i>=0;i--){
        inverse[i]=(inverse[i+1]*(i+1))%mod;
    }
}
vector<vector<int> >dp,c;
vector<vector<bool> >ok;
vector<int>v;
vector<pair<int,int> >range;
int solve(int x,int z)
{
    if(x<0){
        return 1;
    }
    if(z<0){
        return 0;
    }
    if(ok[x][z]){
        return dp[x][z];
    }
    ok[x][z]=true;
    dp[x][z]=solve(x,z-1);
    if(!(range[x].F<=v[z]&&v[z+1]-1<=range[x].S)){
        return dp[x][z];
    }
    long long sum=0,can=1;
    REP(j,0,x){
        if(range[j].F<=v[z]&&v[z+1]-1<=range[j].S){
            can++;
        }
    }
    REP(j,0,x+1){
        sum+=solve(j-1,z-1);
        sum%=mod;
        if(!(range[j].F<=v[z]&&v[z+1]-1<=range[j].S)){
            continue;
        }
        dp[x][z]+=(sum*c[can][z])%mod;
        dp[x][z]%=mod;
        can--;
    }
    return dp[x][z];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long n,ans=0,x,z;
    cin>>n;
    init_fact(2*n);
    vector<vector<long long> >choose(2*n,vector<long long>(n+1));
    dp.resize(n);
    ok.resize(n);
    c.resize(n+1);
    REP(j,0,n){
        dp[j].resize(2*n);
        ok[j].resize(2*n);
        c[j].resize(2*n);
    }
    c[n].resize(2*n);
    range.resize(n);
    re(range);
    REP(j,0,n){
        v.push_back(range[j].F);
        v.push_back(range[j].S+1);
    }
    sort(v.begin(),v.end());
    REP(j,1,v.size()){
        if(v[j]==v[j-1]){
            v.erase(v.begin()+j);
            j--;
        }
    }
    REP(i,0,v.size()-1){
        x=v[i+1]-v[i];
        z=1;
        REP(j,1,n+1){
            z*=x-j+1;
            z%=mod;
            choose[i][j]=(z*inverse[j])%mod;
        }
    }
    vector<vector<long long> >nck(n+1,vector<long long>(n+1));
    REP(i,0,n+1){
        REP(j,0,i+1){
            nck[i][j]=(((fact[i]*inverse[j])%mod)*inverse[i-j])%mod;
        }
    }
    REP(i,2,n+1){
        REP(j,0,v.size()-1){
            REP(k,0,i-1){
                c[i][j]+=(nck[i-2][k]*choose[j][k+2])%mod;
                c[i][j]%=mod;
            }
        }
    }
    REP(j,0,v.size()-1){
        c[1][j]=v[j+1]-v[j];
    }
    REP(j,0,n){
        ans+=solve(j,v.size()-2);
        ans%=mod;
    }
    cout<<ans<<endl;
    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:11:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define REP(i,l,r) for(int i=(l);i<(r);i++)
      |                                   ^
boat.cpp:126:5: note: in expansion of macro 'REP'
  126 |     REP(j,1,v.size()){
      |     ^~~
boat.cpp:11:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define REP(i,l,r) for(int i=(l);i<(r);i++)
      |                                   ^
boat.cpp:132:5: note: in expansion of macro 'REP'
  132 |     REP(i,0,v.size()-1){
      |     ^~~
boat.cpp:11:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define REP(i,l,r) for(int i=(l);i<(r);i++)
      |                                   ^
boat.cpp:148:9: note: in expansion of macro 'REP'
  148 |         REP(j,0,v.size()-1){
      |         ^~~
boat.cpp:11:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define REP(i,l,r) for(int i=(l);i<(r);i++)
      |                                   ^
boat.cpp:155:5: note: in expansion of macro 'REP'
  155 |     REP(j,0,v.size()-1){
      |     ^~~
boat.cpp: In instantiation of 'void re(std::vector<_Tp>&) [with T = std::pair<int, int>]':
boat.cpp:120:13:   required from here
boat.cpp:26:53: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 | template<class T>void re(vector<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
      |                                                    ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 576 ms 10316 KB Output is correct
2 Correct 568 ms 10316 KB Output is correct
3 Correct 577 ms 10404 KB Output is correct
4 Correct 572 ms 10316 KB Output is correct
5 Correct 565 ms 10300 KB Output is correct
6 Correct 569 ms 10316 KB Output is correct
7 Correct 570 ms 10312 KB Output is correct
8 Correct 569 ms 10316 KB Output is correct
9 Correct 587 ms 10436 KB Output is correct
10 Correct 567 ms 10316 KB Output is correct
11 Correct 575 ms 10440 KB Output is correct
12 Correct 577 ms 10316 KB Output is correct
13 Correct 571 ms 10316 KB Output is correct
14 Correct 570 ms 10436 KB Output is correct
15 Correct 569 ms 10316 KB Output is correct
16 Correct 106 ms 10300 KB Output is correct
17 Correct 115 ms 10316 KB Output is correct
18 Correct 107 ms 10316 KB Output is correct
19 Correct 113 ms 10324 KB Output is correct
20 Correct 108 ms 10316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 576 ms 10316 KB Output is correct
2 Correct 568 ms 10316 KB Output is correct
3 Correct 577 ms 10404 KB Output is correct
4 Correct 572 ms 10316 KB Output is correct
5 Correct 565 ms 10300 KB Output is correct
6 Correct 569 ms 10316 KB Output is correct
7 Correct 570 ms 10312 KB Output is correct
8 Correct 569 ms 10316 KB Output is correct
9 Correct 587 ms 10436 KB Output is correct
10 Correct 567 ms 10316 KB Output is correct
11 Correct 575 ms 10440 KB Output is correct
12 Correct 577 ms 10316 KB Output is correct
13 Correct 571 ms 10316 KB Output is correct
14 Correct 570 ms 10436 KB Output is correct
15 Correct 569 ms 10316 KB Output is correct
16 Correct 106 ms 10300 KB Output is correct
17 Correct 115 ms 10316 KB Output is correct
18 Correct 107 ms 10316 KB Output is correct
19 Correct 113 ms 10324 KB Output is correct
20 Correct 108 ms 10316 KB Output is correct
21 Correct 1012 ms 10316 KB Output is correct
22 Correct 981 ms 10316 KB Output is correct
23 Correct 903 ms 10392 KB Output is correct
24 Correct 922 ms 10516 KB Output is correct
25 Correct 937 ms 10436 KB Output is correct
26 Correct 1055 ms 10316 KB Output is correct
27 Correct 1128 ms 10516 KB Output is correct
28 Correct 1045 ms 10452 KB Output is correct
29 Correct 1063 ms 10440 KB Output is correct
30 Correct 574 ms 10324 KB Output is correct
31 Correct 560 ms 10316 KB Output is correct
32 Correct 607 ms 10316 KB Output is correct
33 Correct 574 ms 10316 KB Output is correct
34 Correct 586 ms 10316 KB Output is correct
35 Correct 568 ms 10316 KB Output is correct
36 Correct 573 ms 10396 KB Output is correct
37 Correct 569 ms 10436 KB Output is correct
38 Correct 568 ms 10316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 720 KB Output is correct
2 Correct 12 ms 716 KB Output is correct
3 Correct 9 ms 756 KB Output is correct
4 Correct 9 ms 716 KB Output is correct
5 Correct 9 ms 760 KB Output is correct
6 Correct 11 ms 716 KB Output is correct
7 Correct 11 ms 688 KB Output is correct
8 Correct 10 ms 744 KB Output is correct
9 Correct 11 ms 760 KB Output is correct
10 Correct 11 ms 716 KB Output is correct
11 Correct 8 ms 716 KB Output is correct
12 Correct 8 ms 756 KB Output is correct
13 Correct 8 ms 700 KB Output is correct
14 Correct 8 ms 696 KB Output is correct
15 Correct 9 ms 692 KB Output is correct
16 Correct 6 ms 744 KB Output is correct
17 Correct 5 ms 716 KB Output is correct
18 Correct 6 ms 716 KB Output is correct
19 Correct 6 ms 692 KB Output is correct
20 Correct 6 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 576 ms 10316 KB Output is correct
2 Correct 568 ms 10316 KB Output is correct
3 Correct 577 ms 10404 KB Output is correct
4 Correct 572 ms 10316 KB Output is correct
5 Correct 565 ms 10300 KB Output is correct
6 Correct 569 ms 10316 KB Output is correct
7 Correct 570 ms 10312 KB Output is correct
8 Correct 569 ms 10316 KB Output is correct
9 Correct 587 ms 10436 KB Output is correct
10 Correct 567 ms 10316 KB Output is correct
11 Correct 575 ms 10440 KB Output is correct
12 Correct 577 ms 10316 KB Output is correct
13 Correct 571 ms 10316 KB Output is correct
14 Correct 570 ms 10436 KB Output is correct
15 Correct 569 ms 10316 KB Output is correct
16 Correct 106 ms 10300 KB Output is correct
17 Correct 115 ms 10316 KB Output is correct
18 Correct 107 ms 10316 KB Output is correct
19 Correct 113 ms 10324 KB Output is correct
20 Correct 108 ms 10316 KB Output is correct
21 Correct 1012 ms 10316 KB Output is correct
22 Correct 981 ms 10316 KB Output is correct
23 Correct 903 ms 10392 KB Output is correct
24 Correct 922 ms 10516 KB Output is correct
25 Correct 937 ms 10436 KB Output is correct
26 Correct 1055 ms 10316 KB Output is correct
27 Correct 1128 ms 10516 KB Output is correct
28 Correct 1045 ms 10452 KB Output is correct
29 Correct 1063 ms 10440 KB Output is correct
30 Correct 574 ms 10324 KB Output is correct
31 Correct 560 ms 10316 KB Output is correct
32 Correct 607 ms 10316 KB Output is correct
33 Correct 574 ms 10316 KB Output is correct
34 Correct 586 ms 10316 KB Output is correct
35 Correct 568 ms 10316 KB Output is correct
36 Correct 573 ms 10396 KB Output is correct
37 Correct 569 ms 10436 KB Output is correct
38 Correct 568 ms 10316 KB Output is correct
39 Correct 9 ms 720 KB Output is correct
40 Correct 12 ms 716 KB Output is correct
41 Correct 9 ms 756 KB Output is correct
42 Correct 9 ms 716 KB Output is correct
43 Correct 9 ms 760 KB Output is correct
44 Correct 11 ms 716 KB Output is correct
45 Correct 11 ms 688 KB Output is correct
46 Correct 10 ms 744 KB Output is correct
47 Correct 11 ms 760 KB Output is correct
48 Correct 11 ms 716 KB Output is correct
49 Correct 8 ms 716 KB Output is correct
50 Correct 8 ms 756 KB Output is correct
51 Correct 8 ms 700 KB Output is correct
52 Correct 8 ms 696 KB Output is correct
53 Correct 9 ms 692 KB Output is correct
54 Correct 6 ms 744 KB Output is correct
55 Correct 5 ms 716 KB Output is correct
56 Correct 6 ms 716 KB Output is correct
57 Correct 6 ms 692 KB Output is correct
58 Correct 6 ms 716 KB Output is correct
59 Correct 1054 ms 10448 KB Output is correct
60 Correct 1061 ms 10320 KB Output is correct
61 Correct 1010 ms 10524 KB Output is correct
62 Correct 1031 ms 10316 KB Output is correct
63 Correct 1056 ms 10404 KB Output is correct
64 Correct 1251 ms 10316 KB Output is correct
65 Correct 1257 ms 10440 KB Output is correct
66 Correct 1254 ms 10324 KB Output is correct
67 Correct 1226 ms 10444 KB Output is correct
68 Correct 1292 ms 10444 KB Output is correct
69 Correct 916 ms 10320 KB Output is correct
70 Correct 907 ms 10324 KB Output is correct
71 Correct 906 ms 10404 KB Output is correct
72 Correct 930 ms 10408 KB Output is correct
73 Correct 924 ms 10316 KB Output is correct
74 Correct 211 ms 10320 KB Output is correct
75 Correct 204 ms 10332 KB Output is correct
76 Correct 205 ms 10328 KB Output is correct
77 Correct 211 ms 10324 KB Output is correct
78 Correct 205 ms 10316 KB Output is correct