HDU 1757

简单的矩阵构造题,参看我前几篇的谈到的矩阵的构造法。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int Mod;
struct Matrax {
	int m[15][15];
};
Matrax a,per;
int ats[15],an[15];

void initial(){
	memset(per.m,0,sizeof(per.m));
	memset(a.m,0,sizeof(a.m));
	for(int i=0;i<10;i++){
		a.m[i][0]=ats[i];
		per.m[i][i]=1;
	}
	for(int i=1;i<10;i++){
		a.m[i-1][i]=1;
	}
	for(int i=0;i<10;i++)
	an[i]=10-i-1;
}

Matrax multi(Matrax a,Matrax b){
	Matrax c;
	for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			c.m[i][j]=0;
			for(int k=0;k<10;k++)
			c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%Mod;
		}
	}
	return c;
}

Matrax Power(int k){
	Matrax ans=per,p=a;
	while(k){
		if(k&1){
			ans=multi(ans,p);
		}
		k>>=1;
		p=multi(p,p);
	}
	return ans;
}

int main(){
	int k;
	while(scanf("%d%d",&k,&Mod)!=EOF){
		for(int i=0;i<10;i++)
		scanf("%d",&ats[i]);
		if(k<10){
			printf("%d\n",k);
			continue;
		}
		initial();
		Matrax ans=Power(k-9);
		int sum=0;
		for(int i=0;i<10;i++)
		sum=(sum+an[i]*ans.m[i][0])%Mod;
		printf("%d\n",sum);
	}
	return 0;
}

  

时间: 2024-11-17 08:31:42

HDU 1757的相关文章

HDU 1757 A Simple Math Problem(矩阵快速幂)

题目地址:HDU 1757 终于会构造矩阵了.其实也不难,只怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10) 构造的矩阵是:(我代码中构造的矩阵跟这个正好是上下颠倒过来了) |0 1 0 ......... 0|    |f0|   |f1 | |0 0 1 0 ...... 0|    |f1|   |f2 | |...................1| *  |..| = |...|

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

HDU 1757 矩阵相乘,快速幂模板题

HDU 1757 题意: If x < 10, f(x) = x; If x >= 10, f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10);  给出k和mod,求f(k). 总结: 1.特别注意,矩阵相乘不满足交换律,即a*b != b*a.  2.感觉推方程有点困难. 3.矩阵初始化注意. f(x-10)   0 0 0 0 0 0 0 0 0        ( first矩阵 )       f(x-9)

HDU - 1757 A Simple Math Problem (构造矩阵)

Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); And ai(0<=i<=9) can only be 0 or 1 . Now, I will give a0 ~ a9 and two positive in

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w

hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 所求的是f(x)取m的模,而x,m,a[0]至a[9]都是输入项 初拿到这道题,最开始想的一般是暴力枚举,通过for循环求出f(x)然后再取模,但是有两个问题,首先f(x)可能特别大,其

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

HDU 1757 A Simple Math Problem

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 108 Accepted Submission(s): 77   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

*HDU 1757 矩阵乘法

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4307    Accepted Submission(s): 2586 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) =

【HDU 1757】 A Simple Math Problem

题 Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); And ai(0<=i<=9) can only be 0 or 1 . Now, I will give a0 ~ a9 and two positive