UVA - 10870 Recurrences

题目链接

给定递推式f(n)=a_{1}f(n-1)+a_{2}f(n-2)+...+a_{d}f(n-d),也给出系数数组和递推式前d项,直接套矩阵快速幂模板即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll N,mod;

struct matrix{
	ll x[20][20];
};

matrix multi(matrix a,matrix b){
	matrix tmp;
	memset(tmp.x,0,sizeof(tmp.x));
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			for(int k=0;k<N;k++){
				tmp.x[i][j]=(tmp.x[i][j]+a.x[i][k]*b.x[k][j]%mod)%mod;
			}			
	return tmp;
}

matrix quick_multi(matrix a,int n){
	matrix tmp;
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			tmp.x[i][j]=(i==j);
			
	while(n){
		if(n&1) tmp=multi(tmp,a);
		a=multi(a,a);
		n>>=1;
	}
	return tmp;
}

int main(){
	ll d,n,m;
	ll a[20],f[20];
	while(~scanf("%lld%lld%lld",&d,&n,&m)){
		if(!d&&!n&&!m) break;
		N=d,mod=m;
		for(int i=0;i<d;i++) scanf("%lld",&a[i]);
		for(int i=0;i<d;i++) scanf("%lld",&f[i]);
		matrix A;
		memset(A.x,0,sizeof(A.x));
		for(int i=0;i<d;i++){
			A.x[i][0]=a[i];
			A.x[i][i+1]=1;
		} 
		
		matrix ans;
		memset(ans.x,0,sizeof(ans.x));
		for(int i=0;i<d;i++) ans.x[0][i]=f[d-1-i];
		
		if(n==1){
			printf("%d\n",f[0]);
			continue;
		}
		A=quick_multi(A,n-d);
		ans=multi(ans,A);
		printf("%lld\n",ans.x[0][0]%mod);	
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容介绍: Editorial Reviews Review "In light of the explosive growth in the amount of data and the diversity of computing applications, efficient algorithms are needed now more than ever. This beautifully written, thoughtfully organized book is the definitive introductory book on the design and analysis of algorithms. The first half offers an effective method to teach and study algorithms; the second half then engages more advanced readers and curious students with compelling material on both the possibilities and the challenges in this fascinating field." —Shang-Hua Teng, University of Southern California "Introduction to Algorithms, the 'bible' of the field, is a comprehensive textbook covering the full spectrum of modern algorithms: from the fastest algorithms and data structures to polynomial-time algorithms for seemingly intractable problems, from classical algorithms in graph theory to special algorithms for string matching, computational geometry, and number theory. The revised third edition notably adds a chapter on van Emde Boas trees, one of the most useful data structures, and on multithreaded algorithms, a topic of increasing importance." —Daniel Spielman, Department of Computer Science, Yale University "As an educator and researcher in the field of algorithms for over two decades, I can unequivocally say that the Cormen book is the best textbook that I have ever seen on this subject. It offers an incisive, encyclopedic, and modern treatment of algorithms, and our department will continue to use it for teaching at both the graduate and undergraduate levels, as well as a reliable research reference." —Gabriel Robins, Department of Computer Science, University of Virginia Product Description Some books on algorithms are rigorous but incomplete; others cover masses of material but lack rigor. Introduction to Algorithms uniquely combines rigor and comprehensiveness. The book covers a broad range of algorithms in depth, yet makes their design and analysis accessible to all levels of readers. Each chapter is relatively self-contained and can be used as a unit of study. The algorithms are described in English and in a pseudocode designed to be readable by anyone who has done a little programming. The explanations have been kept elementary without sacrificing depth of coverage or mathematical rigor. The first edition became a widely used text in universities worldwide as well as the standard reference for professionals. The second edition featured new chapters on the role of algorithms, probabilistic analysis and randomized algorithms, and linear programming. The third edition has been revised and updated throughout. It includes two completely new chapters, on van Emde Boas trees and multithreaded algorithms, and substantial additions to the chapter on recurrences (now called "Divide-and-Conquer"). It features improved treatment of dynamic programming and greedy algorithms and a new notion of edge-based flow in the material on flow networks. Many new exercises and problems have been added for this edition. As of the third edition, this textbook is published exclusively by the MIT Press. About the Author Thomas H. Cormen is Professor of Computer Science and former Director of the Institute for Writing and Rhetoric at Dartmouth College. Charles E. Leiserson is Professor of Computer Science and Engineering at the Massachusetts Institute of Technology. Ronald L. Rivest is Andrew and Erna Viterbi Professor of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology. Clifford Stein is Professor of Industrial Engineering and Operations Research at Columbia University.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值