CodeForces - 222E Decoding Genome

Description:

Recently a top secret mission to Mars has taken place. As a result, scientists managed to obtain some information about the Martian DNA. Now we know that any Martian DNA contains at most m different nucleotides, numbered from 1 to m. Special characteristics of the Martian DNA prevent some nucleotide pairs from following consecutively in this chain. For example, if the nucleotide 1 and nucleotide 2 can not follow consecutively in the Martian DNA, then the chain of nucleotides [1, 2] is not a valid chain of Martian DNA, but the chain of nucleotides [2, 1] can be a valid chain (if there is no corresponding restriction). The number of nucleotide pairs that can't follow in the DNA chain consecutively, is k.

The needs of gene research required information about the quantity of correct n-long chains of the Martian DNA. Your task is to write a program that will calculate this value.

Input

The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 1015, 1 ≤ m ≤ 52, 0 ≤ k ≤ m2).

Next k lines contain two characters each, without a space between them, representing a forbidden nucleotide pair. The first character represents the first nucleotide in the forbidden pair, the second character represents the second nucleotide.

The nucleotides with assigned numbers from 1 to 26 are represented by English alphabet letters from "a" to "z" (1 is an "a", 2 is a "b", ..., 26 is a "z"). Nucleotides with assigned numbers from 27 to 52 are represented by English alphabet letters from "A" to "Z" (27 is an "A", 28 is a "B", ..., 52 is a "Z").

It is guaranteed that each forbidden pair occurs at most once in the input. It is guaranteed that nucleotide's numbers in all forbidden pairs cannot be more than m. Note that order is important in nucleotide pairs.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Output

Print a single integer — the sought number modulo 1000000007 (109 + 7).

Examples

Input

3 3 2
ab
ba

Output

17

Input

3 3 0

Output

27

Input

2 1 1
aa

Output

0

Note

In the second test case all possible three-nucleotide DNAs are permitted. Each nucleotide can take one of three values, thus in total there are 27 distinct three nucleotide DNAs.

In the third test sample we cannot make any DNA of two nucleotides — the only possible nucleotide "a" cannot occur two times consecutively.

题意:给出n个位置m个字母,k个限制,每个限制是这两个字母不能按照这个顺序相连。

这道题很好理解,没限制所有的情况就是m^n,加上限制就是k*限制可以占位置的情况-重复的。

难点就在于数据大,需要高精度。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<stack>
#include<vector>
#include<math.h>
const int INF = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef double ld;
int i,j,k;
using namespace std;
#define N 100
#define MOD 1000000007
long long n;
int m;
int k;
int indice(char x)
{
    if(x>='a' && x<='z') return x-'a';
    else return x-'A'+26;
}//预处理字符转数字

long long c[N][N];
void cheng(long long a[][N],long long b[][N])
{
    memset(c,0,sizeof(c));
    for(int i=0; i<m; i++)
        for(int j=0; j<m; j++)
            for(int k=0; k<m; k++)
                c[i][j]=(c[i][j]+a[i][k]*b[k][j])%MOD;
    memcpy(a,c,sizeof(c));
}

long long res[N][N];
long long odd[N][N];

void pow(long long p)
{
    memset(odd,0,sizeof(odd));
    for(int i=0; i<m; i++)
        odd[i][i]=1;
    if(p==0)
    {
        memcpy(res,odd,sizeof(odd));
        return;
    }
    while(p>1)
    {
        if(p&1) 
            cheng(odd,res);
        cheng(res,res);
        p/=2;
    }
    cheng(res,odd);
}

string s;
int main()
{
    cin>>n>>m>>k;
    for(int i=0; i<m; i++)
        for(int j=0; j<m; j++)
            res[i][j]=1;
    for(int i=0; i<k; i++)
    {
        cin>>s;
        res[indice(s[0])][indice(s[1])]=0;
    }
    n--;
    pow(n);
    long long cont=0;
    for(int i=0; i<m; i++)
        for(int j=0; j<m; j++)
            cont=(cont+res[i][j])%MOD;
    cout<<cont<<endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值