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;
    }
  • 相关阅读:
    习题8-2 在数组中查找指定元素 (15分)
    习题8-1 拆分实数的整数与小数部分 (15分)
    练习8-8 移动字母 (10分)
    练习8-2 计算两数的和与差 (10分)
    习题6-6 使用函数输出一个整数的逆序数 (20分)
    狼人杀心得
    PHP配置文件详解
    15个实用的PHP正则表达式
    浅谈 PHP 中的多种加密技术及代码示例
    每个程序员都应该知道的 16个最佳 PHP 库
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/2994767.html
Copyright © 2011-2022 走看看