HDU-4365 Palindrome graph

本文介绍了一种利用快速幂算法解决特定点集覆盖问题的方法,通过将点集转换到左上角并计算覆盖数量,最终使用快速幂得出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先将没有特殊点的所有的情况都计算出来,再将给定的点都计算到左上角的标记点,最后查看有多少个点已经被覆盖了,减去该部分,最后用快速幂输出结果,注意这里要用long long。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
#include<string>
#define MOD 100000007
#define LL long long
using namespace std;

int N, M, K;

struct Node
{
    int x, y;
    bool operator < (Node temp) const
    {
        if (x != temp.x) return x < temp.x;
        else return y < temp.y;
    }
    bool operator == (Node temp) const
    {
        return x == temp.x && y == temp.y;
    }
}seq[2005];

int Cal(int x)
{
    if (x <= 0) return 0;
    if (x & 1) {
        return (x + 1) / 2 + Cal(x - 2);
    }
    else {
        return x / 2 + Cal(x - 2);    
    }
}

void init(int &x, int &y)
{
    if (x > N/2) {
        x = N - x + 1;
    }
    if (y > N/2) {
        y = N - y + 1;
    }
    if (x > y) {
        int t = x;
        x = y, y = t;
    }
}

int _pow(LL a, int b)
{
    LL ret = 1;
    while (b) {
        if (b & 1) {
            ret *= a;
            ret %= MOD;
        }
        a *= a;
        a %= MOD;
        b >>= 1;
    }
    return ret;
}

int main()
{ 
    int ret;
    while (scanf("%d %d %d", &N, &M, &K) == 3) {
        ret = Cal(N);
        for (int i = 1; i <= M; ++i) {
            scanf("%d %d", &seq[i].x, &seq[i].y);
            seq[i].x += 1, seq[i].y += 1;
            init(seq[i].x, seq[i].y);
        }
        sort(seq+1, seq+M+1);
        ret -= unique(seq+1, seq+M+1) - (seq+1);
        printf("%d\n", _pow(K, ret));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值