杭电acm 3658How many words

How many words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 496    Accepted Submission(s): 325


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

题意:

给出一个序列的长度;

这个序列只能有A-Z,a-z;

而且要求相邻的字母asiic 码差值小于等于32;而且必须有一个是等于32的;

问有种排列;


思路:

构造一个52  * 52的矩阵,把每个字母后面能跟哪些标为1;

然后矩阵快速幂;

然后再把差值为32的标志为0,在算一次(这样算出来的就是肯定不会有差值等于32的)

两次结果相减;

代码:

#include<cstdio>
#include<cstring>
#define ll long long
struct mat
{
    ll g[60][60];
}res, ori;
const ll MOD = 1e9 + 7;
ll n;
void init()
{
    memset(ori.g ,0, sizeof(ori.g));
    memset(res.g ,0, sizeof(res.g));
    for(int i = 0; i < 26; i++)//小写
    {
        for(int j = 0; j <= i + 26; j++)
        {
            ori.g[j][i] = 1;
        }
    }
    for(int i = 26; i < 52; i++)//大写
    {
        for(int j = 51; j >= i - 26 ; j--)
        {
            ori.g[j][i] = 1;
        }
    }
    for(int i = 0; i < 52; i++)
    {
        res.g[0][i] = 1;
    }
}
mat mul(mat a, mat b)
{
    mat tmp;
    memset(tmp.g, 0, sizeof(tmp.g));
    for(int i = 0; i < 52; i++)
    {
        for(int j = 0; j < 52; j++)
        {
            for(int k = 0; k < 52; k++)
            {
                tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j])% MOD;
            }
        }
    }
    return tmp;
}
ll cul(ll k)
{
    while(k)
    {
        if(k & 1)
            res = mul(res, ori);
        k >>= 1;
        ori = mul(ori, ori);
    }
    ll ans = 0;
    for(int i = 0; i < 52; i++)
    {
        ans = (ans + res.g[0][i]) % MOD;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        init();
        ll k1 = cul(n - 1);
        init();
        for(int i = 0; i < 26; i++)
        {
            ori.g[i + 26][i] = 0;
        }
        for(int i = 26; i < 52; i++)
        {
            ori.g[i - 26][i] = 0;
        }
        ll k2 = cul(n - 1);
        printf("%lld\n",(k1  + MOD- k2) % MOD);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值