hdu 4602 Partition

找规律,分析的水题

#include<stdio.h>
typedef long long ll;
const ll MOD = 1000000007;
/*
    1 2 5 12 28 64
    n = n-k
    f(n) = 2*f(n-1) + 2^(n-2) n>=2
    f(n) = 1 n == 0
    f(n) = 2 n == 1
    
    f(n) = 2^2*f(n-2) + 2^(n-2) + 2^(n-2)
         = 2^2*(2*f(n-3) + 2^(n-4)) + 2^(n-2) + 2^(n-2) = 2^3*f(n-3) + 2^(n-2) * 3
         = 2^(n-1)*f(1) + 2^(n-2) * (n-1)
         = 2^(n-2)*(n+3)
*/
ll mPow(ll a, int n)
{
    ll res = 1, tp = a;
    while (n)
    {
        if ( n & 0x1)
            res = (res*tp)%MOD;
        tp = (tp*tp)%MOD;
        n >>= 1;
    }
    return res;
}
main()
{
    int t;
    int n, k;
    ll res, tp;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &k);
        n = n - k;
        if ( n < 0)
        {
            printf("0\n");
        }
        else if ( n < 2) 
        {
            printf("%d\n", n+1);
        }
        else
        {
           res = mPow(2, n-2);
           tp = n+3;
           res = (res*tp)%MOD;
           //printf("%lld\n", res);
           printf("%I64d\n", res);
        }
    }
    //return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值