zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    太忙了
    Delphi 的接口(2) 第一个例子
    Delphi 的接口(3) 关于接口的释放
    VS.NET让我做了一场恶梦
    [推荐阅读]The Best Of .The NET 1.x Years
    向大家说声对不起
    [致歉]16:30~17:10电信网络出现问题
    服务器恢复正常
    [SharePoint]更改活动目录(AD)中用户名的问题
    [正式决定]博客园开始接受捐助
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3413020.html
Copyright © 2011-2022 走看看