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


你可能感兴趣的:(hdu 4602 Partition)