zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    格式化Format使用
    ASP.NET几种清除页面缓存的方法
    repeater中分页aspnetpager是遇到的问题
    利用正则表达式去掉html代码
    ASP.NET母版页中调用内容页的方法和web用户控件调用.aspx页面里的方法
    PHP常用代码大全
    用 JA Transmenu 模块做多级弹出菜单
    简单的 "改变" joomla 后台administrator目录
    数据库设计中的14个技巧
    Joomla模板制作教程(转)
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/2994767.html
Copyright © 2011-2022 走看看