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.
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.
Print a single integer — the sought number modulo 1000000007 (109 + 7).
3 3 2
ab
ba
17
3 3 0
27
2 1 1
aa
0
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种字母顺序每种情况都不得出现在字符串中即给出的k种字母顺序不能相邻 比如给出了ab则a和b不能相邻但ba可以。可以通过一个记录数组 zh[][]来表示1就可以0不可以相邻
这个题不太好看出来是一道矩阵快速幂问题。首先我们用一个二维数组sum[i][j]表示长度为i,以j为结尾的字符串的种数。那么sum[i][j]=Esum[i-1][k](1<=k<=m)如果zh[j][k]==1的话就加进去。
这样就类似斐波那契他也有一个关系矩阵就是zh然后通过递推求出他的n-1次方那么这个矩阵所有元素的和就是长度为n的字符串的种数。
#include <iostream>
#include <cmath>
#include<algorithm>
#include<stack>
#include<string.h>
#include<cstdio>
#include<set>
using namespace std;
//int maxx,minn,j,k,a[100000];
//int n,x[100009],ans=0,a[100009],y[100009],l[100090],r[100090];
bool v[6];
int a[100009][6],maxx=-1,t;
long long m=1e9+7;
struct pi
{
long long ah[60][60];
};
pi multi(pi x,pi y)
{
pi z;
for(int i=1;i<=t;i++)
{
for(int j=1;j<=t;j++)
{
z.ah[i][j]=0;
for(int k=1;k<=t;k++)
z.ah[i][j]=(z.ah[i][j]%m+x.ah[i][k]%m*y.ah[k][j]%m)%m;
z.ah[i][j]%=m;
}
}
return z;
}
pi quimul(pi x,long long int n)
{
pi an;
for(int i=1;i<=t;i++)
an.ah[i][i]=1;
for(;n;n>>=1)
{
if(n&1)
an=multi(an,x);
x=multi(x,x);
}
return an;
}
int main()
{
long long n;
int x,v,p[600],zh[90][90];
char c='a',u;
for(int i=1;i<=52;i++)
for(int j=1;j<=52;j++)
zh[i][j]=1;//初始化关系矩阵
for(int i=1;i<=26;i++)//把字母和数字联系起来
p[(int)c+i-1]=i;
c='A';
for(int i=27;i<=52;i++)
p[(int)c+i-27]=i;
cin>>n>>t>>v;
for(int i=0;i<v;i++)
{
scanf(" %c %c",&c,&u);
zh[p[(int)c]][p[(int)u]]=0;//构建关系矩阵
}
pi ans;
for(int i=1;i<=t;i++)
for(int j=1;j<=t;j++)
ans.ah[i][j]=zh[i][j];
ans=quimul(ans,n-1);
long long int pp=0;
for(int i=1;i<=t;i++)
for(int j=1;j<=t;j++)
pp=(pp+ans.ah[i][j])%m;
cout<<(pp+m)%m<<endl;
}