1. 程式人生 > >第六屆藍橋杯——壘骰子(矩陣快速冪)

第六屆藍橋杯——壘骰子(矩陣快速冪)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 1000000007


const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[M][M];
int pa[M*10],rankk[M];
int head[M*6],vis[M*10];
int dis[M][10];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
vector<int> g[M*10];
int dp[M];
int sums[M*10];
int has[105000];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};


void getpri()
{
    ll i;
    int j;
    cnt=0;
    memset(isprime,false,sizeof(isprime));
    for(i=2; i<1000000LL; i++)
    {
        if(!isprime[i])prime[cnt++]=i;
        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
        {
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
ll qk_mul(ll a,ll b)
{
    ll t=0;
    while(b)
    {
        if(b&1)
            t=(t+a)%mod;
        a=(a<<1)%mod;
        b>>=1;
    }
    t%=mod;
    return t;
}
ll qk_mod(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=qk_mul(ans,a);
        a=qk_mul(a,a);
        b>>=1;
    }
    ans%=mod;
    return ans;
}
int gcd(int a,int b)
{
    return b == 0 ? a : gcd(b, a%b);
}
int lcm(int a,int b)
{
    return a*b/gcd(a,b);
}
int findx(int t)
{
    if(t!=pa[t])
        pa[t]=findx(pa[t]);
    return pa[t];
}
void unionx(int x,int y)
{
    x=findx(x);
    y=findx(y);
    if(x!=y)
    {
        pa[y]=x;
    }
}


void init()
{
    for(int i=0; i<101; i++)
        pa[i]=i;
}
int heap[100005];
void push(int x)
{
    int i=++sz;
    while(i>1)  //i>0
    {
        int p=i/2; //(i-1)/2
        if(heap[p]<=x)break;
        heap[i]=heap[p];
        i=p;
    }
    heap[i]=x;
    /* a[++sz] = x;
     int t = a[sz];
     int tson = sz;
     while( (tson > 1)&&( a[tson/2] > t))
     {
         a[tson] = a[tson/2];
         tson = tson/2;
     }
     a[tson] = x;
    */
}
int pop()
{
    int ret=heap[1];//int ret=a[0]
    int x=heap[sz--];//a[--sz]
    int i=1;// i=0
    while(2*i<sz) //2*i+1
    {
        int a=i*2,b=i*2+1;
        if(b<sz&&heap[b]<heap[a])a=b;
        if(heap[a]>=x)break;
        heap[i]=heap[a];
        i=a;
    }
    heap[i]=x;
    return ret;
}
int lowbit(int i)
{
    return i&(-i);
}
void add(int i,int v)//修改值並向父節點修改
{
    while(i<=M)
    {
        sums[i]+=v; //c[i]開始都是0,每經過一個數,它的c[i]+1表示c[i]
        i+=lowbit(i);
    }
}
int sum(int i)//求和
{
    ll s=0;
    while(i>=1)
    {
        s+=sums[i];
        i-=lowbit(i);
    }
    return s;
}


char pre[7][7]=
{
    {'>','>','<','<','<','>','>'},
    {'>','>','<','<','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'<','<','<','<','<','=','0'},
    {'>','>','>','>','0','>','>'},
    {'<','<','<','<','<','0','='}
};
struct Mat
{
    ll mat[6][6];
    Mat(int x)
    {
        memset(mat,0,sizeof(mat));
        for(int i=0; i<6; i++)mat[i][i]=x;
    }
};
Mat operator*(const Mat& a,const Mat& b)
{
    Mat c(0);
    int i,j,k;
    for(i=0; i<6; i++)
        for(j=0; j<6; j++)
            for(k=0; k<6; k++)
            {
                c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
            }
    return c;
}
Mat qmod(Mat x,int b)
{
    Mat res(1);
    while(b)
    {
        if(b&1)res=res*x;
        x=x*x;
        b>>=1;
    }
    return res;
}
ll qmod2(ll x,int b)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*x%mod;
        x=x*x%mod;
        b>>=1;
    }
    return res;
}
int main()
{
    int i,j,t,u,v;
    Mat ans(1);
    scanf("%d%d",&n,&m);
    for(i=0;i<6;i++)
        for(j=0;j<6;j++)
           ans.mat[i][j]=1;
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&u,&v);
        ans.mat[u-1][(v+2)%6]=0;
        ans.mat[v-1][(u+2)%6]=0;
    }
    Mat res=qmod(ans,n-1);//注意冪的值!!
    ll op=0;
       for(i=0;i<6;i++)
        for(j=0;j<6;j++)
            op=(op+res.mat[i][j])%mod;
    op=(op*qmod2(4,n))%mod;
    printf("%I64d\n",op);
    return 0;
}