zoukankan      html  css  js  c++  java
  • Choosing number 写出递推公式,然后用矩阵的快速幂。

    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 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
    ***********************************************************************************************************************************************************
    矩阵的快速幂dp[n][1]即取>=k的数时的方案总数,dp[n][2]即取<k时的方案总数
    dp[n][1]=dp[n-1][1]*(m-k)+dp[n-1][2]*(m-k);
    dp[n][2]=dp[n-1]*k+dp[n-1][2]*(k-1);
    列出矩阵
    : m-k m-k dp[n-1][1] dp[n][1]
    k k-1 dp[n-1][2] dp[n][2]
    可得由矩阵的快速幂快速求解
    ***********************************************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 #include<algorithm>
     7 #define LL long long
     8 #define MOD 1000000007
     9 using namespace std;
    10 LL n,m,k;
    11 LL save[5][5];
    12 LL mod(LL m,LL n)
    13 {
    14     LL sum=1;
    15     while(n)
    16     {
    17         if(n&1)
    18            sum=(sum*m)%MOD;
    19         m=(m*m)%MOD;
    20         n=n>>1;
    21     }
    22     return sum;
    23 }
    24 int main()
    25 {
    26     while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF)
    27     {
    28         if(k==0)
    29         {
    30             printf("%lld
    ",mod(m,n)%MOD);
    31             continue;
    32         }
    33         if(n==1)
    34         {
    35             printf("%lld
    ",m);
    36             continue;
    37         }
    38         save[1][1]=m-k;
    39         save[1][2]=m-k;
    40         save[2][1]=k;
    41         save[2][2]=k-1;
    42         LL ans1=m-k;
    43         LL ans2=k;
    44         n--;
    45         while(n)
    46         {
    47             if(n&1)
    48             {
    49                 LL temp1=(((ans1*save[1][1])%MOD)+((ans2*save[1][2])%MOD))%MOD;
    50                 LL temp2=(((ans1*save[2][1])%MOD)+((ans2*save[2][2])%MOD))%MOD;
    51                 ans1=temp1;
    52                 ans2=temp2;
    53             }
    54             n=n>>1;
    55             LL a1=(((save[1][1]*save[1][1])%MOD)+((save[1][2]*save[2][1])%MOD))%MOD;
    56             LL a2=(((save[1][2]*save[2][2])%MOD)+((save[1][1]*save[1][2])%MOD))%MOD;
    57             LL b1=(((save[2][2]*save[2][1])%MOD)+((save[2][1]*save[1][1])%MOD))%MOD;
    58             LL b2=(((save[2][2]*save[2][2])%MOD)+((save[2][1]*save[1][2])%MOD))%MOD;
    59             save[1][1]=a1; save[1][2]=a2;
    60             save[2][1]=b1; save[2][2]=b2;
    61 
    62         }
    63         printf("%lld
    ",(ans1+ans2)%MOD);
    64     }
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    使用元数据简化jdbc代码---查询操作(用到反射)
    DDD(领域驱动设计)总结
    关于java中BufferedReader的read()及readLine()方法的使用心得
    BufferedInputStream使用详解
    java 分次读取大文件的三种方法
    《深入理解mybatis原理》 Mybatis数据源与连接池
    《深入理解mybatis原理》 MyBatis的架构设计以及实例分析
    Mybatis源码解析优秀博文
    java 通过调用存储过程获取结果集
    httprunner学习3-extract提取token值参数关联(上个接口返回的token,传给下个接口请求参数)
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3446454.html
Copyright © 2011-2022 走看看