Submission #44531

# Submission time Handle Problem Language Result Execution time Memory
44531 2018-04-02T23:23:25 Z RezwanArefin01 Boat (APIO16_boat) C++17
100 / 100
368 ms 1552 KB
/*
 * Author: Geunwoo Bae(functionx)
 * Time Complexity: O(N^2 lg N)
 */
#include<stdio.h>
#include<cmath>
#include<complex>
#include<algorithm>
#include<vector>
#define mod 1000000007
using namespace std;
 
typedef long long lld;
typedef complex<double> base;
 
struct qry {
    int ix;
    lld x, pm;
    bool operator< (const qry& c) const {
        return x<c.x;
    }
}que[11010];
 
int n, chk[5050], act, qcn;
lld dy[5050], inv[5050];
 
void fft(vector <base> &a, bool invert){
    int n = a.size();
    for(int i=1,j=0; i<n; i++){
        int bit = n>>1;
        for(; j>=bit; bit>>=1)j -= bit;
        j += bit;
        if(i<j)swap(a[i], a[j]);
    }
    for(int len=2; len<=n; len<<=1){
        double ang = 2*M_PI/len*(invert?-1:1);
        base wlen(cos(ang), sin(ang));
        for(int i=0; i<n; i+=len){
            base w(1);
            for(int j=0; j<len/2; j++){
                base u=a[i+j], v=a[i+j+len/2]*w;
                a[i+j] = u+v;
                a[i+j+len/2] = u-v;
                w *= wlen;
            }
        }
    }
    if(invert){
        for(int i=0; i<n; i++)a[i] /= n;
    }
}
 
void convolution(vector<lld>& x, vector<lld>& y, const int sz){
    vector<base> fa1, fa2, fb1, fb2;
    int n, i;
    for(n=1; n<sz; n<<=1);
    for(i=0; i<n; i++){
        fa1.push_back(0), fa2.push_back(0);
        fb1.push_back(0), fb2.push_back(0);
    }
    for(i=0; i<sz; i++){
        fa1.push_back(x[i]/30000), fa2.push_back(x[i]%30000);
        fb1.push_back(y[i]/30000), fb2.push_back(y[i]%30000);
    }
    n<<=1;
    fa1.resize(n), fa2.resize(n);
    fb1.resize(n), fb2.resize(n);
    fft(fa1, 0), fft(fa2, 0);
    fft(fb1, 0), fft(fb2, 0);
    for(i=0; i<n; i++){
        base a1b1 = fa1[i]*fb1[i], a1b2 = fa1[i]*fb2[i];
        base a2b1 = fa2[i]*fb1[i], a2b2 = fa2[i]*fb2[i];
        fa1[i] = a1b1, fa2[i] = a1b2, fb1[i] = a2b1, fb2[i] = a2b2;
    }
    fft(fa1, 1), fft(fa2, 1);
    fft(fb1, 1), fft(fb2, 1);
    for(i=0; i<sz; i++){
        lld a1b1, a1b2, a2b1, a2b2;
        a1b1 = lld(fa1[i].real() + (fa1[i].real()>0 ? 0.5:-0.5)), a1b1%=mod;
        a1b2 = lld(fa2[i].real() + (fa2[i].real()>0 ? 0.5:-0.5)), a1b2%=mod;
        a2b1 = lld(fb1[i].real() + (fb1[i].real()>0 ? 0.5:-0.5)), a2b1%=mod;
        a2b2 = lld(fb2[i].real() + (fb2[i].real()>0 ? 0.5:-0.5)), a2b2%=mod;
        x[i] = (a1b1*900000000ll + (a1b2+a2b1)*30000ll + a2b2)%mod;
    }
}
 
/*
void convolution(vector<lld>& x, vector<lld>& y, const int sz){
    int i, j;
    for(i=sz-1; i>=0; i--){
        lld sum=0;
        for(j=0; j<=i; j++){
            sum+=x[j]*y[i-j];
            sum%=mod;
        }
        x[i]=sum;
    }
}*/
 
lld pw(lld a, lld b){
    if(b<=0)return 1;
    lld g=pw(a,b/2);
    g=(g*g)%mod;
    if(b%2)g=(g*a)%mod;
    return g;
}
 
int main(){
    int i;
    lld pv;
    scanf("%d", &n);
    for(i=0; i<n; i++){
        lld s, e;
        scanf("%lld%lld", &s, &e);
        que[qcn].ix=i, que[qcn].x=s, que[qcn++].pm=1;
        que[qcn].ix=i, que[qcn].x=e+1, que[qcn++].pm=-1;
    }
    for(i=1; i<=n; i++)inv[i]=pw(i, mod-2);
    sort(que, que+qcn);
     
    pv=0;
    for(i=0; i<qcn; i++){
        if(pv<que[i].x && act){
            lld psum=-1, sum=0, mul=1;
            int j, ccn=0;
            vector <lld> p, q;
            for(j=0; j<n; j++){
                if(chk[j]){
                    lld arg = sum-psum+mod;
                    while(arg>=mod)arg-=mod;
                    mul *= (que[i].x-pv+ccn)*inv[ccn+1]%mod;
                    mul %= mod;
                    p.push_back(arg);
                    q.push_back(mul);
                    psum=sum;
                    ccn++;
                }
                sum += dy[j];
                if(sum >= mod)sum -= mod;
            }
            convolution(p, q, ccn);
            for(j=ccn=0; j<n; j++){
                if(chk[j]){
                    dy[j] += p[ccn];
                    if(dy[j] >= mod)dy[j] -= mod;
                    ccn++;
                }
            }
        }
/*      printf("%lld ~ %lld:\n", pv, que[i].x-1);
        for(int j=0; j<n; j++)printf("%lld ", dy[j]);
        puts("");*/
        pv = que[i].x;
        chk[que[i].ix] += que[i].pm, act += que[i].pm;
    }
     
    lld sum=0;
    for(i=0; i<n; i++){
        sum += dy[i];
        if(sum >= mod)sum -= mod;
    }
    printf("%lld", sum);
    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:111:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
boat.cpp:114:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld", &s, &e);
         ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 452 KB Output is correct
4 Correct 5 ms 464 KB Output is correct
5 Correct 5 ms 476 KB Output is correct
6 Correct 5 ms 488 KB Output is correct
7 Correct 5 ms 500 KB Output is correct
8 Correct 5 ms 564 KB Output is correct
9 Correct 5 ms 736 KB Output is correct
10 Correct 5 ms 736 KB Output is correct
11 Correct 5 ms 904 KB Output is correct
12 Correct 5 ms 904 KB Output is correct
13 Correct 5 ms 904 KB Output is correct
14 Correct 5 ms 904 KB Output is correct
15 Correct 5 ms 904 KB Output is correct
16 Correct 3 ms 904 KB Output is correct
17 Correct 3 ms 904 KB Output is correct
18 Correct 3 ms 904 KB Output is correct
19 Correct 3 ms 904 KB Output is correct
20 Correct 3 ms 904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 452 KB Output is correct
4 Correct 5 ms 464 KB Output is correct
5 Correct 5 ms 476 KB Output is correct
6 Correct 5 ms 488 KB Output is correct
7 Correct 5 ms 500 KB Output is correct
8 Correct 5 ms 564 KB Output is correct
9 Correct 5 ms 736 KB Output is correct
10 Correct 5 ms 736 KB Output is correct
11 Correct 5 ms 904 KB Output is correct
12 Correct 5 ms 904 KB Output is correct
13 Correct 5 ms 904 KB Output is correct
14 Correct 5 ms 904 KB Output is correct
15 Correct 5 ms 904 KB Output is correct
16 Correct 3 ms 904 KB Output is correct
17 Correct 3 ms 904 KB Output is correct
18 Correct 3 ms 904 KB Output is correct
19 Correct 3 ms 904 KB Output is correct
20 Correct 3 ms 904 KB Output is correct
21 Correct 201 ms 904 KB Output is correct
22 Correct 216 ms 904 KB Output is correct
23 Correct 186 ms 904 KB Output is correct
24 Correct 205 ms 952 KB Output is correct
25 Correct 200 ms 952 KB Output is correct
26 Correct 293 ms 952 KB Output is correct
27 Correct 310 ms 1044 KB Output is correct
28 Correct 299 ms 1044 KB Output is correct
29 Correct 300 ms 1044 KB Output is correct
30 Correct 10 ms 1044 KB Output is correct
31 Correct 10 ms 1044 KB Output is correct
32 Correct 10 ms 1044 KB Output is correct
33 Correct 10 ms 1044 KB Output is correct
34 Correct 10 ms 1044 KB Output is correct
35 Correct 10 ms 1044 KB Output is correct
36 Correct 10 ms 1044 KB Output is correct
37 Correct 10 ms 1044 KB Output is correct
38 Correct 10 ms 1044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1044 KB Output is correct
2 Correct 11 ms 1044 KB Output is correct
3 Correct 15 ms 1044 KB Output is correct
4 Correct 11 ms 1044 KB Output is correct
5 Correct 12 ms 1044 KB Output is correct
6 Correct 16 ms 1044 KB Output is correct
7 Correct 16 ms 1044 KB Output is correct
8 Correct 15 ms 1044 KB Output is correct
9 Correct 17 ms 1044 KB Output is correct
10 Correct 17 ms 1044 KB Output is correct
11 Correct 12 ms 1044 KB Output is correct
12 Correct 11 ms 1044 KB Output is correct
13 Correct 11 ms 1044 KB Output is correct
14 Correct 12 ms 1116 KB Output is correct
15 Correct 11 ms 1152 KB Output is correct
16 Correct 7 ms 1152 KB Output is correct
17 Correct 9 ms 1152 KB Output is correct
18 Correct 7 ms 1152 KB Output is correct
19 Correct 7 ms 1152 KB Output is correct
20 Correct 7 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 452 KB Output is correct
4 Correct 5 ms 464 KB Output is correct
5 Correct 5 ms 476 KB Output is correct
6 Correct 5 ms 488 KB Output is correct
7 Correct 5 ms 500 KB Output is correct
8 Correct 5 ms 564 KB Output is correct
9 Correct 5 ms 736 KB Output is correct
10 Correct 5 ms 736 KB Output is correct
11 Correct 5 ms 904 KB Output is correct
12 Correct 5 ms 904 KB Output is correct
13 Correct 5 ms 904 KB Output is correct
14 Correct 5 ms 904 KB Output is correct
15 Correct 5 ms 904 KB Output is correct
16 Correct 3 ms 904 KB Output is correct
17 Correct 3 ms 904 KB Output is correct
18 Correct 3 ms 904 KB Output is correct
19 Correct 3 ms 904 KB Output is correct
20 Correct 3 ms 904 KB Output is correct
21 Correct 201 ms 904 KB Output is correct
22 Correct 216 ms 904 KB Output is correct
23 Correct 186 ms 904 KB Output is correct
24 Correct 205 ms 952 KB Output is correct
25 Correct 200 ms 952 KB Output is correct
26 Correct 293 ms 952 KB Output is correct
27 Correct 310 ms 1044 KB Output is correct
28 Correct 299 ms 1044 KB Output is correct
29 Correct 300 ms 1044 KB Output is correct
30 Correct 10 ms 1044 KB Output is correct
31 Correct 10 ms 1044 KB Output is correct
32 Correct 10 ms 1044 KB Output is correct
33 Correct 10 ms 1044 KB Output is correct
34 Correct 10 ms 1044 KB Output is correct
35 Correct 10 ms 1044 KB Output is correct
36 Correct 10 ms 1044 KB Output is correct
37 Correct 10 ms 1044 KB Output is correct
38 Correct 10 ms 1044 KB Output is correct
39 Correct 11 ms 1044 KB Output is correct
40 Correct 11 ms 1044 KB Output is correct
41 Correct 15 ms 1044 KB Output is correct
42 Correct 11 ms 1044 KB Output is correct
43 Correct 12 ms 1044 KB Output is correct
44 Correct 16 ms 1044 KB Output is correct
45 Correct 16 ms 1044 KB Output is correct
46 Correct 15 ms 1044 KB Output is correct
47 Correct 17 ms 1044 KB Output is correct
48 Correct 17 ms 1044 KB Output is correct
49 Correct 12 ms 1044 KB Output is correct
50 Correct 11 ms 1044 KB Output is correct
51 Correct 11 ms 1044 KB Output is correct
52 Correct 12 ms 1116 KB Output is correct
53 Correct 11 ms 1152 KB Output is correct
54 Correct 7 ms 1152 KB Output is correct
55 Correct 9 ms 1152 KB Output is correct
56 Correct 7 ms 1152 KB Output is correct
57 Correct 7 ms 1152 KB Output is correct
58 Correct 7 ms 1152 KB Output is correct
59 Correct 221 ms 1168 KB Output is correct
60 Correct 201 ms 1232 KB Output is correct
61 Correct 203 ms 1232 KB Output is correct
62 Correct 266 ms 1304 KB Output is correct
63 Correct 204 ms 1316 KB Output is correct
64 Correct 368 ms 1328 KB Output is correct
65 Correct 351 ms 1340 KB Output is correct
66 Correct 336 ms 1416 KB Output is correct
67 Correct 341 ms 1416 KB Output is correct
68 Correct 344 ms 1416 KB Output is correct
69 Correct 199 ms 1416 KB Output is correct
70 Correct 214 ms 1416 KB Output is correct
71 Correct 201 ms 1416 KB Output is correct
72 Correct 205 ms 1480 KB Output is correct
73 Correct 210 ms 1508 KB Output is correct
74 Correct 42 ms 1508 KB Output is correct
75 Correct 41 ms 1508 KB Output is correct
76 Correct 42 ms 1508 KB Output is correct
77 Correct 44 ms 1540 KB Output is correct
78 Correct 51 ms 1552 KB Output is correct