hdu-4602-Partition(矩阵快速幂)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4602

Partition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3371    Accepted Submission(s): 1301


Problem Description
Define f(n) as the number of ways to perform n in format of the sum of some positive integers. For instance, when n=4, we have
  4=1+1+1+1
  4=1+1+2
  4=1+2+1
  4=2+1+1
  4=1+3
  4=2+2
  4=3+1
  4=4
totally 8 ways. Actually, we will have f(n)=2 (n-1) after observations.
Given a pair of integers n and k, your task is to figure out how many times that the integer k occurs in such 2 (n-1) ways. In the example above, number 1 occurs for 12 times, while number 4 only occurs once.
 

Input
The first line contains a single integer T(1≤T≤10000), indicating the number of test cases.
Each test case contains two integers n and k(1≤n,k≤10 9).
 

Output
Output the required answer modulo 10 9+7 for each test case, one per line.
 

Sample Input
  
  
2 4 2 5 5
 

Sample Output
  
  
5 1
题解:

最近在做矩阵快速幂的题,这个题也推出了序列是1,2,5,12,28,64.....但可惜的是没有推出它的递推公式,看网上的解法有用公式的有找规律的,在这里就说一下矩阵快速幂吧。

先给出递推公式f(N)=2*f(N-1)+2^(N-3);这里的N是根据题目中的n和k得出来的。n和序列的对应是:a[n]=1,a[n-1]=2,a[n-3]=5,a[n-4]=12,a[n-5]=28  ......。那么在n中的k出现的次数是多少呢?那就要看看k是n减几了。所以这里的N=n-k;

然后就是构造矩阵了:

【f(n),2^(N-2)】=【f(n-1),2^(N-3)】*【(2,1) (0,2)】

这里求N的时候到了N-3的值,所以N要从3开始。接下来就是利用快速幂求就行了。关于矩阵快速幂不会自行解决吧。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
struct Matrix
{
    LL a[2][2];//矩阵大小根据需求修改
    Matrix()
    {
        memset(a,0,sizeof(a));
    }
    void init()
    {
        for(int i=0; i<2; i++)
            for(int j=0; j<2; j++)
                a[i][j]=(i==j);
    }
    Matrix operator + (const Matrix &B)const
    {
        Matrix C;
        for(int i=0; i<2; i++)
            for(int j=0; j<2; j++)
                C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
        return C;
    }
    Matrix operator * (const Matrix &B)const
    {
        Matrix C;
        for(int i=0; i<2; i++)
            for(int k=0; k<2; k++)
                for(int j=0; j<2; j++)
                    C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%MOD;
        return C;
    }
    Matrix operator ^ (const LL &t)const
    {
        Matrix A=(*this),res;
        res.init();  ///矩阵的单位矩阵初始化
        LL p=t;
        while(p)
        {
            if(p&1)res=res*A;
            A=A*A;
            p>>=1;
        }
        return res;
    }
}base,ans;
LL q_pow(LL a,LL b)
{
    LL res=1;
    while(b)
    {
        if(b&1)res=res*a%MOD;
        a=a*a%MOD;
        b=b>>1;
    }
    return res;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        LL n,k;
        scanf("%lld%lld",&n,&k);
        if(k>n||k<=0)
        {
            printf("0\n");
            continue;
        }
        if(n-k+1==1)
        {
            printf("1\n");
            continue;
        }
        if(n-k+1==2)
        {
            printf("2\n");
            continue;
        }
        if(n-k+1==3)
        {
            printf("5\n");
            continue;
        }
        n=n-k;
        base.a[0][0]=2;base.a[0][1]=1;
        base.a[1][0]=0;base.a[1][1]=2;
        ans=base^(n-1);
        cout<<(2*ans.a[0][0]+ans.a[0][1])%MOD<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值