답안 #219882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219882 2020-04-06T16:10:13 Z MKopchev Boat (APIO16_boat) C++14
58 / 100
2000 ms 17656 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize ("unroll-loops")

#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];

int 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+1LL*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 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]+1LL*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:90:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
boat.cpp:100: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 1084 ms 17368 KB Output is correct
2 Correct 1093 ms 17328 KB Output is correct
3 Correct 1078 ms 17384 KB Output is correct
4 Correct 1095 ms 17368 KB Output is correct
5 Correct 1114 ms 17352 KB Output is correct
6 Correct 1095 ms 17376 KB Output is correct
7 Correct 1078 ms 17528 KB Output is correct
8 Correct 1066 ms 17308 KB Output is correct
9 Correct 1072 ms 17360 KB Output is correct
10 Correct 1084 ms 17400 KB Output is correct
11 Correct 1069 ms 17404 KB Output is correct
12 Correct 1067 ms 17400 KB Output is correct
13 Correct 1072 ms 17376 KB Output is correct
14 Correct 1066 ms 17420 KB Output is correct
15 Correct 1091 ms 17348 KB Output is correct
16 Correct 238 ms 13936 KB Output is correct
17 Correct 212 ms 13944 KB Output is correct
18 Correct 203 ms 13944 KB Output is correct
19 Correct 213 ms 14024 KB Output is correct
20 Correct 205 ms 13944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1084 ms 17368 KB Output is correct
2 Correct 1093 ms 17328 KB Output is correct
3 Correct 1078 ms 17384 KB Output is correct
4 Correct 1095 ms 17368 KB Output is correct
5 Correct 1114 ms 17352 KB Output is correct
6 Correct 1095 ms 17376 KB Output is correct
7 Correct 1078 ms 17528 KB Output is correct
8 Correct 1066 ms 17308 KB Output is correct
9 Correct 1072 ms 17360 KB Output is correct
10 Correct 1084 ms 17400 KB Output is correct
11 Correct 1069 ms 17404 KB Output is correct
12 Correct 1067 ms 17400 KB Output is correct
13 Correct 1072 ms 17376 KB Output is correct
14 Correct 1066 ms 17420 KB Output is correct
15 Correct 1091 ms 17348 KB Output is correct
16 Correct 238 ms 13936 KB Output is correct
17 Correct 212 ms 13944 KB Output is correct
18 Correct 203 ms 13944 KB Output is correct
19 Correct 213 ms 14024 KB Output is correct
20 Correct 205 ms 13944 KB Output is correct
21 Correct 1022 ms 17016 KB Output is correct
22 Correct 988 ms 17104 KB Output is correct
23 Correct 928 ms 17016 KB Output is correct
24 Correct 989 ms 17016 KB Output is correct
25 Correct 991 ms 17016 KB Output is correct
26 Correct 1248 ms 16992 KB Output is correct
27 Correct 1303 ms 16876 KB Output is correct
28 Correct 1343 ms 16952 KB Output is correct
29 Correct 1285 ms 16888 KB Output is correct
30 Correct 1039 ms 17400 KB Output is correct
31 Correct 1047 ms 17412 KB Output is correct
32 Correct 1062 ms 17412 KB Output is correct
33 Correct 1050 ms 17320 KB Output is correct
34 Correct 1038 ms 17400 KB Output is correct
35 Correct 1039 ms 17528 KB Output is correct
36 Correct 1038 ms 17400 KB Output is correct
37 Correct 1062 ms 17400 KB Output is correct
38 Correct 1039 ms 17552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 14080 KB Output is correct
2 Correct 26 ms 13952 KB Output is correct
3 Correct 27 ms 13944 KB Output is correct
4 Correct 28 ms 13952 KB Output is correct
5 Correct 27 ms 13952 KB Output is correct
6 Correct 31 ms 13952 KB Output is correct
7 Correct 31 ms 13952 KB Output is correct
8 Correct 31 ms 13952 KB Output is correct
9 Correct 31 ms 13944 KB Output is correct
10 Correct 31 ms 13952 KB Output is correct
11 Correct 29 ms 13952 KB Output is correct
12 Correct 27 ms 13952 KB Output is correct
13 Correct 27 ms 13952 KB Output is correct
14 Correct 27 ms 13952 KB Output is correct
15 Correct 28 ms 13952 KB Output is correct
16 Correct 20 ms 13568 KB Output is correct
17 Correct 21 ms 13568 KB Output is correct
18 Correct 20 ms 13568 KB Output is correct
19 Correct 20 ms 13568 KB Output is correct
20 Correct 20 ms 13568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1084 ms 17368 KB Output is correct
2 Correct 1093 ms 17328 KB Output is correct
3 Correct 1078 ms 17384 KB Output is correct
4 Correct 1095 ms 17368 KB Output is correct
5 Correct 1114 ms 17352 KB Output is correct
6 Correct 1095 ms 17376 KB Output is correct
7 Correct 1078 ms 17528 KB Output is correct
8 Correct 1066 ms 17308 KB Output is correct
9 Correct 1072 ms 17360 KB Output is correct
10 Correct 1084 ms 17400 KB Output is correct
11 Correct 1069 ms 17404 KB Output is correct
12 Correct 1067 ms 17400 KB Output is correct
13 Correct 1072 ms 17376 KB Output is correct
14 Correct 1066 ms 17420 KB Output is correct
15 Correct 1091 ms 17348 KB Output is correct
16 Correct 238 ms 13936 KB Output is correct
17 Correct 212 ms 13944 KB Output is correct
18 Correct 203 ms 13944 KB Output is correct
19 Correct 213 ms 14024 KB Output is correct
20 Correct 205 ms 13944 KB Output is correct
21 Correct 1022 ms 17016 KB Output is correct
22 Correct 988 ms 17104 KB Output is correct
23 Correct 928 ms 17016 KB Output is correct
24 Correct 989 ms 17016 KB Output is correct
25 Correct 991 ms 17016 KB Output is correct
26 Correct 1248 ms 16992 KB Output is correct
27 Correct 1303 ms 16876 KB Output is correct
28 Correct 1343 ms 16952 KB Output is correct
29 Correct 1285 ms 16888 KB Output is correct
30 Correct 1039 ms 17400 KB Output is correct
31 Correct 1047 ms 17412 KB Output is correct
32 Correct 1062 ms 17412 KB Output is correct
33 Correct 1050 ms 17320 KB Output is correct
34 Correct 1038 ms 17400 KB Output is correct
35 Correct 1039 ms 17528 KB Output is correct
36 Correct 1038 ms 17400 KB Output is correct
37 Correct 1062 ms 17400 KB Output is correct
38 Correct 1039 ms 17552 KB Output is correct
39 Correct 30 ms 14080 KB Output is correct
40 Correct 26 ms 13952 KB Output is correct
41 Correct 27 ms 13944 KB Output is correct
42 Correct 28 ms 13952 KB Output is correct
43 Correct 27 ms 13952 KB Output is correct
44 Correct 31 ms 13952 KB Output is correct
45 Correct 31 ms 13952 KB Output is correct
46 Correct 31 ms 13952 KB Output is correct
47 Correct 31 ms 13944 KB Output is correct
48 Correct 31 ms 13952 KB Output is correct
49 Correct 29 ms 13952 KB Output is correct
50 Correct 27 ms 13952 KB Output is correct
51 Correct 27 ms 13952 KB Output is correct
52 Correct 27 ms 13952 KB Output is correct
53 Correct 28 ms 13952 KB Output is correct
54 Correct 20 ms 13568 KB Output is correct
55 Correct 21 ms 13568 KB Output is correct
56 Correct 20 ms 13568 KB Output is correct
57 Correct 20 ms 13568 KB Output is correct
58 Correct 20 ms 13568 KB Output is correct
59 Correct 1949 ms 17520 KB Output is correct
60 Correct 1875 ms 17488 KB Output is correct
61 Correct 1868 ms 17528 KB Output is correct
62 Correct 1943 ms 17656 KB Output is correct
63 Correct 1898 ms 17656 KB Output is correct
64 Execution timed out 2086 ms 17532 KB Time limit exceeded
65 Halted 0 ms 0 KB -