• 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
    ",mpow(f));
        }
    //  fin.close();
        return 0;
    }
    
  • 相关阅读:
    HTML5学习笔记简明版(1):HTML5介绍与语法
    用margin还是用padding(1)——W3School CSS外边距合并
    Minimum Depth of Binary Tree
    118. Pascal's Triangle
    Convert Sorted Array to Binary Search Tree
    112. Path Sum
    Balanced Binary Tree
    centos 7下nginx搭建流媒体服务器【动态添加模块】
    Java内存泄漏
    Quartz的job中注入的services接口为空的解决办法
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3413020.html
Copyright © 2020-2023  润新知