Submission #784211

# Submission time Handle Problem Language Result Execution time Memory
784211 2023-07-15T21:05:57 Z oscar1f Digital Circuit (IOI22_circuit) C++17
100 / 100
1014 ms 27332 KB
#include "circuit.h"
#include<bits/stdc++.h>
using namespace std;

#define ll long long

struct noeud{
    ll give0,give1,coeff,deb,fin;
};

const ll TAILLE_MAX=300*1000+5,MODU=1000*1000*1000+2022,NB_FACT=3;
ll nbSom,nbSource,rep;
pair<ll,ll> prodGlob[NB_FACT];
vector<ll> pere,prem;
vector<ll> etatDeb;
pair<ll,ll> prodSom[TAILLE_MAX][NB_FACT];
ll nbFils[TAILLE_MAX];
ll valMod;
ll val[TAILLE_MAX];
noeud cumu[TAILLE_MAX];

pair<ll,ll> decompo(ll nb,ll p) {
    if (nb==0) {
        return {1,0};
    }
    ll nbOccu=0;
    while (nb%p==0) {
        nbOccu++;
        nb/=p;
    }
    return {nb%p,nbOccu};
}

ll puis(ll nb,ll expo,ll p) {
    if (expo==0) {
        return 1;
    }
    ll ans=puis(nb,expo/2,p);
    ans*=ans;
    ans%=p;
    if (expo%2==1) {
        ans*=nb;
        ans%=p;
    }
    return ans;
}

ll inverse(ll nb,ll p) {
    return puis(nb,p-2,p);
}

void prepa(ll pos,ll debInter,ll finInter) {
    cumu[pos].deb=debInter;
    cumu[pos].fin=finInter;
    if (debInter==finInter) {
        cumu[pos].give0=val[debInter]%MODU;
    }
    else {
        ll midInter=(debInter+finInter)/2;
        prepa(2*pos,debInter,midInter);
        prepa(2*pos+1,midInter+1,finInter);
        cumu[pos].give0=(cumu[2*pos].give0+cumu[2*pos+1].give0)%MODU;
    }
}

void pushFlag(ll pos) {
    if (cumu[pos].coeff%2==1) {
        swap(cumu[pos].give0,cumu[pos].give1);
    }
    if (cumu[pos].deb!=cumu[pos].fin) {
        cumu[2*pos].coeff+=cumu[pos].coeff;
        cumu[2*pos].coeff%=2;
        cumu[2*pos+1].coeff+=cumu[pos].coeff;
        cumu[2*pos+1].coeff%=2;
    }
    cumu[pos].coeff=0;
}

pair<ll,ll> update(ll pos,ll debInter,ll finInter) {
    //cout<<pos<<" "<<debInter<<" "<<finInter<<" : ";
    if (cumu[pos].deb>finInter or cumu[pos].fin<debInter) {
        //cout<<"A"<<endl;
        pushFlag(pos);
        return {cumu[pos].give0,cumu[pos].give1};
    }
    if (cumu[pos].deb>=debInter and cumu[pos].fin<=finInter) {
        //cout<<"B"<<endl;
        cumu[pos].coeff++;
        pushFlag(pos);
        return {cumu[pos].give0,cumu[pos].give1};
    }
    //cout<<"C"<<endl;
    pushFlag(pos);
    auto gauche=update(2*pos,debInter,finInter);
    auto droite=update(2*pos+1,debInter,finInter);
    cumu[pos].give0=(gauche.first+droite.first)%MODU;
    cumu[pos].give1=(gauche.second+droite.second)%MODU;
    return {cumu[pos].give0,cumu[pos].give1};
}

void afficher() {
    cout<<endl;
    for (ll i=1;i<20;i++) {
        cout<<i<<" : "<<cumu[i].deb<<" "<<cumu[i].fin<<" "<<cumu[i].coeff<<" "<<cumu[i].give0<<" "<<cumu[i].give1<<endl;
    }
    cout<<endl;
}

void init(int N, int M, vector<int> P, vector<int> A) {
    nbSom=N;
    nbSource=M;
    for (ll i:P) {
        pere.push_back(i);
    }
    prem={2,223,MODU/(2*223)};
    for (ll i:A) {
       etatDeb.push_back(i);
    }
    for (ll i:pere) {
        if (i>=0) {
            nbFils[i]++;
        }
    }
    for (ll j=0;j<NB_FACT;j++) {
        prodGlob[j].first=1;
        for (ll i=0;i<nbSom;i++) {
            prodGlob[j].first*=decompo(nbFils[i],prem[j]).first;
            prodGlob[j].first%=prem[j];
            prodGlob[j].second+=decompo(nbFils[i],prem[j]).second;
        }
    }

    for (ll j=0;j<NB_FACT;j++) {
        prodSom[0][j]=decompo(nbFils[0],prem[j]);
        for (ll i=1;i<nbSom+nbSource;i++) {
            prodSom[i][j]=prodSom[pere[i]][j];
            prodSom[i][j].first*=decompo(nbFils[i],prem[j]).first;
            prodSom[i][j].first%=prem[j];
            prodSom[i][j].second+=decompo(nbFils[i],prem[j]).second;
        }
    }

    /*for (ll i=0;i<nbSom+nbSource;i++) {
        cout<<i<<" : ";
        for (ll j=0;j<NB_FACT;j++) {
            cout<<prodSom[i][j].first<<" "<<prodSom[i][j].second<<"   ";
        }
        cout<<endl;
    }*/
    /*for (int j=0;j<NB_FACT;j++) {
        cout<<prodGlob[j].first<<" "<<prodGlob[j].second<<endl;
    }*/

    for (ll i=0;i<nbSource;i++) {
        //cout<<i+nbSom<<" : ";
        for (ll j=0;j<NB_FACT;j++) {
            if (prodSom[i+nbSom][j].second<prodGlob[j].second) {
                valMod=0;
            }
            else {
                valMod=prodGlob[j].first*inverse(prodSom[i+nbSom][j].first,prem[j]);
                //cout<<prodGlob[j].first<<" "<<prodSom[i+nbSom][j].first<<" "<<valMod<<" "<<prem[j]<<endl;
                valMod%=prem[j];
            }
            //cout<<valMod<<" ";
            val[i]+=valMod*(((MODU/prem[j])*inverse((MODU/prem[j]),prem[j]))%MODU);
        }
        val[i]%=MODU;
        //cout<<endl;
    }

    prepa(1,0,nbSource-1);
    //afficher();

    for (ll i=0;i<nbSource;i++) {
        //cout<<i+nbSom<<" : "<<val[i]<<endl;
        if (etatDeb[i]==1) {
            rep=update(1,i,i).second;
            //afficher();
        }
    }
}

int count_ways(int L, int R) {
    L-=nbSom;
    R-=nbSom;
    rep=update(1,L,R).second;
    return rep%MODU;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 2 ms 516 KB Output is correct
8 Correct 2 ms 464 KB Output is correct
9 Correct 2 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 2 ms 500 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 2 ms 516 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 2 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 2 ms 500 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 2 ms 464 KB Output is correct
25 Correct 2 ms 584 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 2 ms 496 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 2 ms 464 KB Output is correct
37 Correct 2 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 464 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 508 ms 8220 KB Output is correct
2 Correct 858 ms 16160 KB Output is correct
3 Correct 823 ms 16228 KB Output is correct
4 Correct 684 ms 16196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 508 ms 8220 KB Output is correct
2 Correct 858 ms 16160 KB Output is correct
3 Correct 823 ms 16228 KB Output is correct
4 Correct 684 ms 16196 KB Output is correct
5 Correct 662 ms 8292 KB Output is correct
6 Correct 875 ms 16208 KB Output is correct
7 Correct 852 ms 16188 KB Output is correct
8 Correct 705 ms 16220 KB Output is correct
9 Correct 385 ms 824 KB Output is correct
10 Correct 609 ms 1352 KB Output is correct
11 Correct 684 ms 1336 KB Output is correct
12 Correct 613 ms 1340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 2 ms 516 KB Output is correct
8 Correct 2 ms 464 KB Output is correct
9 Correct 2 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 2 ms 500 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 508 ms 8220 KB Output is correct
14 Correct 858 ms 16160 KB Output is correct
15 Correct 823 ms 16228 KB Output is correct
16 Correct 684 ms 16196 KB Output is correct
17 Correct 662 ms 8292 KB Output is correct
18 Correct 875 ms 16208 KB Output is correct
19 Correct 852 ms 16188 KB Output is correct
20 Correct 705 ms 16220 KB Output is correct
21 Correct 385 ms 824 KB Output is correct
22 Correct 609 ms 1352 KB Output is correct
23 Correct 684 ms 1336 KB Output is correct
24 Correct 613 ms 1340 KB Output is correct
25 Correct 800 ms 26980 KB Output is correct
26 Correct 997 ms 27192 KB Output is correct
27 Correct 999 ms 27224 KB Output is correct
28 Correct 697 ms 27220 KB Output is correct
29 Correct 905 ms 27160 KB Output is correct
30 Correct 972 ms 27164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 2 ms 516 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 2 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 2 ms 500 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 2 ms 464 KB Output is correct
25 Correct 2 ms 584 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 2 ms 496 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 2 ms 464 KB Output is correct
37 Correct 2 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 464 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 464 ms 1152 KB Output is correct
44 Correct 693 ms 1212 KB Output is correct
45 Correct 801 ms 1188 KB Output is correct
46 Correct 819 ms 1876 KB Output is correct
47 Correct 897 ms 1872 KB Output is correct
48 Correct 752 ms 1876 KB Output is correct
49 Correct 839 ms 1872 KB Output is correct
50 Correct 686 ms 1872 KB Output is correct
51 Correct 703 ms 1476 KB Output is correct
52 Correct 605 ms 1488 KB Output is correct
53 Correct 642 ms 728 KB Output is correct
54 Correct 771 ms 1872 KB Output is correct
55 Correct 795 ms 1616 KB Output is correct
56 Correct 699 ms 1488 KB Output is correct
57 Correct 805 ms 1488 KB Output is correct
58 Correct 747 ms 1872 KB Output is correct
59 Correct 672 ms 1920 KB Output is correct
60 Correct 742 ms 1872 KB Output is correct
61 Correct 729 ms 1200 KB Output is correct
62 Correct 763 ms 1160 KB Output is correct
63 Correct 634 ms 1152 KB Output is correct
64 Correct 846 ms 1488 KB Output is correct
65 Correct 374 ms 820 KB Output is correct
66 Correct 727 ms 1336 KB Output is correct
67 Correct 766 ms 1360 KB Output is correct
68 Correct 694 ms 1336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 2 ms 516 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 2 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 2 ms 500 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 2 ms 464 KB Output is correct
25 Correct 2 ms 584 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 2 ms 496 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 2 ms 464 KB Output is correct
37 Correct 2 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 464 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 508 ms 8220 KB Output is correct
44 Correct 858 ms 16160 KB Output is correct
45 Correct 823 ms 16228 KB Output is correct
46 Correct 684 ms 16196 KB Output is correct
47 Correct 662 ms 8292 KB Output is correct
48 Correct 875 ms 16208 KB Output is correct
49 Correct 852 ms 16188 KB Output is correct
50 Correct 705 ms 16220 KB Output is correct
51 Correct 385 ms 824 KB Output is correct
52 Correct 609 ms 1352 KB Output is correct
53 Correct 684 ms 1336 KB Output is correct
54 Correct 613 ms 1340 KB Output is correct
55 Correct 800 ms 26980 KB Output is correct
56 Correct 997 ms 27192 KB Output is correct
57 Correct 999 ms 27224 KB Output is correct
58 Correct 697 ms 27220 KB Output is correct
59 Correct 905 ms 27160 KB Output is correct
60 Correct 972 ms 27164 KB Output is correct
61 Correct 464 ms 1152 KB Output is correct
62 Correct 693 ms 1212 KB Output is correct
63 Correct 801 ms 1188 KB Output is correct
64 Correct 819 ms 1876 KB Output is correct
65 Correct 897 ms 1872 KB Output is correct
66 Correct 752 ms 1876 KB Output is correct
67 Correct 839 ms 1872 KB Output is correct
68 Correct 686 ms 1872 KB Output is correct
69 Correct 703 ms 1476 KB Output is correct
70 Correct 605 ms 1488 KB Output is correct
71 Correct 642 ms 728 KB Output is correct
72 Correct 771 ms 1872 KB Output is correct
73 Correct 795 ms 1616 KB Output is correct
74 Correct 699 ms 1488 KB Output is correct
75 Correct 805 ms 1488 KB Output is correct
76 Correct 747 ms 1872 KB Output is correct
77 Correct 672 ms 1920 KB Output is correct
78 Correct 742 ms 1872 KB Output is correct
79 Correct 729 ms 1200 KB Output is correct
80 Correct 763 ms 1160 KB Output is correct
81 Correct 634 ms 1152 KB Output is correct
82 Correct 846 ms 1488 KB Output is correct
83 Correct 374 ms 820 KB Output is correct
84 Correct 727 ms 1336 KB Output is correct
85 Correct 766 ms 1360 KB Output is correct
86 Correct 694 ms 1336 KB Output is correct
87 Correct 1 ms 208 KB Output is correct
88 Correct 593 ms 25228 KB Output is correct
89 Correct 909 ms 15716 KB Output is correct
90 Correct 838 ms 15640 KB Output is correct
91 Correct 886 ms 27236 KB Output is correct
92 Correct 839 ms 27204 KB Output is correct
93 Correct 869 ms 27144 KB Output is correct
94 Correct 947 ms 27176 KB Output is correct
95 Correct 862 ms 27160 KB Output is correct
96 Correct 949 ms 19284 KB Output is correct
97 Correct 936 ms 19264 KB Output is correct
98 Correct 586 ms 7472 KB Output is correct
99 Correct 947 ms 27192 KB Output is correct
100 Correct 797 ms 23664 KB Output is correct
101 Correct 866 ms 21088 KB Output is correct
102 Correct 951 ms 19312 KB Output is correct
103 Correct 1014 ms 27332 KB Output is correct
104 Correct 928 ms 27156 KB Output is correct
105 Correct 853 ms 27244 KB Output is correct
106 Correct 891 ms 14996 KB Output is correct
107 Correct 925 ms 17312 KB Output is correct
108 Correct 970 ms 19492 KB Output is correct
109 Correct 953 ms 19288 KB Output is correct