How many words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 259 Accepted Submission(s): 128
Problem Description
In order to make a new word, we will pick out m letters from all the upper case letters and lower case letters(from `a' to `Z'). Therefore, that means you can pick some same letters. But here are two rules:
● as to all the neighbour letters, the absolute value of their ASCII code must be not greater than 32.
● there must be at least one pair of neighbour letters whose absolute value of ASCII code is exactly equal to 32. For example, considering the word in the form like "Xx" or "xX", the neighbour letters have an absolute value of ASCII code exactly equal to 32.
Now how many di erent words can we get?
● as to all the neighbour letters, the absolute value of their ASCII code must be not greater than 32.
● there must be at least one pair of neighbour letters whose absolute value of ASCII code is exactly equal to 32. For example, considering the word in the form like "Xx" or "xX", the neighbour letters have an absolute value of ASCII code exactly equal to 32.
Now how many di erent words can we get?
Input
The first line of input is the number of test case. For each test case, there is only one line contains one integer m(2 ≤ m ≤ 10
9).
Output
For each test case output one line, just the answer mod 1000000007.
Sample Input
4 2 3 100 7926778
Sample Output
52 4056 533550434 773908369
Author
windy7926778
Source
Recommend
lcy
简单的矩阵快速幂。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MOD 1000000007
typedef struct
{
long long a[55][55];
}Matrix;
Matrix Mul(Matrix x,Matrix y)
{
int i,j,k;
Matrix tag;
for (i=0;i<52;i++)
{
for (j=0;j<52;j++)
{
tag.a[i][j]=0;
for (k=0;k<52;k++)
{
tag.a[i][j]+=x.a[i][k]*y.a[k][j];
tag.a[i][j]%=MOD;
}
}
}
return tag;
}
Matrix a,b;
int main()
{
int i,j,n,T,p;
long long ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for (i=0;i<52;i++)
{
for (j=0;j<52;j++)
{
a.a[i][j]=(abs(i-j)<=26);
b.a[i][j]=0;
}
b.a[i][0]=1;
}
p=n;
n--;
while(n)
{
if (n&1) b=Mul(a,b);
a=Mul(a,a);
n>>=1;
}
ans=0;
for (i=0;i<52;i++)
{
ans+=b.a[i][0];
ans%=MOD;
}
for (i=0;i<52;i++)
{
for (j=0;j<52;j++)
{
a.a[i][j]=(abs(i-j)<26);
b.a[i][j]=0;
}
b.a[i][0]=1;
}
n=p;
n--;
while(n)
{
if (n&1) b=Mul(a,b);
a=Mul(a,a);
n>>=1;
}
for (i=0;i<52;i++)
{
ans+=MOD-b.a[i][0];
ans%=MOD;
}
printf("%I64d\n",ans);
}
return 0;
}