题意:给出n,k,以及n个正数,把n个数放在一个集合里,进行k次操作,每次操作取最大的数和次大的数放进集合。问k次操作结束后,集合中所有数的和。
思路:假设初始时刻,次大和最大分别为a0,a1,那么得到的就是一个类斐波那契数列。初始为a0,a1,fn = fn-1 + fn
最后求和。
利用这个性质。。。
我们直接构造完矩阵。。。求出F(n+2)即可。。
需要注意。。。矩阵中每一项是小于1E7+7的。。。矩乘的时候会爆int…
所以mat要用LL存。。我好傻啊。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
/* *********************************************** Author :111qqz Created Time :Thu 20 Oct 2016 10:40:39 AM CST File Name :code/hdu/5171.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N =1E5+7; const LL mod = 1E7+7; int n; LL k; LL a[N]; LL sum; LL a0,a1; struct Mat { LL mat[3][3]; void clear() { ms(mat,0); } Mat operator*(const Mat &b)const { Mat ans; ans.clear(); for ( int i = 0 ; i < 2 ; i++) for ( int j = 0 ; j < 2 ; j++) for ( int k = 0 ; k < 2 ; k++) ans.mat[i][j] = ( ans.mat[i][j] + mat[i][k] * b.mat[k][j] ) % mod; return ans; } void pr() { cout << mat[0][0] << ' ' << mat[0][1] << endl << mat[1][0] << ' ' << mat[1][1] << endl; } Mat operator ^ ( LL b) { Mat res; res.clear(); for ( int i = 0 ; i < 2 ; i++) res.mat[i][i] = 1; Mat a = *this; while (b>0) { if (b&1) res = res * a; b = b >> 1LL; a = a * a; } return res; } void out() { for ( int i = 0 ; i < 2 ; i++) { for ( int j = 0 ; j < 2 ; j++) { printf("%d ",mat[i][j]); } printf("\n"); } } }M,M1; int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif while (~scanf("%d%lld",&n,&k)) { sum = 0 ; for ( int i = 1 ; i <= n ; i++) { scanf("%lld\n",&a[i]); sum = (sum + a[i])%mod; } sort(a+1,a+n+1); a0 = a[n-1]; a1 = a[n]; // cout<<"a0:"<<a0<<" a1:"<<a1<<endl; M1.clear(); M1.mat[0][0] = a0; M1.mat[1][0] = a1; M.clear(); M.mat[0][1] = 1; M.mat[1][0] = 1; M.mat[1][1] = 1; Mat ans; ans.clear(); ans = (M^(k+2))*M1; sum = sum-a0-a1; printf("%lld\n",(sum + ans.mat[1][0]-a1+mod) % mod); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!