hdu3658 How many words

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?
 

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;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值