↓ Skip to main content
  1. Posts/

hdu 5171 GTY's birthday gift (矩阵快速幂)

·684 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:给出n,k,以及n个正数,把n个数放在一个集合里,进行k次操作,每次操作取最大的数和次大的数放进集合。问k次操作结束后,集合中所有数的和。

思路:假设初始时刻,次大和最大分别为a0,a1,那么得到的就是一个类斐波那契数列。初始为a0,a1,fn = fn-1 + fn

最后求和。

利用这个性质。。。

我们直接构造完矩阵。。。求出F(n+2)即可。。

需要注意。。。矩阵中每一项是小于1E7+7的。。。矩乘的时候会爆int…

所以mat要用LL存。。我好傻啊。。。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :Thu 20 Oct 2016 10:40:39 AM CST
  4File Name :code/hdu/5171.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <set>
 13#include <map>
 14#include <string>
 15#include <cmath>
 16#include <cstdlib>
 17#include <ctime>
 18#define fst first
 19#define sec second
 20#define lson l,m,rt<<1
 21#define rson m+1,r,rt<<1|1
 22#define ms(a,x) memset(a,x,sizeof(a))
 23typedef long long LL;
 24#define pi pair < int ,int >
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31const int N =1E5+7;
 32const LL mod = 1E7+7;
 33int n;
 34LL k;
 35LL a[N];
 36LL sum;
 37LL a0,a1;
 38struct Mat
 39{
 40    LL mat[3][3];
 41    void clear()
 42    {
 43	ms(mat,0);
 44    }
 45    Mat operator*(const Mat &b)const
 46    {
 47	Mat ans;
 48	ans.clear();
 49	for ( int i = 0 ; i < 2 ; i++)
 50	    for ( int j = 0 ; j < 2 ; j++)
 51		for ( int k = 0 ; k < 2 ; k++)
 52		    ans.mat[i][j] = ( ans.mat[i][j] + mat[i][k] * b.mat[k][j] ) % mod;
 53	return ans;
 54    }
 55    void pr()
 56    {
 57	cout << mat[0][0] << ' ' <<  mat[0][1] << endl << mat[1][0] << ' ' << mat[1][1] << endl;
 58    }
 59    Mat operator ^ ( LL b)
 60    {
 61	Mat res;
 62	res.clear();
 63	for ( int i = 0 ; i < 2 ; i++) res.mat[i][i] = 1;
 64	Mat a = *this;
 65	while (b>0)
 66	{
 67	    if (b&1) res = res * a;
 68	    b = b >> 1LL;
 69	    a = a * a;
 70	}
 71	return res;
 72    }
 73    void out()
 74    {
 75	for ( int i = 0 ; i < 2 ; i++)
 76	{
 77	    for ( int j = 0 ; j < 2 ; j++)
 78	    {
 79		printf("%d ",mat[i][j]);
 80	    }
 81		printf("\n");
 82	}
 83    }
 84}M,M1;
 85int main()
 86{
 87	#ifndef  ONLINE_JUDGE
 88	freopen("code/in.txt","r",stdin);
 89  #endif
 90	while (~scanf("%d%lld",&n,&k))
 91	{
 92	    sum = 0 ;
 93	    for ( int i = 1 ; i <= n ; i++)
 94	    {
 95		scanf("%lld\n",&a[i]);
 96		sum = (sum + a[i])%mod;
 97	    }
 98	    sort(a+1,a+n+1);
 99	    a0 = a[n-1];
100	    a1 = a[n];
101	  //  cout<<"a0:"<<a0<<" a1:"<<a1<<endl;
102	    M1.clear();
103	    M1.mat[0][0] = a0;
104	    M1.mat[1][0] = a1;
105	    M.clear();
106	    M.mat[0][1] = 1;
107	    M.mat[1][0] = 1;
108	    M.mat[1][1] = 1;
109	    Mat ans;
110	    ans.clear();
111	    ans = (M^(k+2))*M1;
112	    sum = sum-a0-a1;
113	    printf("%lld\n",(sum + ans.mat[1][0]-a1+mod) % mod);
114    	}
115  #ifndef ONLINE_JUDGE
116  fclose(stdin);
117  #endif
118    return 0;
119}

Related

hdu 2157 How many ways?? (矩阵快速幂经典题目)

·631 words·2 mins
题意:给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值 思路: ** 把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,CA的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,我们只需要快速幂求出A^k即可。**

poj 3233 Matrix Power Series (矩阵快速幂+分治)

·619 words·2 mins
题目链接 题意: Given a n × n matrix A and a positive integer k, find the sum S = A + _A_2 + _A_3 + … + Ak. 思路: 对k进行二分。 比如,当k=6时,有: A + A^2 + A^3 + A^4 + A^5 + A^6 =(A + A^2 + A^3) + A^3*(A + A^2 + A^3) 应用这个式子后,规模k减小了一半。我们二分求出A^3后再递归地计算A + A^2 + A^3,即可得到原问题的答案。

hdu 1575 Tr A (矩阵快速幂模板题)

·417 words·1 min
http://acm.hdu.edu.cn/showproblem.php?pid=1575 题意:A为一方阵,求(A^k)73得到的矩阵的主对角线的和。 思路:矩阵快速幂。模板题。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月21日 星期日 10时28分33秒 4File Name :code/hdu/1575.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=12; 34const int MOD = 9973; 35struct Mat 36{ 37 int mat[N][N]; 38 void clear() 39 { 40 ms(mat,0); 41 } 42}A; 43int n,k; 44 45Mat operator * (Mat a,Mat b) 46{ 47 Mat c; 48 c.clear(); 49 for ( int i = 0 ; i < n ; i++) 50 for ( int j = 0 ; j < n ; j++) 51 for (int k = 0 ; k < n ; k++) 52 c.mat[i][j] =(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD; 53 54 return c; 55 56} 57Mat operator ^ (Mat a,int b) 58{ 59 Mat c; 60 for ( int i = 0 ; i < n ; i++) 61 for ( int j = 0 ; j < n ; j++ ) 62 c.mat[i][j]=(i==j); 63 while (b) 64 { 65 if (b&1) c = c * a; 66 b = b>>1; 67 a = a * a; 68 } 69 return c; 70} 71int main() 72{ 73 #ifndef ONLINE_JUDGE 74 freopen("code/in.txt","r",stdin); 75 #endif 76 77 int T; 78 scanf("%d",&T); 79 while (T--) 80 { 81 scanf("%d %d",&n,&k); 82 A.clear(); 83 for ( int i = 0 ; i < n ; i++) 84 for ( int j = 0 ; j < n; j ++) 85 scanf("%d",&A.mat[i][j]); 86 87 Mat res; 88 res.clear(); 89 res = A^k; 90 91 int ans = 0 ; 92 for ( int i = 0 ; i < n ;i++) ans = (ans +res.mat[i][i])%MOD; 93 94 printf("%d\n",ans); 95 96 } 97 98 99 #ifndef ONLINE_JUDGE 100 fclose(stdin); 101 #endif 102 return 0; 103}

codeforces #341 div 2 E. Wet Shark and Blocks (数位dp+矩阵加速)

·558 words·2 mins
http://codeforces.com/problemset/problem/621/E # 题意:有b组数,每组数均有n个且相同。你必须在每组选一个数,组成一个新数sum,使得sum % x == k,问方案数 % (1e9+7)。 思路:数位dp.首先考虑b不是很大的一般情况。dp[i][j]表示处理到前i个块的时候结果为j的方案数。那么转移方程就是:**dp[i][(j_10+t)%x] = dp[i-1][j]_cnt[t] ** cnt[i]表示数字i出现的个数。