bestcoder#29 1002 矩阵连乘快速幂解fib数列

bestcoder#29 1002 矩阵连乘快速幂解fib数列

GTY's birthday gift

Accepts: 103
Submissions: 585
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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

题意:给定一个集合,每次从集合中找出两个元素a,b,将a+b加进集合,求重复k次之后元素的最大和
思路:
显然每次会从可重集中选择最大的两个进行操作,设这两数为a,b(a>=b),操作之后的数一定是操作后集合中最大的,下一次选取的数一定是a+ba,这就形成了一个类似于斐波那契数列的东西,矩阵乘法快速幂求前n项和即可,转移矩阵如下
Sn    1 1 1  Sn-1
Fn  =  0 1 1 * Fn-1
Fn-1   0 1 0  Fn-2
推导时先填上前后项,中间的01矩阵直接配即可
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>



using namespace std;



const int maxn=1000100;

const int MOD=10000007;



long long n,k;

long long fib[maxn];



int main()

{

    while(scanf("%lld%lld",&n,&k)!=EOF){

        long long a,b,sum=0;

        for(int i=0;i<n;i++){

            scanf("%lld",&fib[i]);

            sum+=fib[i];

        }

        sort(fib,fib+n);

        a=fib[n-1],b=fib[n-2];

        long long res[3][3]={{1,0,0},{0,1,0},{0,0,1}};

        long long A[3][3]={{1,1,1},{0,1,1},{0,1,0}};

        while(k){

            long long s[3][3];

            if(k&1){

                memset(s,0,sizeof(s));

                for(int i=0;i<3;i++){

                    for(int j=0;j<3;j++){

                        for(int k=0;k<3;k++){

                            s[i][j]+=res[i][k]*A[k][j];

                            s[i][j]%=MOD;

                        }

                    }

                }

                memcpy(res,s,sizeof(s));

            }

            memset(s,0,sizeof(s));

            for(int i=0;i<3;i++){

                for(int j=0;j<3;j++){

                    for(int k=0;k<3;k++){

                        s[i][j]+=A[i][k]*A[k][j];

                        s[i][j]%=MOD;

                    }

                }

            }

            memcpy(A,s,sizeof(s));

            k>>=1;

        }

        long long ans=res[0][0]*(a+b)+res[0][1]*a+res[0][2]*b;

        ans+=sum-a-b;

        cout<<ans%MOD<<endl;

    }

    return 0;

}
View Code

 




你可能感兴趣的:(code)