HDU 4365 Palindrome graph(几何变换+快速幂)

Problem Description

In addition fond of programing, Jack also loves painting. He likes to draw many interesting graphics on the paper.
One day,Jack found a new interesting graph called Palindrome graph. No matter how many times to flip or rotate 90 degrees, the palindrome graph are always unchanged.
Jack took a paper with n*n grid and K kinds of pigments.Some of the grid has been filled with color and can not be modified.Jack want to know:how many ways can he paint a palindrome graph?

Input

There are several test cases.
For each test case,there are three integer n m k(0<n<=10000,0<=m<=2000,0<k<=1000000), indicate n*n grid and k kinds of pigments.
Then follow m lines,for each line,there are 2 integer i,j.indicated that grid(i,j) (0<=i,j<n) has been filled with color.
You can suppose that jack have at least one way to paint a palindrome graph.

Output

For each case,print a integer in a line,indicate the number of ways jack can paint. The result can be very large, so print the result modulo 100 000 007.

Sample Input

3 0 2

4 2 3

1 1

3 1

Sample Output

8

3

题目大意:(感觉题意有点不清,看了下网上的解释才搞懂) 给一个n*n大小的矩阵,有k种颜色,要求把颜色填到格子中(每格一色),使整个矩阵无论是旋转还是翻转后矩阵与操作前矩阵完全相同。

思路:分析一下矩阵的操作,旋转翻转,可以理解成上下对称,左右对称与对角线对称,满足这三个对称的条件即可,这三个对称把矩形分成了8等份(三角形),问题就转化成了求一份三角形能涂的种数,但原格子中可能已有颜色,所以需要对原有颜色的格子映射到三角形中,若三角形格数为n,已有颜色m,则可涂得方案数为k^(n-m),解决映射后用快速幂就可以了。

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int mod=1e8+7;
#define LL long long
map<pair<int,int>,int>p;
LL Pow(LL x,LL n){
    int ret=1;
    while(n){
        if(n&1)
            ret=ret*x%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return ret;
}//快速幂
int main(){
    int n,m,k,x,y;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
        p.clear();
        LL t=0,s=0;
        while(m--)
		{
            scanf("%d%d",&x,&y);
            if(x>n-1-x)  x=n-1-x; 
            if(y>n-1-y)  y=n-1-y;
            if(x>y)  swap(x,y);
            if(p[make_pair(x,y)]==0)
			{
                p[make_pair(x,y)]=1;
                t++;//已经涂过色的
            }
        }
        LL z=(n+1)/2; 
        s=(z+1)*z/2;    //三角形中的总格子数
        LL ans=Pow(k,(s-t));
        printf("%lld\n",ans);
    }
    return 0;
}

在网上还找到了一份分奇偶情况的代码(好像繁了,但值得学习):

代码如下:

#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define max(a,b)  a>b?a:b
#define min(a,b)  a<b?a:b
#define LL long long 
//#define swap(a,b) {int t=a;a=b;b=t} 
using namespace std;
using namespace __gnu_cxx;

const int MOD=1e8+7;
class Point{
    public:
    int x,y;
    bool operator==(const Point &tmp) const//重载等号变 双等 
	{
        return (x==tmp.x&&y==tmp.y);        
    }    
}p[2024];
int midx,midy;
bool cmp(const Point &a,const Point &b){
    if(a.x!=b.x)  return a.x<b.x;
    return a.y<b.y;
}
void Solve1(int x,int y ,int cnt)
{
    if(x>midx) 
    {
        x=(midx<<1)-x;//左对称 
    } 
    if(y>midy) y=(midy<<1)-y;//上对称 
    if(x>y) swap(x,y);//对角线对称 
    p[cnt].x=x; p[cnt].y=y;//对称后的点 
}//偶数情况 
void Solve2(int x,int y,int cnt)
{
    if(x>midx)
    {
        int d=x-midx-1;
        x=midx-d;
    }
    if(y>midy)
    {
        int d=y-midy-1;
        y=midy-d;
    }
    if(x>y) swap(x,y);
    p[cnt].x=x;p[cnt].y=y;    
}//奇数情况 
LL Pow(LL k,LL a)
{
    LL ans=1;
    while(k)    
    {
        if(k&1) ans=(ans*a)%MOD;
        a=(a*a)%MOD;
        k>>=1;    
    }
    return ans;
}//快速幂 
int main()
{
    int n,m,k,x,y;
    while(scanf("%d%d%d",&n,&m,&k)==3)
    {
           midx=(n+1)/2;midy=midx;
           for(int i=0;i<m;i++)
           {
                scanf("%d%d",&x,&y);
                x+=1;y+=1;
                if(n&1) Solve1(x,y,i);
                else Solve2(x,y,i);
           }
           LL L=(n+1)/2;
           LL ans=L*L/2+(L+1)/2;//八分之一 
           sort(p,p+m,cmp);//对点进行排序           
           ans-=unique(p,p+m)-p;//减去有颜色的格 //unique类属性操作 去除重复点(存的时候有颜色的点变换后会重复)           
           if(ans>0)
           {
               ans=Pow(ans,(LL)k);
               printf("%I64d\n",ans); 
           }
           else printf("0\n");    
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值