Submission #219914

# Submission time Handle Problem Language Result Execution time Memory
219914 2020-04-06T16:48:01 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_];
}
//mod= 1000000007

const long long M=18446743944;//(1<<64)/mod

const unsigned int MX_VAL_INT=4294967295;
const unsigned long long MX_VAL_LONG=18446744073709551615;

const unsigned long long M1=M>>32;
const unsigned long long M2=M&MX_VAL_INT;

int barreet(long long a)
{
    unsigned int a1=a>>32;
    unsigned int a2=a&MX_VAL_INT;

    unsigned long long val_32=M1*a2+M2*a1;
    unsigned long long val_0=M2*a2;

    unsigned long long q=M1;
    q=q*a1;

    q=q+(val_32>>32);
    val_32=val_32&MX_VAL_INT;

    val_32=val_32>>32;

    if(MX_VAL_LONG-val_0<val_32)q++;

    unsigned int r=a-q*mod;
    if(r >= mod) r= r - mod;

    //assert(r==a%mod);

    return r;
}
/*
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
	ull b, m;
	FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
	ull reduce(ull a) {
		ull q = (ull)((L(m) * a) >> 64), r = a - q * b;
		return r >= b ? r - b : r;
	}
};
*/
int main()
{
    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=barreet(1LL*barreet(coeff_help*(total-(nums-zero)+1))*inv[nums-zero]);

                    //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]=barreet(coeff[which][nums]+1LL*ask_C(nums-2,zero)*coeff_help);
                }
            }

            //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:83:38: warning: integer constant is so large that it is unsigned
 const unsigned long long MX_VAL_LONG=18446744073709551615;
                                      ^~~~~~~~~~~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:134:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
boat.cpp:144: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);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1461 ms 17656 KB Output is correct
2 Correct 1458 ms 17528 KB Output is correct
3 Correct 1482 ms 17400 KB Output is correct
4 Correct 1473 ms 17528 KB Output is correct
5 Correct 1453 ms 17528 KB Output is correct
6 Correct 1457 ms 17544 KB Output is correct
7 Correct 1448 ms 17632 KB Output is correct
8 Correct 1451 ms 17584 KB Output is correct
9 Correct 1456 ms 17440 KB Output is correct
10 Correct 1465 ms 17400 KB Output is correct
11 Correct 1447 ms 17528 KB Output is correct
12 Correct 1451 ms 17528 KB Output is correct
13 Correct 1467 ms 17528 KB Output is correct
14 Correct 1454 ms 17400 KB Output is correct
15 Correct 1457 ms 17528 KB Output is correct
16 Correct 264 ms 13816 KB Output is correct
17 Correct 283 ms 13944 KB Output is correct
18 Correct 276 ms 13944 KB Output is correct
19 Correct 294 ms 13944 KB Output is correct
20 Correct 278 ms 14020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1461 ms 17656 KB Output is correct
2 Correct 1458 ms 17528 KB Output is correct
3 Correct 1482 ms 17400 KB Output is correct
4 Correct 1473 ms 17528 KB Output is correct
5 Correct 1453 ms 17528 KB Output is correct
6 Correct 1457 ms 17544 KB Output is correct
7 Correct 1448 ms 17632 KB Output is correct
8 Correct 1451 ms 17584 KB Output is correct
9 Correct 1456 ms 17440 KB Output is correct
10 Correct 1465 ms 17400 KB Output is correct
11 Correct 1447 ms 17528 KB Output is correct
12 Correct 1451 ms 17528 KB Output is correct
13 Correct 1467 ms 17528 KB Output is correct
14 Correct 1454 ms 17400 KB Output is correct
15 Correct 1457 ms 17528 KB Output is correct
16 Correct 264 ms 13816 KB Output is correct
17 Correct 283 ms 13944 KB Output is correct
18 Correct 276 ms 13944 KB Output is correct
19 Correct 294 ms 13944 KB Output is correct
20 Correct 278 ms 14020 KB Output is correct
21 Correct 1065 ms 17144 KB Output is correct
22 Correct 1053 ms 17144 KB Output is correct
23 Correct 988 ms 17124 KB Output is correct
24 Correct 1024 ms 17016 KB Output is correct
25 Correct 1060 ms 17144 KB Output is correct
26 Correct 1320 ms 17016 KB Output is correct
27 Correct 1297 ms 17004 KB Output is correct
28 Correct 1362 ms 17108 KB Output is correct
29 Correct 1330 ms 16916 KB Output is correct
30 Correct 1420 ms 17320 KB Output is correct
31 Correct 1428 ms 17656 KB Output is correct
32 Correct 1417 ms 17568 KB Output is correct
33 Correct 1422 ms 17480 KB Output is correct
34 Correct 1421 ms 17528 KB Output is correct
35 Correct 1419 ms 17436 KB Output is correct
36 Correct 1406 ms 17528 KB Output is correct
37 Correct 1428 ms 17488 KB Output is correct
38 Correct 1414 ms 17400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 13952 KB Output is correct
2 Correct 33 ms 13952 KB Output is correct
3 Correct 35 ms 13952 KB Output is correct
4 Correct 33 ms 13952 KB Output is correct
5 Correct 34 ms 13980 KB Output is correct
6 Correct 36 ms 13952 KB Output is correct
7 Correct 36 ms 13944 KB Output is correct
8 Correct 36 ms 13952 KB Output is correct
9 Correct 38 ms 13944 KB Output is correct
10 Correct 37 ms 13952 KB Output is correct
11 Correct 38 ms 13952 KB Output is correct
12 Correct 32 ms 13952 KB Output is correct
13 Correct 33 ms 13944 KB Output is correct
14 Correct 33 ms 13944 KB Output is correct
15 Correct 34 ms 13944 KB Output is correct
16 Correct 23 ms 13568 KB Output is correct
17 Correct 23 ms 13568 KB Output is correct
18 Correct 23 ms 13568 KB Output is correct
19 Correct 22 ms 13616 KB Output is correct
20 Correct 24 ms 13568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1461 ms 17656 KB Output is correct
2 Correct 1458 ms 17528 KB Output is correct
3 Correct 1482 ms 17400 KB Output is correct
4 Correct 1473 ms 17528 KB Output is correct
5 Correct 1453 ms 17528 KB Output is correct
6 Correct 1457 ms 17544 KB Output is correct
7 Correct 1448 ms 17632 KB Output is correct
8 Correct 1451 ms 17584 KB Output is correct
9 Correct 1456 ms 17440 KB Output is correct
10 Correct 1465 ms 17400 KB Output is correct
11 Correct 1447 ms 17528 KB Output is correct
12 Correct 1451 ms 17528 KB Output is correct
13 Correct 1467 ms 17528 KB Output is correct
14 Correct 1454 ms 17400 KB Output is correct
15 Correct 1457 ms 17528 KB Output is correct
16 Correct 264 ms 13816 KB Output is correct
17 Correct 283 ms 13944 KB Output is correct
18 Correct 276 ms 13944 KB Output is correct
19 Correct 294 ms 13944 KB Output is correct
20 Correct 278 ms 14020 KB Output is correct
21 Correct 1065 ms 17144 KB Output is correct
22 Correct 1053 ms 17144 KB Output is correct
23 Correct 988 ms 17124 KB Output is correct
24 Correct 1024 ms 17016 KB Output is correct
25 Correct 1060 ms 17144 KB Output is correct
26 Correct 1320 ms 17016 KB Output is correct
27 Correct 1297 ms 17004 KB Output is correct
28 Correct 1362 ms 17108 KB Output is correct
29 Correct 1330 ms 16916 KB Output is correct
30 Correct 1420 ms 17320 KB Output is correct
31 Correct 1428 ms 17656 KB Output is correct
32 Correct 1417 ms 17568 KB Output is correct
33 Correct 1422 ms 17480 KB Output is correct
34 Correct 1421 ms 17528 KB Output is correct
35 Correct 1419 ms 17436 KB Output is correct
36 Correct 1406 ms 17528 KB Output is correct
37 Correct 1428 ms 17488 KB Output is correct
38 Correct 1414 ms 17400 KB Output is correct
39 Correct 33 ms 13952 KB Output is correct
40 Correct 33 ms 13952 KB Output is correct
41 Correct 35 ms 13952 KB Output is correct
42 Correct 33 ms 13952 KB Output is correct
43 Correct 34 ms 13980 KB Output is correct
44 Correct 36 ms 13952 KB Output is correct
45 Correct 36 ms 13944 KB Output is correct
46 Correct 36 ms 13952 KB Output is correct
47 Correct 38 ms 13944 KB Output is correct
48 Correct 37 ms 13952 KB Output is correct
49 Correct 38 ms 13952 KB Output is correct
50 Correct 32 ms 13952 KB Output is correct
51 Correct 33 ms 13944 KB Output is correct
52 Correct 33 ms 13944 KB Output is correct
53 Correct 34 ms 13944 KB Output is correct
54 Correct 23 ms 13568 KB Output is correct
55 Correct 23 ms 13568 KB Output is correct
56 Correct 23 ms 13568 KB Output is correct
57 Correct 22 ms 13616 KB Output is correct
58 Correct 24 ms 13568 KB Output is correct
59 Execution timed out 2089 ms 17096 KB Time limit exceeded
60 Halted 0 ms 0 KB -