zoj3690 Choosing number 矩阵

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

#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
const ll mod=1000000007;

struct matrix{
    ll f[2][2];
};

matrix mul(matrix a,matrix b){
    matrix s;
    memset(s.f,0,sizeof s.f);
    ll i,j,k;
    for(i=0;i<2;i++)
        for(j=0;j<2;j++)
    for(k=0;k<2;k++){
        s.f[i][j]+=a.f[i][k]*b.f[k][j];
        s.f[i][j]%=mod;
    }
    return s;
}

matrix pow_mod(matrix a,ll k){
    matrix s;
    s.f[0][0]=s.f[1][1]=1;
    s.f[0][1]=s.f[1][0]=0;
    while(k){
        if(k&1)   s=mul(s,a);
        a=mul(a,a);
        k>>=1;
    }
    return s;
}

int main(){
    ll n,m,k;
    while(cin>>n>>m>>k){
        matrix e;
        e.f[0][0]=e.f[1][0]=m-k;
        e.f[0][1]=k;
        e.f[1][1]=k-1;

        e=pow_mod(e,n-1);
        ll ans=((m-k)*e.f[0][0]+k*e.f[1][0]+(m-k)*e.f[0][1]+k*e.f[1][1])%mod;
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-07-29 14:31:08

zoj3690 Choosing number 矩阵的相关文章

ZOJ - 3690 Choosing number 矩阵快速幂

题目大意:有n个人排成一行,有m个数字,每个人可以选择1 – m的任一个数字,但有一个限制,如果相邻的两个人选择相同的数字的话,这个数字必须大于k 问有多少种选择方法 解题思路:变化矩阵为(m-k, k, m - k, k - 1),按行的写 设前一个数为j 如果j大于k的话,那么j后面可以跟上任一个数 如果j小于等于k,那么j后面只能跟上不等于k的数 如果有p种情况为前一个数大于k的,q种情况为前一个数小于等于k的,由上面可得,当前这个数大于k的情况有 p * (n -k) + q * (n

ZOJ 3690 Choosing number(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

ZOJ 3690 Choosing number(矩阵快速幂)

题目地址:ZOJ 3690 假设F(n)表示前n个人第n个人选择的数大于k的个数,G(n)表示的是前n个人第n个人选择的数小于等于k的个数 那么F(n) = F(n-1)*(m-k)+G(n-1)*(m-k) , G(n) = F(n-1)*k+G(n-1)*(k-1) , 那么最后的结果就是F(n)+G(n); 那么我们可以构造出矩阵 | m-k m-k|   | F(n-1) |       | F(n) | | k      k-1| * | G(n-1) | => | G(n) | 那么

HDU 6198 number number number 矩阵快速幂 找规律

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6198 题目描述: 给你一个k, 你可以用K个斐波那契数列中的数来构造一个数, 现在我们要求构造不出来的那个最小的数字 解题思路: 首先我们把斐波那契数列写出来, 0, 1, 1, 2, 3, 5, 8, 13, 21, 43 . . . . . . , 我们首先发现当K == 1 的时候构造不出来的数显然是4, 然后2个的时候是12, 三个是33, 然后找规律就是 f(2*n+3)-1 .....

zoj3690--Choosing number(dp,矩阵快速幂)

Choosing number Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description 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

机器学习笔记(Washington University)- Clustering Specialization-week six

1. Hierarchical clustering Avoid choosing number of clusters beforehand Dendrograms help visualize different clustering granularities (no need to rerun algorithm) Most algorithm allow user to choose any distance metric (k-means restricted us to eucli

来自 Google 的 R 语言编码风格指南

本文转自Xiao Nan的博客 R语言是一门主要用于统计计算和绘图的高级编程语言. 这份 R 语言编码风格指南旨在让我们的 R 代码更容易阅读.分享和检查. 以下规则系与 Google 的 R 用户群体协同设计而成. 概要: R编码风格约定 文件命名: 以 .R (大写) 结尾 标识符命名: variable.name, FunctionName, kConstantName 单行长度: 不超过 80 个字符 缩进: 两个空格, 不使用制表符 空白 花括号: 前括号不折行写, 后括号独占一行 赋

Number Sequence(快速幂矩阵)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 131753    Accepted Submission(s): 31988 Problem Description A number sequence

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #