Submission #448274

# Submission time Handle Problem Language Result Execution time Memory
448274 2021-07-29T13:11:46 Z stefantaga Energetic turtle (IZhO11_turtle) C++14
90 / 100
2000 ms 1480 KB
#include <bits/stdc++.h>

using namespace std;
int frec[600005];
int k,t,z,n,m;
struct wow
{
    int x,y;
} v[105];
vector <int> prime;
bool c[600005];
int lim;
int ridput (int a,int b)
{
    if (b==0)
    {
        return 1;
    }
    int rez=ridput(a,b/2);
    if (b%2==0)
    {
        return (1LL*rez*rez)%z;
    }
    return (1LL*(1LL*rez*rez)%z*a)%z;
}
void adauga(int n,int val)
{
    int j;
    for (j=0; j<(int)prime.size(); j++)
    {
        long long p=prime[j];
        while (p<=n)
        {
            int sal=(n/p);
            frec[j]=frec[j]+1LL*sal*val;
            p=1LL*p*prime[j];
        }
    }
}
int combinari(int k,int n)
{
    int j;
    for (j=0; j<(int)prime.size(); j++)
    {
        frec[j]=0;
    }
    adauga(n,1);
    adauga(k,-1);
    adauga(n-k,-1);
    int p=1;
    for (j=0; j<(int)prime.size(); j++)
    {
        p=(1LL*p*ridput(prime[j],frec[j]))%z;
    }
    return p;
}
bool dif (int x,int y,int x2,int y2)
{
    if (x!=x2||y!=y2)
    {
        return 1;
    }
    return 0;
}
bool nimic(int x,int y,int x2,int y2)
{
    for (int i=1; i<=k; i++)
    {
        if (dif(v[i].x,v[i].y,x,y)==1&&dif(v[i].x,v[i].y,x2,y2)==1)
        {
            if (x<=v[i].x&&v[i].x<=x2&&y<=v[i].y&&v[i].y<=y2)
            {
                return 0;
            }
        }
    }
    return 1;
}
map <pair <int,int> ,int > mr;
int nimicpr[105][105];
int nr(int st,int dr)
{
    int x=v[st].x;
    int y=v[st].y;
    int x2=v[dr].x;
    int y2=v[dr].y;
    if (x>x2||y>y2)
    {
        swap(st,dr);
        swap(x,x2);
        swap(y,y2);
    }
    int inc=combinari(x2-x,x2-x+y2-y);
    if (nimic(x,y,x2,y2)==1)
    {
        return combinari(x2-x,x2-x+y2-y);
    }
    if (mr.find({st,dr})!=mr.end())
    {
            return mr[{st,dr}];
    }
    for (int i=1;i<=k;i++)
    {
        if (dif(v[i].x,v[i].y,x,y)==1&&dif(v[i].x,v[i].y,x2,y2)==1)
        {
            if (x<=v[i].x&&v[i].x<=x2&&y<=v[i].y&&v[i].y<=y2)
            {
                inc=(inc-(1LL*nr(st,i)*combinari(x2-v[i].x,x2-v[i].x+y2-v[i].y))%z+z)%z;
            }
        }
    }
    mr[{st,dr}]=mr[{dr,st}]=inc;
    return inc;
}
bool compare(int a,int b)
{
    return v[a].x<v[b].x||(v[a].x==v[b].x&&v[a].y<v[b].y);
}
long long i,j,dist[105][105];
int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);
#ifdef HOME
    ifstream cin("turtle.in");
    ofstream cout("turtle.out");
#endif // HOME
    cin>>n>>m>>k>>t>>z;
    n++;
    m++;
    lim=600000;
    c[0]=c[1]=1;
    for (i=2; i<=lim; i++)
    {
        if (c[i]==0)
        {
            prime.push_back(i);
            for (j=i*i; j<=lim; j+=i)
            {
                c[j]=1;
            }
        }
    }
    for (i=1; i<=k; i++)
    {
        cin>>v[i].x>>v[i].y;
        v[i].x++;
        v[i].y++;
    }
    v[0].x=1;
    v[0].y=1;
    v[k+1].x=n;
    v[k+1].y=m;
    lim=(1<<k);
    for (i=0;i<=k+1;i++)
    {
        for (j=0;j<=k+1;j++)
        {
            if (i!=j)
            {
                dist[i][j]=dist[j][i]=nr(i,j);
            }
        }
    }
    long long sum=nr(0,k+1);
    for (int i=1;i<lim;i++)
    {
        vector <int> sol;
        for (int j=0;j<k;j++)
        {
            if (i&(1<<j))
            {
                sol.push_back(j+1);
            }
        }
        int ok=1;
        sort (sol.begin(),sol.end(),compare);
        for (int j=1;j<sol.size();j++)
        {
            if (v[sol[j-1]].y>v[sol[j]].y)
            {
                ok=0;
                break;
            }
        }
        if (sol.size()>t)
        {
            ok=0;
        }
        if (ok==0)
        {
            continue;
        }
        int p=dist[0][sol[0]];
        for (int j=1;j<sol.size();j++)
        {
            p=(1LL*p*dist[sol[j-1]][sol[j]])%z;
        }
        int ult=(int)sol.size()-1;
        p=(1LL*p*dist[sol[ult]][k+1])%z;
        sum=(sum+p)%z;
    }
    //cout<<brut()<<'\n';
    cout<<sum;
    return 0;
}

Compilation message

turtle.cpp: In function 'int main()':
turtle.cpp:178:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |         for (int j=1;j<sol.size();j++)
      |                      ~^~~~~~~~~~~
turtle.cpp:186:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  186 |         if (sol.size()>t)
      |             ~~~~~~~~~~^~
turtle.cpp:195:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  195 |         for (int j=1;j<sol.size();j++)
      |                      ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1360 KB Output is correct
2 Correct 93 ms 1448 KB Output is correct
3 Correct 62 ms 1360 KB Output is correct
4 Correct 473 ms 1364 KB Output is correct
5 Correct 1333 ms 1436 KB Output is correct
6 Correct 416 ms 1360 KB Output is correct
7 Correct 572 ms 1480 KB Output is correct
8 Correct 288 ms 1440 KB Output is correct
9 Correct 816 ms 1436 KB Output is correct
10 Correct 833 ms 1432 KB Output is correct
11 Correct 617 ms 1480 KB Output is correct
12 Correct 1226 ms 1360 KB Output is correct
13 Correct 303 ms 1360 KB Output is correct
14 Correct 1405 ms 1432 KB Output is correct
15 Correct 1607 ms 1480 KB Output is correct
16 Correct 1610 ms 1436 KB Output is correct
17 Correct 1037 ms 1440 KB Output is correct
18 Execution timed out 2093 ms 1360 KB Time limit exceeded
19 Execution timed out 2076 ms 1428 KB Time limit exceeded
20 Correct 1350 ms 1432 KB Output is correct