zoj 3690, 计数 dp , 快速幂

Choosing number

Time Limit: 2 Seconds      Memory Limit: 65536 KB

There are n people standing in a row. And There are m numbers, 1.2...m. Every one should choose a number. But if two persons standing adjacent to each other choose the same number, the number shouldn't equal or less than k. Apart from this rule, there are no more limiting conditions.

And you need to calculate how many ways they can choose the numbers obeying the rule.

Input

There are multiple test cases. Each case contain a line, containing three integer n (2 ≤ n ≤ 108), m (2 ≤ m ≤ 30000), k(0 ≤ k ≤ m).

Output

One line for each case. The number of ways module 1000000007.

Sample Input

4 4 1

Sample Output

216

Author: GU, Shenlong
Contest: ZOJ Monthly, March 2013

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MOD 1000000007

LL m,n,k;

void pp(LL t[2][2],LL p[2][2]){
    LL i,j,k;
    LL c[2][2];
    memset(c,0,sizeof(c));
    for(i=0; i<2; i++)
        for(j=0; j<2; j++)
            for(k=0; k<2; k++)
                c[i][j] =(c[i][j] + t[i][k] * p[k][j])%MOD;
    memcpy(p,c,sizeof(c));
}

LL mpow(LL f[2][2]){
    LL i,j;
    LL e[2][2]={k-1,k,m-k,m-k};
    n--;
    while(n){
        if(n&1) pp(e,f);
        n=n>>1;
        pp(e,e);
    }
    return (f[0][0] + f[1][0])%MOD;
}

int main(){
//  fstream fin("C:\\Users\\Administrator\\Desktop\\in.txt",ios::in);
  //  freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
   // LL n,m,k;
    while(scanf(" %lld %lld %lld",&n,&m,&k)==3){
        LL f[2][2]={k,0,m-k,0};
        printf("%lld\n",mpow(f));
    }
//  fin.close();
    return 0;
}

 

posted @ 2013-11-07 18:35  Ramanujan  阅读(382)  评论(0编辑  收藏  举报