LightOJ 1333 结构体排序

来源:互联网 发布:香港研究生网络面试 编辑:程序博客网 时间:2024/06/06 12:33

题目大意就是求出每一列的颜色组合数之积,一个不可涂色的方块等同于将一列分成2列,开始对不可涂色的方块进行列排序,在进行行排序,对每一个方块进行处理即可。

结构体排序采用sort函数即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;


#define i64 long long


struct Block
{
    i64 x;
    i64 y;
};


Block block[501];


bool column[1000001];
i64 mod = 1000000000;


bool cmp(Block a,Block b)
{
    if(a.y!=b.y)
        return a.y<b.y;
    if(a.x!=b.x)
        return a.x<b.x;
}


i64 POW(i64 a,i64 b,i64 mod)
{
    i64 ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}


i64 getColumn(int k,int num)
{
    if(num!=0)
        return k*POW(k-1,num-1,mod)%mod;
    return 1;
}
int main()
{
    int T;
    i64 M,N,K,B,top,ans,count;
    scanf("%d",&T);
    for(int i = 1;i<=T;i++)
    {
        scanf("%lld %lld %lld %lld",&M,&N,&K,&B);
        count = N;
        ans = 1;
        memset(column,false,sizeof(column));
        for(int j = 0;j<B;j++)
        {
            scanf("%lld %lld",&block[j].x,&block[j].y);
        }
        sort(block,block+B,cmp);
        for(int j = 0;j<B;j++)
        {
            if(column[block[j].y]==false)
                {
                    count--;
                    column[block[j].y] = true;
                    top = 1;
                }
            ans = (ans * getColumn(K,block[j].x-top))%mod;
            top = block[j].x+1;
            if((j+1<B&&block[j+1].y!=block[j].y)||j+1==B)
            {
                ans = (ans * getColumn(K,M+1-top))%mod;
            }
        }
        ans = (POW(getColumn(K,M),count,mod)*ans)%mod;
        printf("Case %d: %lld\n",i,ans);
    }
    return 0;
}