Submission #478033

# Submission time Handle Problem Language Result Execution time Memory
478033 2021-10-05T06:44:40 Z stefantaga Lucky Numbers (RMI19_lucky) C++14
100 / 100
183 ms 10112 KB
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
struct wow
{
    int are1,are31,are3,ebun,nrcifre;
    int cntbune,cnt1,cnt31,cnt3;
};
wow arb[400005];
int din[100005];
int scade (int x,int y)
{
    return (x%MOD-y%MOD+MOD)%MOD;
}
int inmult(int x,int y)
{
    return (1LL*x*y)%MOD;
}
int aduna(int x,int y)
{
    return (x+y)%MOD;
}
void initializeaza(wow &x)
{
    x.are1=x.are31=x.are3=x.ebun=x.cntbune=x.cnt1=x.cnt31=x.cnt3=x.nrcifre=0;
}
wow adunawow(wow a,wow b)
{
    wow rez;
    initializeaza(rez);
    rez.nrcifre=a.nrcifre+b.nrcifre;
    if (a.are1==0||b.are3==0)
    {
        if (a.ebun==1&&b.ebun==1)
        {
            rez.ebun=1;
        }
        if (a.are3&&b.are1)
        {
            rez.are31=1;
        }
        if (b.are1&&a.ebun)
        {
            rez.are1=1;
        }
        if (a.are3&&b.ebun)
        {
            rez.are3=1;
        }
    }
    int nr=b.nrcifre;
    rez.cntbune=scade(aduna(inmult(scade(a.cntbune,a.ebun),din[nr]),inmult(a.ebun,b.cntbune)),aduna(inmult(scade(a.cnt1,a.are1),din[nr-1]),inmult(b.cnt3,a.are1)));
    rez.cnt1=scade(aduna(inmult(scade(a.cntbune,a.ebun),din[nr-1]),inmult(a.ebun,b.cnt1)),aduna(inmult(scade(a.cnt1,a.are1),din[nr-2]),inmult(b.cnt31,a.are1)));
    rez.cnt3=scade(aduna(inmult(scade(a.cnt3,a.are3),din[nr]),inmult(a.are3,b.cntbune)),aduna(inmult(scade(a.cnt31,a.are31),din[nr-1]),inmult(a.are31,b.cnt3)));
    rez.cnt31=scade(aduna(inmult(scade(a.cnt3,a.are3),din[nr-1]),inmult(a.are3,b.cnt1)),aduna(inmult(scade(a.cnt31,a.are31),din[nr-2]),inmult(a.are31,b.cnt31)));
    return rez;
}
void update(int st,int dr,int nod,int poz,int val)
{
    if (st==dr)
    {
        initializeaza(arb[nod]);
        arb[nod].cntbune=val+1;
        arb[nod].ebun=1;
        arb[nod].nrcifre=1;
        if (val==1)
        {
            arb[nod].are1=1;
        }
        if (val>=1)
        {
            arb[nod].cnt1=1;
        }
        if (val>=3)
        {
            arb[nod].cnt3=1;
        }
        if (val==3)
        {
            arb[nod].are3=1;
        }
        return;
    }
    int mij=(st+dr)/2;
    if (poz<=mij)
    {
        update(st,mij,2*nod,poz,val);
    }
    else
    {
        update(mij+1,dr,2*nod+1,poz,val);
    }
    arb[nod]=adunawow(arb[2*nod],arb[2*nod+1]);
}
wow query(int st,int dr,int nod,int ua,int ub)
{
    if (ua<=st&&dr<=ub)
    {
        return arb[nod];
    }
    int ok=0,mij=(st+dr)/2;
    if (mij<ua)
    {
        return query(mij+1,dr,2*nod+1,ua,ub);
    }
    else
    if (ub<=mij)
    {
        return query(st,mij,2*nod,ua,ub);
    }
    else
    {
        return adunawow(query(st,mij,2*nod,ua,ub),query(mij+1,dr,2*nod+1,ua,ub));
    }
}
int n,m,i,cifra,tip,x,y;
char s[100005];
int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);
    #ifdef HOME
    ifstream cin("date.in");
    ofstream cout("date.out");
    #endif // HOME
    cin>>n>>m;
    cin>>s;
    din[0]=1;
    din[1]=10;
    for (i=2;i<=100000;i++)
    {
        din[i]=scade(inmult(din[i-1],10),din[i-2]);
    }
    for (i=0;i<n;i++)
    {
        cifra=s[i]-'0';
        update(1,n,1,i+1,cifra);
    }
    cout<<query(1,n,1,1,n).cntbune<<'\n';
    for (i=1;i<=m;i++)
    {
        cin>>tip>>x>>y;
        if (tip==2)
        {
            update(1,n,1,x,y);
        }
        else
        {
            cout<<query(1,n,1,x,y).cntbune<<'\n';
        }
    }
    return 0;
}

Compilation message

lucky.cpp: In function 'wow query(int, int, int, int, int)':
lucky.cpp:101:9: warning: unused variable 'ok' [-Wunused-variable]
  101 |     int ok=0,mij=(st+dr)/2;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 2 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 1344 KB Output is correct
2 Correct 39 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 1344 KB Output is correct
2 Correct 39 ms 1868 KB Output is correct
3 Correct 162 ms 10076 KB Output is correct
4 Correct 122 ms 10092 KB Output is correct
5 Correct 142 ms 10080 KB Output is correct
6 Correct 159 ms 10072 KB Output is correct
7 Correct 178 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 2 ms 768 KB Output is correct
7 Correct 28 ms 1344 KB Output is correct
8 Correct 39 ms 1868 KB Output is correct
9 Correct 20 ms 1356 KB Output is correct
10 Correct 28 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 2 ms 768 KB Output is correct
7 Correct 28 ms 1344 KB Output is correct
8 Correct 39 ms 1868 KB Output is correct
9 Correct 162 ms 10076 KB Output is correct
10 Correct 122 ms 10092 KB Output is correct
11 Correct 142 ms 10080 KB Output is correct
12 Correct 159 ms 10072 KB Output is correct
13 Correct 178 ms 10112 KB Output is correct
14 Correct 20 ms 1356 KB Output is correct
15 Correct 28 ms 1884 KB Output is correct
16 Correct 123 ms 10008 KB Output is correct
17 Correct 134 ms 10036 KB Output is correct
18 Correct 183 ms 10008 KB Output is correct
19 Correct 165 ms 10012 KB Output is correct
20 Correct 157 ms 9964 KB Output is correct