zoukankan      html  css  js  c++  java
  • LightOJ

    链接:

    https://vjudge.net/problem/LightOJ-1333

    题意:

    You have to color an M x N two dimensional grid. You will be provided K different colors for this. You will also be provided a list of B blocked cells of this grid. You cannot color these blocked cells.

    A cell can be described as (x, y), which points to the yth cell from the left of the xth row from the top.

    While coloring the grid, you have to follow these rules -

    1.  You have to color each cell which is not blocked.
      
    2.  You cannot color a blocked cell.
      
    3.  You can choose exactly one color from K given colors to color a cell.
      
    4.  No two vertically adjacent cells can have the same color, i.e. cell (x, y) and cell (x + 1, y) shouldn't contain the same color.
      

    You have to calculate the number of ways you can color this grid obeying all the rules provided.

    思路:

    把不能填的位置记录排序,然后枚举每个不能填的位置,同时维护上一个能填的位置,计算之间的种类,乘起来

    代码:

    // #include<bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string.h>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    const int MOD = 1e9;
    const int MAXN = 1e6+10;
    
    struct Node
    {
        int x, y;
        bool operator < (const Node& rhs) const
        {
            if (this->y != rhs.y)
                return this->y < rhs.y;
            return this->x < rhs.x;
        }
    }node[510];
    int n, m, k, b;
    
    LL PowMod(LL a, LL b, LL p)
    {
        LL res = 1;
        while(b)
        {
            if (b&1)
                res = res*a%p;
            a = a*a%p;
            b >>= 1;
        }
        return res;
    }
    
    LL GetVal(LL len)
    {
        if (len <= 0)
            return 1;
        if (len == 1)
            return k;
        return k*PowMod(k-1, len-1, MOD)%MOD;
    }
    
    int main()
    {
        // freopen("test.in", "r", stdin);
        int t, cas = 0;
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d%d%d%d", &n, &m, &k, &b);
            printf("Case %d:", ++cas);
            for (int i = 1;i <= b;i++)
                scanf("%d%d", &node[i].x, &node[i].y);
            sort(node+1, node+1+b);
            int x = 1, y = 1;
            LL ans = 1;
            if (b == 0)
            {
                ans = k*PowMod(k-1, n-1, MOD)%MOD;
                ans = PowMod(ans, m, MOD);
            }
            for (int i = 1;i <= b;i++)
            {
                if (node[i].y == y)
                {
                    LL tmp = GetVal(node[i].x-x);
                    ans = ans*tmp%MOD;
                    x = node[i].x+1;
                    if (x > n)
                        x = 1, y++;
                }
                else
                {
                    LL tmp = GetVal(n-x+1);
                    ans = ans*tmp%MOD;
                    tmp = GetVal(n);
                    ans = ans*PowMod(tmp, node[i].y-y-1, MOD)%MOD;
                    x = 1, y = node[i].y;
                    i--;
                }
                // cout << ans << ' ' << x << ' ' << y << endl;
            }
            if (y <= m && b != 0)
            {
                LL tmp = GetVal(n-x+1);
                ans = ans*tmp%MOD;
                tmp = GetVal(n);
                ans = ans*PowMod(tmp, m-y, MOD)%MOD;
            }
            printf(" %lld
    ", ans);
        }
    
        return 0;
    }
    
  • 相关阅读:
    poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)
    Git-删除本地文件夹的repository(本地仓库)
    Unique path ii
    jeecms使用小结
    jeecms9自定义标签以及使用新创建的数据库表
    Jeecms网站直接访问html静态页面
    jeecms系统使用介绍——jeecms中的内容、栏目、模型之间的关系
    jeecms内容管理系统使用了哪些技术
    jeecms附件标签用法
    jeecms v8 网站访问量配置
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12019529.html
Copyright © 2011-2022 走看看