zoukankan      html  css  js  c++  java
  • hdu 5171(矩阵快速幂,递推)

    GTY's birthday gift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1286    Accepted Submission(s): 502


    Problem Description
    FFZ's birthday is coming. GTY wants to give a gift to ZZF. He asked his gay friends what he should give to ZZF. One of them said, 'Nothing is more interesting than a number multiset.' So GTY decided to make a multiset for ZZF. Multiset can contain elements with same values. Because GTY wants to finish the gift as soon as possible, he will use JURUO magic. It allows him to choose two numbers a and b(a,bS), and add a+b to the multiset. GTY can use the magic for k times, and he wants the sum of the multiset is maximum, because the larger the sum is, the happier FFZ will be. You need to help him calculate the maximum sum of the multiset.
     
    Input
    Multi test cases (about 3) . The first line contains two integers n and k (2n100000,1k1000000000). The second line contains n elements ai (1ai100000)separated by spaces , indicating the multiset S .
     
    Output
    For each case , print the maximum sum of the multiset (mod 10000007).
     
    Sample Input
    3 2 3 6 2
     
    Sample Output
    35
     
    Source
     
    题意:给出一个集合s,集合中的有个初始元素集合,现在的规则是从这些元素中选出两个最大的分别为 a,b,将 a+b 重新添加进集合,然后依次操作 k 次,问最终的元素集合的和。
    例如 : 3 6 2 --> 9 6 3 2 --> 15 9 6 3 2 操作两次之后结果为 35
    题解:从题目中我们可以知道每次选出的两个数构成了斐波拉契数列,得到递推式 f[i] = f[i-1]+f[i-2],然后我们可以看出第i步求出的和为 sum[i] = sum[i-1]+f[i+1] = sum[i]+f[i]+f[i-1] 我们要求的最终结果是sum[k] 所以我们可以构造矩阵来做.
    构造矩阵的方法如下(图丑死了。。。别怪我):
    解出 a - j 即可。
    (sum[i],f[i+1],f[i]) = ((1,1,1),(0,1,1),(0,1,0))*(sum[i-1],f[i],f[i-1])
    所以此递推式的特征矩阵为 M = ((1,1,1),(0,1,1),(0,1,0))
    然后求出 A = M^k
    最终结果为 A[0][0]*sum[1]+A[0][1]*f[2]+A[0][2]*f[1]
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    const LL mod = 10000007;
    const int N = 100005;
    struct Matrix
    {
        LL v[3][3];
        Matrix(){memset(v,0,sizeof(v));}
    }ori;
    LL n,k,a[N];
    Matrix mult(Matrix a,Matrix b){
        Matrix temp;
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    temp.v[i][j] = (temp.v[i][j]+(a.v[i][k]*b.v[k][j])%mod)%mod;
                }
            }
        }
        return temp;
    }
    Matrix pow_mod(Matrix a,LL n){
        Matrix ans;
        for(int i=0;i<3;i++){
            ans.v[i][i] = 1;
        }
        while(n){
            if(n&1) ans = mult(ans,a);
            a = mult(a,a);
            n>>=1;
        }
        return ans;
    }
    int main()
    {
        ori.v[0][0]=1,ori.v[0][1]=1,ori.v[0][2]=1;
        ori.v[1][0]=0,ori.v[1][1]=1,ori.v[1][2]=1;
        ori.v[2][0]=0,ori.v[2][1]=1,ori.v[2][2]=0;
        while(scanf("%lld%lld",&n,&k)!=EOF)
        {
            LL max1=-1,max2=-1,sum=0;
            for(int i=1; i<=n; i++)
            {
                scanf("%lld",&a[i]);
                if(a[i]>max1){
                    max2 = max1;
                    max1 = a[i];
                }else if(a[i]>max2&&a[i]<=max1){
                    max2 = a[i];
                }
                sum+=a[i];
            }
          //  printf("%lld %lld %lld
    ",sum,max1,max2);
            Matrix A = pow_mod(ori,k);
            LL ans = (A.v[0][0]*sum%mod+A.v[0][1]*max1%mod+A.v[0][2]*max2%mod)%mod;
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    网站首页蒙灰CSS样式
    MATLAB数值计算编程题
    通过P/Invoke控制继电器
    Java实习生入职测试
    navicat for mongodb12破解
    用svg绘制圣诞帽
    Excel条件格式
    10道算法题
    从事软件开发工作10年后的总结
    ASP.NET Core MVC+EF Core从开发到部署
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5682655.html
Copyright © 2011-2022 走看看