답안 #219922

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219922 2020-04-06T16:57:36 Z MKopchev Boat (APIO16_boat) C++14
100 / 100
1932 ms 13072 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];

void rec(int which_number,int last_used_group,bool can_place_zero)
{
    for(int which_number=n+1;which_number>=1;which_number--)
        for(int last_used_group=pointer;last_used_group>=0;last_used_group--)
            for(int can_place_zero=0;can_place_zero<2;can_place_zero++)
            {

    if(which_number>n){dp[which_number][last_used_group][can_place_zero]=1;continue;}//properly chosen
    if(last_used_group==pointer){dp[which_number][last_used_group][can_place_zero]=can_place_zero;continue;}//skipped too much

    long long ret=0;

    //place zero
    if(can_place_zero)ret=(ret+dp[which_number+1][last_used_group][1])%mod;

    //place non-zero for other group
    ret=(ret+dp[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]*dp[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;

}

int C[nmax][nmax];

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

int main()
{
    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;
        }

    rec(1,0,1);

    int out=dp[1][0][1];//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:89:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
boat.cpp:99: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 1078 ms 12768 KB Output is correct
2 Correct 1070 ms 12984 KB Output is correct
3 Correct 1058 ms 12984 KB Output is correct
4 Correct 1060 ms 12792 KB Output is correct
5 Correct 1068 ms 12920 KB Output is correct
6 Correct 1071 ms 13000 KB Output is correct
7 Correct 1069 ms 12920 KB Output is correct
8 Correct 1062 ms 12988 KB Output is correct
9 Correct 1053 ms 13048 KB Output is correct
10 Correct 1055 ms 12928 KB Output is correct
11 Correct 1058 ms 12968 KB Output is correct
12 Correct 1091 ms 12920 KB Output is correct
13 Correct 1090 ms 12836 KB Output is correct
14 Correct 1085 ms 12924 KB Output is correct
15 Correct 1051 ms 12896 KB Output is correct
16 Correct 193 ms 8056 KB Output is correct
17 Correct 207 ms 8184 KB Output is correct
18 Correct 198 ms 8056 KB Output is correct
19 Correct 207 ms 8184 KB Output is correct
20 Correct 203 ms 8060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1078 ms 12768 KB Output is correct
2 Correct 1070 ms 12984 KB Output is correct
3 Correct 1058 ms 12984 KB Output is correct
4 Correct 1060 ms 12792 KB Output is correct
5 Correct 1068 ms 12920 KB Output is correct
6 Correct 1071 ms 13000 KB Output is correct
7 Correct 1069 ms 12920 KB Output is correct
8 Correct 1062 ms 12988 KB Output is correct
9 Correct 1053 ms 13048 KB Output is correct
10 Correct 1055 ms 12928 KB Output is correct
11 Correct 1058 ms 12968 KB Output is correct
12 Correct 1091 ms 12920 KB Output is correct
13 Correct 1090 ms 12836 KB Output is correct
14 Correct 1085 ms 12924 KB Output is correct
15 Correct 1051 ms 12896 KB Output is correct
16 Correct 193 ms 8056 KB Output is correct
17 Correct 207 ms 8184 KB Output is correct
18 Correct 198 ms 8056 KB Output is correct
19 Correct 207 ms 8184 KB Output is correct
20 Correct 203 ms 8060 KB Output is correct
21 Correct 748 ms 12476 KB Output is correct
22 Correct 758 ms 12540 KB Output is correct
23 Correct 737 ms 12604 KB Output is correct
24 Correct 748 ms 12540 KB Output is correct
25 Correct 778 ms 12408 KB Output is correct
26 Correct 960 ms 12408 KB Output is correct
27 Correct 969 ms 12408 KB Output is correct
28 Correct 924 ms 12408 KB Output is correct
29 Correct 930 ms 12280 KB Output is correct
30 Correct 1030 ms 12916 KB Output is correct
31 Correct 1036 ms 13020 KB Output is correct
32 Correct 1038 ms 12920 KB Output is correct
33 Correct 1050 ms 12920 KB Output is correct
34 Correct 1062 ms 12920 KB Output is correct
35 Correct 1042 ms 12792 KB Output is correct
36 Correct 1044 ms 12876 KB Output is correct
37 Correct 1061 ms 12844 KB Output is correct
38 Correct 1052 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 6016 KB Output is correct
2 Correct 21 ms 6004 KB Output is correct
3 Correct 22 ms 6016 KB Output is correct
4 Correct 21 ms 6016 KB Output is correct
5 Correct 22 ms 6016 KB Output is correct
6 Correct 23 ms 6016 KB Output is correct
7 Correct 23 ms 6016 KB Output is correct
8 Correct 23 ms 6016 KB Output is correct
9 Correct 24 ms 6008 KB Output is correct
10 Correct 23 ms 6016 KB Output is correct
11 Correct 22 ms 6016 KB Output is correct
12 Correct 27 ms 6008 KB Output is correct
13 Correct 21 ms 6016 KB Output is correct
14 Correct 22 ms 6008 KB Output is correct
15 Correct 22 ms 6016 KB Output is correct
16 Correct 15 ms 5632 KB Output is correct
17 Correct 15 ms 5504 KB Output is correct
18 Correct 15 ms 5632 KB Output is correct
19 Correct 15 ms 5504 KB Output is correct
20 Correct 15 ms 5504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1078 ms 12768 KB Output is correct
2 Correct 1070 ms 12984 KB Output is correct
3 Correct 1058 ms 12984 KB Output is correct
4 Correct 1060 ms 12792 KB Output is correct
5 Correct 1068 ms 12920 KB Output is correct
6 Correct 1071 ms 13000 KB Output is correct
7 Correct 1069 ms 12920 KB Output is correct
8 Correct 1062 ms 12988 KB Output is correct
9 Correct 1053 ms 13048 KB Output is correct
10 Correct 1055 ms 12928 KB Output is correct
11 Correct 1058 ms 12968 KB Output is correct
12 Correct 1091 ms 12920 KB Output is correct
13 Correct 1090 ms 12836 KB Output is correct
14 Correct 1085 ms 12924 KB Output is correct
15 Correct 1051 ms 12896 KB Output is correct
16 Correct 193 ms 8056 KB Output is correct
17 Correct 207 ms 8184 KB Output is correct
18 Correct 198 ms 8056 KB Output is correct
19 Correct 207 ms 8184 KB Output is correct
20 Correct 203 ms 8060 KB Output is correct
21 Correct 748 ms 12476 KB Output is correct
22 Correct 758 ms 12540 KB Output is correct
23 Correct 737 ms 12604 KB Output is correct
24 Correct 748 ms 12540 KB Output is correct
25 Correct 778 ms 12408 KB Output is correct
26 Correct 960 ms 12408 KB Output is correct
27 Correct 969 ms 12408 KB Output is correct
28 Correct 924 ms 12408 KB Output is correct
29 Correct 930 ms 12280 KB Output is correct
30 Correct 1030 ms 12916 KB Output is correct
31 Correct 1036 ms 13020 KB Output is correct
32 Correct 1038 ms 12920 KB Output is correct
33 Correct 1050 ms 12920 KB Output is correct
34 Correct 1062 ms 12920 KB Output is correct
35 Correct 1042 ms 12792 KB Output is correct
36 Correct 1044 ms 12876 KB Output is correct
37 Correct 1061 ms 12844 KB Output is correct
38 Correct 1052 ms 12920 KB Output is correct
39 Correct 22 ms 6016 KB Output is correct
40 Correct 21 ms 6004 KB Output is correct
41 Correct 22 ms 6016 KB Output is correct
42 Correct 21 ms 6016 KB Output is correct
43 Correct 22 ms 6016 KB Output is correct
44 Correct 23 ms 6016 KB Output is correct
45 Correct 23 ms 6016 KB Output is correct
46 Correct 23 ms 6016 KB Output is correct
47 Correct 24 ms 6008 KB Output is correct
48 Correct 23 ms 6016 KB Output is correct
49 Correct 22 ms 6016 KB Output is correct
50 Correct 27 ms 6008 KB Output is correct
51 Correct 21 ms 6016 KB Output is correct
52 Correct 22 ms 6008 KB Output is correct
53 Correct 22 ms 6016 KB Output is correct
54 Correct 15 ms 5632 KB Output is correct
55 Correct 15 ms 5504 KB Output is correct
56 Correct 15 ms 5632 KB Output is correct
57 Correct 15 ms 5504 KB Output is correct
58 Correct 15 ms 5504 KB Output is correct
59 Correct 1696 ms 12768 KB Output is correct
60 Correct 1666 ms 12840 KB Output is correct
61 Correct 1656 ms 12844 KB Output is correct
62 Correct 1698 ms 12752 KB Output is correct
63 Correct 1713 ms 12748 KB Output is correct
64 Correct 1932 ms 12860 KB Output is correct
65 Correct 1928 ms 12852 KB Output is correct
66 Correct 1927 ms 12808 KB Output is correct
67 Correct 1909 ms 12808 KB Output is correct
68 Correct 1905 ms 12788 KB Output is correct
69 Correct 1670 ms 13072 KB Output is correct
70 Correct 1668 ms 12796 KB Output is correct
71 Correct 1676 ms 12924 KB Output is correct
72 Correct 1711 ms 12868 KB Output is correct
73 Correct 1684 ms 12856 KB Output is correct
74 Correct 243 ms 8056 KB Output is correct
75 Correct 251 ms 8184 KB Output is correct
76 Correct 248 ms 8160 KB Output is correct
77 Correct 250 ms 8132 KB Output is correct
78 Correct 252 ms 8056 KB Output is correct