답안 #219877

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219877 2020-04-06T16:02:53 Z MKopchev Boat (APIO16_boat) C++14
58 / 100
2000 ms 21444 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e3+42,mod=1e9+7;

int my_pow(long long a,int b)
{
    long long ret=1;
    while(b)
    {
        if(b%2)ret=ret*a%mod;
        a=a*a%mod;
        b=b/2;
    }
    return ret;
}

int n;
pair<int,int> inp[nmax];

set< pair<int,int> > seen;

pair<int,int> groups[nmax];
int pointer=0;

int inv[nmax];

int dp[nmax][nmax][2];

long long coeff[nmax][nmax];

int rec(int which_number,int last_used_group,bool can_place_zero)
{
    if(which_number>n)return 1;//properly chosen
    if(last_used_group==pointer)return can_place_zero;//skipped too much

    if(dp[which_number][last_used_group][can_place_zero]!=-1)return dp[which_number][last_used_group][can_place_zero];

    long long ret=0;

    //place zero
    if(can_place_zero)ret=(ret+rec(which_number+1,last_used_group,1))%mod;

    //place non-zero for other group
    ret=(ret+rec(which_number,last_used_group+1,0))%mod;//ignore this group

    //place non-zero
    if(inp[which_number].first<=groups[last_used_group+1].first&&groups[last_used_group+1].second<=inp[which_number].second)
    {
        int nums=0;
        for(int use=which_number;use<=n;use++)
        {
            if(inp[use].first<=groups[last_used_group+1].first&&groups[last_used_group+1].second<=inp[use].second)
            {
                nums++;
                ret=(ret+coeff[last_used_group+1][nums]*rec(use+1,last_used_group+1,1))%mod;
            }
            //cout<<"which_number= "<<which_number<<" last_used_group= "<<last_used_group<<" use= "<<use<<" mult= "<<coeff[last_used_group+1][nums]<<" can_place_zero= "<<can_place_zero<<" finish= "<<rec(use+1,last_used_group+1,1)<<endl;
        }
    }
    dp[which_number][last_used_group][can_place_zero]=ret;

    //cout<<which_number<<" "<<last_used_group<<" "<<can_place_zero<<" -> "<<ret<<endl;

    return ret;
}

int C[nmax][nmax];

int ask_C(int n_,int k_)
{
    if(0>k_||k_>n_)return 0;
    return C[n_][k_];
}

int slow(int n_,int k_)
{
    if(n_<k_)return 0;
    long long ret=1;
    for(int i=1;i<=k_;i++)
        ret=ret*(n_-i+1)%mod*inv[i]%mod;
    return ret;
}
int main()
{
    //freopen("output6.c","r",stdin);

    memset(dp,-1,sizeof(dp));

    for(int i=0;i<nmax;i++)
        for(int j=0;j<=i;j++)
            if(j==0||j==i)C[i][j]=1;
            else C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;

    scanf("%i",&n);

    for(int i=1;i<nmax;i++)
    {
        inv[i]=my_pow(i,mod-2);
    }

    int prv=1e9;
    for(int i=1;i<=n;i++)
    {
        scanf("%i%i",&inp[i].first,&inp[i].second);

        seen.insert({inp[i].first,-1});
        seen.insert({inp[i].second,0});

        prv=min(prv,inp[i].first);
        prv=min(prv,inp[i].second);
    }

    for(auto k:seen)
    {
        int other=k.first+k.second;
        if(prv<=other)
        {
            pointer++;
            groups[pointer]={prv,other};
            prv=other+1;
        }
    }
    /*
    for(int i=1;i<=n;i++)
        for(int j=1;j<=pointer;j++)
        {
            int le=max(inp[i].first,groups[j].first);
            int ri=min(inp[i].second,groups[j].second);

            if(le>ri)continue;

            assert(le==groups[j].first&&ri==groups[j].second);
        }

    cout<<"---"<<endl;
    for(int i=1;i<=pointer;i++)cout<<groups[i].first<<" "<<groups[i].second<<endl;
    */
    for(int which=1;which<=pointer;which++)
        for(int nums=1;nums<=n;nums++)
        {
            int total=groups[which].second-groups[which].first+1;

            if(nums==1)coeff[which][1]=total;
            else
            {
                long long coeff_help=total;
                for(int zero=nums-2;zero>=0;zero--)
                {
                    if(total<nums-zero)coeff_help=0;
                    else if(total==nums-zero)coeff_help=1;
                    else coeff_help=coeff_help*(total-(nums-zero)+1)%mod*inv[nums-zero]%mod;

                    //cout<<"zero= "<<zero<<" "<<total-(nums-zero)+1<<" , "<<nums-zero<<" actual= "<<inv[nums-zero]<<endl;
                    //cout<<"zero= "<<zero<<" "<<coeff_help<<" "<<ask_C(total,nums-zero)<<endl;

                    //if(coeff_help!=ask_C(total,nums-zero))system("pause");
                    coeff[which][nums]=(coeff[which][nums]+ask_C(nums-2,zero)*coeff_help)%mod;
                }
            }

            //cout<<which<<" "<<nums<<" -> "<<coeff[which][nums]<<endl;
            //cout<<"---"<<endl;
        }
    int out=rec(1,0,1);

    out=(out-1+mod)%mod;

    printf("%i\n",out);
    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
boat.cpp:104:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i",&inp[i].first,&inp[i].second);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1067 ms 21184 KB Output is correct
2 Correct 1073 ms 21240 KB Output is correct
3 Correct 1075 ms 21372 KB Output is correct
4 Correct 1097 ms 21268 KB Output is correct
5 Correct 1051 ms 21220 KB Output is correct
6 Correct 1056 ms 21372 KB Output is correct
7 Correct 1074 ms 21384 KB Output is correct
8 Correct 1058 ms 21240 KB Output is correct
9 Correct 1079 ms 21368 KB Output is correct
10 Correct 1048 ms 21240 KB Output is correct
11 Correct 1051 ms 21240 KB Output is correct
12 Correct 1055 ms 21368 KB Output is correct
13 Correct 1048 ms 21444 KB Output is correct
14 Correct 1050 ms 21336 KB Output is correct
15 Correct 1056 ms 21368 KB Output is correct
16 Correct 221 ms 14584 KB Output is correct
17 Correct 209 ms 14712 KB Output is correct
18 Correct 199 ms 14584 KB Output is correct
19 Correct 239 ms 14712 KB Output is correct
20 Correct 205 ms 14840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1067 ms 21184 KB Output is correct
2 Correct 1073 ms 21240 KB Output is correct
3 Correct 1075 ms 21372 KB Output is correct
4 Correct 1097 ms 21268 KB Output is correct
5 Correct 1051 ms 21220 KB Output is correct
6 Correct 1056 ms 21372 KB Output is correct
7 Correct 1074 ms 21384 KB Output is correct
8 Correct 1058 ms 21240 KB Output is correct
9 Correct 1079 ms 21368 KB Output is correct
10 Correct 1048 ms 21240 KB Output is correct
11 Correct 1051 ms 21240 KB Output is correct
12 Correct 1055 ms 21368 KB Output is correct
13 Correct 1048 ms 21444 KB Output is correct
14 Correct 1050 ms 21336 KB Output is correct
15 Correct 1056 ms 21368 KB Output is correct
16 Correct 221 ms 14584 KB Output is correct
17 Correct 209 ms 14712 KB Output is correct
18 Correct 199 ms 14584 KB Output is correct
19 Correct 239 ms 14712 KB Output is correct
20 Correct 205 ms 14840 KB Output is correct
21 Correct 1028 ms 20600 KB Output is correct
22 Correct 972 ms 20856 KB Output is correct
23 Correct 905 ms 20476 KB Output is correct
24 Correct 963 ms 20600 KB Output is correct
25 Correct 973 ms 20612 KB Output is correct
26 Correct 1290 ms 20216 KB Output is correct
27 Correct 1296 ms 20472 KB Output is correct
28 Correct 1241 ms 20168 KB Output is correct
29 Correct 1214 ms 20392 KB Output is correct
30 Correct 1029 ms 21244 KB Output is correct
31 Correct 1028 ms 21368 KB Output is correct
32 Correct 1038 ms 21368 KB Output is correct
33 Correct 1036 ms 21268 KB Output is correct
34 Correct 1029 ms 21240 KB Output is correct
35 Correct 1041 ms 21240 KB Output is correct
36 Correct 1075 ms 21368 KB Output is correct
37 Correct 1061 ms 21240 KB Output is correct
38 Correct 1035 ms 21240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 14080 KB Output is correct
2 Correct 26 ms 14080 KB Output is correct
3 Correct 28 ms 14072 KB Output is correct
4 Correct 27 ms 14080 KB Output is correct
5 Correct 28 ms 14080 KB Output is correct
6 Correct 31 ms 14080 KB Output is correct
7 Correct 31 ms 14080 KB Output is correct
8 Correct 31 ms 14080 KB Output is correct
9 Correct 31 ms 14080 KB Output is correct
10 Correct 32 ms 14080 KB Output is correct
11 Correct 29 ms 14080 KB Output is correct
12 Correct 28 ms 14080 KB Output is correct
13 Correct 28 ms 14080 KB Output is correct
14 Correct 28 ms 14080 KB Output is correct
15 Correct 28 ms 14080 KB Output is correct
16 Correct 22 ms 13696 KB Output is correct
17 Correct 22 ms 13696 KB Output is correct
18 Correct 20 ms 13744 KB Output is correct
19 Correct 20 ms 13668 KB Output is correct
20 Correct 21 ms 13696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1067 ms 21184 KB Output is correct
2 Correct 1073 ms 21240 KB Output is correct
3 Correct 1075 ms 21372 KB Output is correct
4 Correct 1097 ms 21268 KB Output is correct
5 Correct 1051 ms 21220 KB Output is correct
6 Correct 1056 ms 21372 KB Output is correct
7 Correct 1074 ms 21384 KB Output is correct
8 Correct 1058 ms 21240 KB Output is correct
9 Correct 1079 ms 21368 KB Output is correct
10 Correct 1048 ms 21240 KB Output is correct
11 Correct 1051 ms 21240 KB Output is correct
12 Correct 1055 ms 21368 KB Output is correct
13 Correct 1048 ms 21444 KB Output is correct
14 Correct 1050 ms 21336 KB Output is correct
15 Correct 1056 ms 21368 KB Output is correct
16 Correct 221 ms 14584 KB Output is correct
17 Correct 209 ms 14712 KB Output is correct
18 Correct 199 ms 14584 KB Output is correct
19 Correct 239 ms 14712 KB Output is correct
20 Correct 205 ms 14840 KB Output is correct
21 Correct 1028 ms 20600 KB Output is correct
22 Correct 972 ms 20856 KB Output is correct
23 Correct 905 ms 20476 KB Output is correct
24 Correct 963 ms 20600 KB Output is correct
25 Correct 973 ms 20612 KB Output is correct
26 Correct 1290 ms 20216 KB Output is correct
27 Correct 1296 ms 20472 KB Output is correct
28 Correct 1241 ms 20168 KB Output is correct
29 Correct 1214 ms 20392 KB Output is correct
30 Correct 1029 ms 21244 KB Output is correct
31 Correct 1028 ms 21368 KB Output is correct
32 Correct 1038 ms 21368 KB Output is correct
33 Correct 1036 ms 21268 KB Output is correct
34 Correct 1029 ms 21240 KB Output is correct
35 Correct 1041 ms 21240 KB Output is correct
36 Correct 1075 ms 21368 KB Output is correct
37 Correct 1061 ms 21240 KB Output is correct
38 Correct 1035 ms 21240 KB Output is correct
39 Correct 27 ms 14080 KB Output is correct
40 Correct 26 ms 14080 KB Output is correct
41 Correct 28 ms 14072 KB Output is correct
42 Correct 27 ms 14080 KB Output is correct
43 Correct 28 ms 14080 KB Output is correct
44 Correct 31 ms 14080 KB Output is correct
45 Correct 31 ms 14080 KB Output is correct
46 Correct 31 ms 14080 KB Output is correct
47 Correct 31 ms 14080 KB Output is correct
48 Correct 32 ms 14080 KB Output is correct
49 Correct 29 ms 14080 KB Output is correct
50 Correct 28 ms 14080 KB Output is correct
51 Correct 28 ms 14080 KB Output is correct
52 Correct 28 ms 14080 KB Output is correct
53 Correct 28 ms 14080 KB Output is correct
54 Correct 22 ms 13696 KB Output is correct
55 Correct 22 ms 13696 KB Output is correct
56 Correct 20 ms 13744 KB Output is correct
57 Correct 20 ms 13668 KB Output is correct
58 Correct 21 ms 13696 KB Output is correct
59 Correct 1999 ms 21292 KB Output is correct
60 Correct 1952 ms 21236 KB Output is correct
61 Correct 1905 ms 21356 KB Output is correct
62 Correct 1956 ms 21312 KB Output is correct
63 Correct 1920 ms 21396 KB Output is correct
64 Execution timed out 2100 ms 21240 KB Time limit exceeded
65 Halted 0 ms 0 KB -