Submission #76784

# Submission time Handle Problem Language Result Execution time Memory
76784 2018-09-17T19:51:27 Z vex Boat (APIO16_boat) C++14
58 / 100
2000 ms 12748 KB
#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define maxn 505
#define mod (long long)(1e9 + 7)
using namespace std;

long long ste(long long x,long long y)
{
    if(y==0)return 1LL;
    if(y%2==1)return (x*ste(x,y-1))%mod;

    long long ss=ste(x,y/2);
    return (ss*ss)%mod;
}
long long fact[2*maxn];
long long invf[2*maxn];
int d[2*maxn];
long long ff[2*maxn][maxn];
long long dff[2*maxn][maxn];
int n;
void precalc()
{
    fact[0]=1;
    for(int i=1;i<=1000;i++)
    {
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
    }
    for(int i=0;i<=1000;i++)
        invf[i]=ste(fact[i],mod-2);
        
    for(int i=1;i<=2*n-1;i++)
    {
    	ff[i][1]=d[i+1]-d[i];
    	dff[i][1]=d[i+1]-d[i]-1;
    	for(int j=1;j<500 && d[i+1]-d[i]-j>0;j++)
    	{
    		ff[i][j+1]=ff[i][j]*(d[i+1]-d[i]-j);
    		ff[i][j+1]%=mod;
    		
    		dff[i][j+1]=dff[i][j]*(d[i+1]-d[i]-j-1);
    		dff[i][j+1]%=mod;
    	}
    }
}
long long bc(int x,int y)
{
    if(x<y)return 0;
    if(x<=1000)return (((fact[x]*invf[y])%mod) * invf[x-y] )%mod;

	long long res=1;
	for(int i=0;i<y;i++)
	{
		res*=(x-i);
		res%=mod;
	}
	return (res*invf[y])%mod;
}


pii t[maxn];

long long dp[2*maxn][maxn];

vector<int>tre;
long long pre[maxn];
long long f[2*maxn],gf[2*maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>t[i].f>>t[i].s;
        d[2*i-1]=t[i].f;
        d[2*i]=t[i].s;
    }

    sort(d+1,d+1+2*n);
    precalc();

    for(int i=1;i<=n;i++)dp[0][i]=0LL;
    for(int i=1;i<=n;i++)if(t[i].f==d[1])dp[0][i]=1LL;
    dp[0][0]=1LL;
    //for(int i=1;i<=n;i++)cout<<dp[0][i]<<" "<<t[i].f<<" "<<d[0]<<endl;

    for(int i=1;i<=2*n-1;i++)
    {
        for(int j=0;j<=n;j++)dp[i][j]=dp[i-1][j];
        if(d[i]==d[i+1])continue;

        tre.clear();
        for(int j=0;j<=n;j++)
        {
            f[j]=0;
            pre[j]=0;
            gf[j]=0;
        }

        for(int j=1;j<=n;j++)
        {
            if(t[j].f==d[i+1]);
            else if(t[j].f<=d[i] && d[i+1]<=t[j].s);
            else continue;


            for(int k=0;k<j;k++)
            {
                pre[j]+=dp[i-1][k];
                pre[j]%=mod;
            }

            if(t[j].f==d[i+1])
            {
                int sz=tre.size();
                int dd=min(d[i+1]-d[i]-1,sz);
                for(int k=1;k<=dd;k++)
                {
                    long long sss=0;
                    for(int kk=sz-k;kk>=0;kk--)
                    {
                        sss+=pre[tre[kk]]*bc(sz-kk-1,k-1);
                        sss%=mod;
                    }
                    dp[i][j]+=(((sss*invf[k])%mod)*dff[i][k])%mod;//bc(d[i+1]-d[i]-1,k);
                    dp[i][j]%=mod;
                }
                
                dp[i][j]+=pre[j];
                dp[i][j]%=mod;
            }
            else if(t[j].f<=d[i] && d[i+1]<=t[j].s)
            {
                int sz=tre.size();
                int dd=min(d[i+1]-d[i],sz+1);

                for(int k=2;k<=dd;k++)
                {
                    long long sss=0;
                    for(int kk=sz+1-k;kk>=0;kk--)
                    {
                        sss+=pre[tre[kk]]*bc(sz-1-kk,k-2);
                        sss%=mod;
                    }
                    dp[i][j]+=(((sss*invf[k])%mod)*ff[i][k])%mod;//bc(d[i+1]-d[i],k);
                    dp[i][j]%=mod;
                }
		
	    dp[i][j]+=pre[j]*(d[i+1]-d[i]);
	    dp[i][j]%=mod;
                tre.push_back(j);
            }
            /*if(t[j].f==d[i+1])
            {
                int sz=tre.size();
                int dd=min(d[i+1]-d[i]-1,sz);
                for(int k=1;k<=dd;k++)
                {
                    dp[i][j]+=bc(d[i+1]-d[i]-1,k)*f[k];
                    dp[i][j]%=mod;
                }

                dp[i][j]+=pre[j];
            }

            else if(t[j].f<=d[i] && d[i+1]<=t[j].s)
            {

                int sz=tre.size();
                int dd=min(d[i+1]-d[i],sz+1);

                dp[i][j]+=(d[i+1]-d[i])*pre[j];
                dp[i][j]%=mod;

                for(int k=2;k<=dd;k++)
                {
                    dp[i][j]+=bc(d[i+1]-d[i],k)*gf[k];
                    dp[i][j]%=mod;
                }

                for(int k=dd;k>2;k--)
                {
                    gf[k]+=gf[k-1];
                    gf[k]%=mod;
                }
                gf[2]+=pre[j];
                gf[2]%=mod;

                ///!!!!!
                ///BAGA OVDE

                tre.push_back(j);
                sz++;
                if(sz+1<=d[i+1]-d[i])
                {
                    gf[sz+2]=pre[tre[0]];
                }


                for(int k=dd;k>1;k--)
                {
                    f[k]+=f[k-1];
                    f[k]%=mod;
                }
                f[1]+=pre[j];
                f[1]%=mod;
                if(sz<=d[i+1]-d[i])
                {
                    f[sz]=pre[tre[0]];
                }
            }*/
        }


        //for(int j=1;j<=n;j++)cout<<pre[j]<<" ";cout<<endl;
    }


    //for(int i=1;i<=n;i++)cout<<i<<"   "<<t[i].f<<" "<<t[i].s<<endl;
    //for(int i=1;i<=2*n;i++)cout<<d[i]<<" ";cout<<endl;
    /*for(int i=0;i<=2*n-1;i++)
    {
        cout<<i<<"     ";
        for(int j=1;j<=n;j++)cout<<dp[i][j]<<" ";
        cout<<endl;
    }*/


    long long sol=0;
    for(int i=1;i<=n;i++)
    {
        sol+=dp[2*n-1][i];
        sol%=mod;
    }
    cout<<sol<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 12284 KB Output is correct
2 Correct 16 ms 12284 KB Output is correct
3 Correct 16 ms 12344 KB Output is correct
4 Correct 16 ms 12464 KB Output is correct
5 Correct 16 ms 12548 KB Output is correct
6 Correct 15 ms 12548 KB Output is correct
7 Correct 16 ms 12548 KB Output is correct
8 Correct 16 ms 12548 KB Output is correct
9 Correct 21 ms 12548 KB Output is correct
10 Correct 15 ms 12548 KB Output is correct
11 Correct 19 ms 12548 KB Output is correct
12 Correct 16 ms 12612 KB Output is correct
13 Correct 15 ms 12612 KB Output is correct
14 Correct 15 ms 12640 KB Output is correct
15 Correct 15 ms 12640 KB Output is correct
16 Correct 12 ms 12640 KB Output is correct
17 Correct 12 ms 12640 KB Output is correct
18 Correct 12 ms 12640 KB Output is correct
19 Correct 13 ms 12640 KB Output is correct
20 Correct 12 ms 12640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 12284 KB Output is correct
2 Correct 16 ms 12284 KB Output is correct
3 Correct 16 ms 12344 KB Output is correct
4 Correct 16 ms 12464 KB Output is correct
5 Correct 16 ms 12548 KB Output is correct
6 Correct 15 ms 12548 KB Output is correct
7 Correct 16 ms 12548 KB Output is correct
8 Correct 16 ms 12548 KB Output is correct
9 Correct 21 ms 12548 KB Output is correct
10 Correct 15 ms 12548 KB Output is correct
11 Correct 19 ms 12548 KB Output is correct
12 Correct 16 ms 12612 KB Output is correct
13 Correct 15 ms 12612 KB Output is correct
14 Correct 15 ms 12640 KB Output is correct
15 Correct 15 ms 12640 KB Output is correct
16 Correct 12 ms 12640 KB Output is correct
17 Correct 12 ms 12640 KB Output is correct
18 Correct 12 ms 12640 KB Output is correct
19 Correct 13 ms 12640 KB Output is correct
20 Correct 12 ms 12640 KB Output is correct
21 Correct 900 ms 12748 KB Output is correct
22 Correct 881 ms 12748 KB Output is correct
23 Correct 776 ms 12748 KB Output is correct
24 Correct 892 ms 12748 KB Output is correct
25 Correct 878 ms 12748 KB Output is correct
26 Correct 1524 ms 12748 KB Output is correct
27 Correct 1448 ms 12748 KB Output is correct
28 Correct 1435 ms 12748 KB Output is correct
29 Correct 1481 ms 12748 KB Output is correct
30 Correct 19 ms 12748 KB Output is correct
31 Correct 19 ms 12748 KB Output is correct
32 Correct 18 ms 12748 KB Output is correct
33 Correct 19 ms 12748 KB Output is correct
34 Correct 19 ms 12748 KB Output is correct
35 Correct 18 ms 12748 KB Output is correct
36 Correct 17 ms 12748 KB Output is correct
37 Correct 17 ms 12748 KB Output is correct
38 Correct 17 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 12748 KB Output is correct
2 Correct 18 ms 12748 KB Output is correct
3 Correct 24 ms 12748 KB Output is correct
4 Correct 25 ms 12748 KB Output is correct
5 Correct 30 ms 12748 KB Output is correct
6 Correct 86 ms 12748 KB Output is correct
7 Correct 88 ms 12748 KB Output is correct
8 Correct 88 ms 12748 KB Output is correct
9 Correct 84 ms 12748 KB Output is correct
10 Correct 86 ms 12748 KB Output is correct
11 Correct 30 ms 12748 KB Output is correct
12 Correct 19 ms 12748 KB Output is correct
13 Correct 25 ms 12748 KB Output is correct
14 Correct 25 ms 12748 KB Output is correct
15 Correct 30 ms 12748 KB Output is correct
16 Correct 15 ms 12748 KB Output is correct
17 Correct 12 ms 12748 KB Output is correct
18 Correct 14 ms 12748 KB Output is correct
19 Correct 12 ms 12748 KB Output is correct
20 Correct 19 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 12284 KB Output is correct
2 Correct 16 ms 12284 KB Output is correct
3 Correct 16 ms 12344 KB Output is correct
4 Correct 16 ms 12464 KB Output is correct
5 Correct 16 ms 12548 KB Output is correct
6 Correct 15 ms 12548 KB Output is correct
7 Correct 16 ms 12548 KB Output is correct
8 Correct 16 ms 12548 KB Output is correct
9 Correct 21 ms 12548 KB Output is correct
10 Correct 15 ms 12548 KB Output is correct
11 Correct 19 ms 12548 KB Output is correct
12 Correct 16 ms 12612 KB Output is correct
13 Correct 15 ms 12612 KB Output is correct
14 Correct 15 ms 12640 KB Output is correct
15 Correct 15 ms 12640 KB Output is correct
16 Correct 12 ms 12640 KB Output is correct
17 Correct 12 ms 12640 KB Output is correct
18 Correct 12 ms 12640 KB Output is correct
19 Correct 13 ms 12640 KB Output is correct
20 Correct 12 ms 12640 KB Output is correct
21 Correct 900 ms 12748 KB Output is correct
22 Correct 881 ms 12748 KB Output is correct
23 Correct 776 ms 12748 KB Output is correct
24 Correct 892 ms 12748 KB Output is correct
25 Correct 878 ms 12748 KB Output is correct
26 Correct 1524 ms 12748 KB Output is correct
27 Correct 1448 ms 12748 KB Output is correct
28 Correct 1435 ms 12748 KB Output is correct
29 Correct 1481 ms 12748 KB Output is correct
30 Correct 19 ms 12748 KB Output is correct
31 Correct 19 ms 12748 KB Output is correct
32 Correct 18 ms 12748 KB Output is correct
33 Correct 19 ms 12748 KB Output is correct
34 Correct 19 ms 12748 KB Output is correct
35 Correct 18 ms 12748 KB Output is correct
36 Correct 17 ms 12748 KB Output is correct
37 Correct 17 ms 12748 KB Output is correct
38 Correct 17 ms 12748 KB Output is correct
39 Correct 27 ms 12748 KB Output is correct
40 Correct 18 ms 12748 KB Output is correct
41 Correct 24 ms 12748 KB Output is correct
42 Correct 25 ms 12748 KB Output is correct
43 Correct 30 ms 12748 KB Output is correct
44 Correct 86 ms 12748 KB Output is correct
45 Correct 88 ms 12748 KB Output is correct
46 Correct 88 ms 12748 KB Output is correct
47 Correct 84 ms 12748 KB Output is correct
48 Correct 86 ms 12748 KB Output is correct
49 Correct 30 ms 12748 KB Output is correct
50 Correct 19 ms 12748 KB Output is correct
51 Correct 25 ms 12748 KB Output is correct
52 Correct 25 ms 12748 KB Output is correct
53 Correct 30 ms 12748 KB Output is correct
54 Correct 15 ms 12748 KB Output is correct
55 Correct 12 ms 12748 KB Output is correct
56 Correct 14 ms 12748 KB Output is correct
57 Correct 12 ms 12748 KB Output is correct
58 Correct 19 ms 12748 KB Output is correct
59 Execution timed out 2062 ms 12748 KB Time limit exceeded
60 Halted 0 ms 0 KB -