zoukankan      html  css  js  c++  java
  • FZU oj 1683 纪念SlingShot(矩阵快速幂)

    http://acm.fzu.edu.cn/problem.php?pid=1683

    自己独立完成的矩阵快速幂,O(∩_∩)O哈哈~

    Description

    已知 F(n)=3 * F(n-1)+2 * F(n-2)+7 * F(n-3),n>=3,其中F(0)=1,F(1)=3,F(2)=5,对于给定的每个n,输出F(0)+ F(1)+ …… + F(n) mod 2009。

    Input

    第一行是一整数m,代表总共有m个cases。

    Output

    对于每个case,输出一行。格式见样例,冒号后有一个空格。

    Sample Input

    2
    3
    6
    

    Sample Output

    Case 1: 37
    Case 2: 313
    此题采用矩阵快速幂
    

    题意:f(0)=1,f(1)=3,f(2)=5,f(n)=3f(n-1)+2f(n-2)+5f(n-3)。求s(n)=(f(0)+f(1)……+f(n))%2009。

    思路:S(n)=S(n-1)+F(n)=S(n-1)+3F(n-1)+2F(n-2)+7F(n-3),因此构造矩阵:

    我从s3开始使用矩阵, 从S0就开始用矩阵,想出来太麻烦,就干脆直接从S3吧大笑 AC代码
    #include <cstdio>
    using namespace std;
    #define N 4
    #define MOD 2009
    void matric_mul(int a[][N], int b[][N])
    {
    	int i, j, k;
    	int tmp1[N][N] = {0};
    
    	for (i = 0; i < N; i++)
    	{
    		for (j = 0; j < N; j++)
    		{
    			for (k = 0; k < N; k++)
    			{
    				tmp1[i][j] = (tmp1[i][j] + b[i][k] * a[k][j]) % MOD;
    			}
    		}
    	}
    
    	for (i = 0; i < N; i++)
    	{
    		for (j = 0; j < N; j++)
    		{
    			a[i][j] = tmp1[i][j];
    		}
    	}
    }
    int quickpow(int n)
    {
    	//int ans=1,tmp=base;
    	//int ans[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0},{0, 0, 1, 0},{0, 0, 0, 1}};
    	int tmp[4][4] = {{1, 3, 2, 7}, {0, 3, 2, 7},{0, 1, 0, 0},{0, 0, 1, 0}};
    	int begin[4][4] = {{9, 0, 0, 0},{5, 0, 0, 0},{3, 0, 0, 0},{1, 0, 0, 0}};
    
    	while (n)
    	{
    		if (n & 1)
    		{
    			matric_mul(begin,tmp);
    		}			//ans=(ans*tmp)%MOD;tmp=(tmp*tmp)%MOD;
    
    		matric_mul(tmp, tmp);
    
    		n >>= 1;
    	}
    
    	return begin[0][0];
    }
    int main()
    {
    	int t,k = 1;
    	scanf("%d",&t);
    	while (t--)
    	{
    		int n;
    		scanf("%d",&n);
    		printf("Case %d: ",k++);
    		if(n>2)
    			printf("%d
    ", quickpow(n-2));
    		else
    		{
    			if(n==0) printf("1
    ");
    			else if(n==1)  printf("4
    ");
    			else printf("9
    ");
    		}
    	}
    
    	return 0;
    }
    


    www.cnblogs.com/tenlee
  • 相关阅读:
    使用 Fetch
    实现一个联系客服对话框的前端部分
    javascript之Object.defineProperty的奥妙
    vue之nextTick全面解析
    创建元素和删除元素
    vue.js应用开发笔记
    待字闺中之最多连续数的子集
    HDU-1212-Big Number
    虚方法【仅仅有虚方法或者抽象方法才干被子类方法重写】
    利用localStorage实现对ueditor编辑内容定时保存为草稿
  • 原文地址:https://www.cnblogs.com/tenlee/p/4420154.html
Copyright © 2011-2022 走看看