zoj 3690(递推+矩阵优化)

其实比较简单的一道递推题。 当时我却没有什么想法真的是太失败了。。

F[X] 表示前X个人,且最后一个选的是比k大的数 的总人数

K[X] 表示前X个人,且最后一个选的是<=k 的总人数

F[X]=F[X-1]*(m-k)+K[X-1]*(m-k)

K[X]=F[X-1]*k+K[X]*(k-1)

构造出矩阵求解即可

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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

#define MOD 1000000007

typedef long long int LL;


struct node
{
    LL g[2][2];
};


int n,m,k;

node cal(node x,node y)
{
    node tmp;
    tmp.g[0][0]=0; tmp.g[0][1]=0;
    tmp.g[1][0]=0; tmp.g[1][1]=0;
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                tmp.g[i][j]+=(x.g[i][k]*y.g[k][j])%MOD;
    return tmp;
}

int main()
{
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        node tmp;
        tmp.g[0][0]=m-k; tmp.g[0][1]=k;
        tmp.g[1][0]=m-k; tmp.g[1][1]=k-1;
        node ans;
        ans.g[0][0]=1; ans.g[0][1]=0;
        ans.g[1][0]=0; ans.g[1][1]=1;
        n--;
        while(n)
        {
            if(n&1)
                ans=cal(ans,tmp);
            n>>=1;
            tmp=cal(tmp,tmp);
        }
        LL ans1=0,ans2=0;
        ans1=((ans.g[0][0]*(m-k))%MOD+(ans.g[1][0]*k)%MOD)%MOD;
        ans2= ((ans.g[0][1]*(m-k))%MOD+(ans.g[1][1]*k)%MOD)%MOD;
        printf("%lld\n",(ans1+ans2)%MOD);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/04/02/2994767.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值