ACM 矩阵乘法 SPOJ AMR10E Stocks Prediction
来源:互联网 发布:8年四钻淘宝店值多少钱 编辑:程序博客网 时间:2024/05/22 13:48
http://www.spoj.com/problems/AMR10E/
Description
The department store where my family goes shopping is trying to predict how much of each item they stock they will sell each month. Keeping a large amount of inventory is costly, and running out of items is also not a good idea. Since the manager asked for my help as a sales consultant, I decided to formulate a model for predicting each month's sales S of an item based on its sales during the previous R months. After a lot of trial and error, I came up with such a model, where
S(n) = a1*S(n-1) + a2*S(n-2) + ... + aR*S(n-R)
where S(n) is the predicted sales for the nth month for n > R, and S(1) to S(R) are seed values.
The store manager was pleased with my model's ability to help him in controlling his inventory.
He asked me to list out every Kth month's sales, and give him the sum of the first N values from this list. For example he wanted every Christmas month's sales summed up for the next 10 years (N=10 and K=12, month 1 being January), or every end-of-quarter month's sales for the next 2 years (N=2, K=3).
Can you please help me write a program that does all the above?
INPUT
The first line of the input T, the number of test cases. Each test case consists of three lines.
The first line of each test case contains N, R, K.
The second line of each test case contains R integers denoting S(1), S(2), ..., S(R).
The third line of each test case contains R integers denoting the coefficients a1, a2, ..., aR of the predictive model.
OUTPUT
For each test case, output the sum requested by the manager as given in the problem statement, modulo 1,000,000,007.
CONSTRAINTS
T <= 40
1 <= N <= 1000000000
1 <= R <= 8
1 <= K <= 8
0 <= All other input values < 1000000007
SAMPLE INPUT
2
4 1 1
1
2
3 2 3
1 1
1 1
SAMPLE OUTPUT
15
44
EXPLANATION
In the first test case, it is given that S(1) = 1 and the relation is S(n)=2*S(n-1). The list asked by the store manager consists of all the terms of S since K is 1. Hence, the answer is just the sum of the first 4 terms of S.
In the second test case, the sequence S is the fibonacci sequence which is: 1, 1, 2, 3, 5, 8, 13, 21, 34. The list consists of 2, 8, 34 which sum up to 44.
转移矩阵为a
先将分为每组k次,算a^k //a=quick(a,k);
再算(a^k)^k //a=quick(a,n);
<strong>Memory: </strong>2662 KB<strong>Time: </strong>170 MS<strong>Language: </strong>C++ (g++ 4.3.2)<strong>Result: </strong><span style="color:blue;">Accepted</span>
/* * Author: NICK WONG * Created Time: 7/30/2014 16:45:37 * File Name: */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;#define out(x) cout<<#x<<": "<<x<<endlconst double eps(1e-8);const int maxn=10100;const long long maxint=-1u>>1;const long long maxlong=maxint*maxint;typedef long long lint;const lint mod=1000000007;lint ans;int n,r,k,p[20];lint s[200];struct matrix{ lint m[10][10]; matrix() { memset(m,0,sizeof(m));//注意要清零 }};matrix operator * (const matrix & a,const matrix & b){ matrix c; for (int i=1; i<=9; i++) for (int j=1; j<=9; j++) { c.m[i][j]=0; for (int k=1; k<=9; k++) c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%mod; } return c;}matrix quick(matrix base,int pow){ matrix a; for (int i=1; i<=9; i++) a.m[i][i]=1;//单位阵,矩阵乘法时要用到 while (pow) { if (pow&1) a=a*base; base=base*base; pow>>=1; } //if (pow==0) return base; return a;} void init(){ memset(s,0,sizeof(s)); cin>>n>>r>>k; for (int i=1; i<=r; i++) cin>>s[i]; for (int i=1; i<=r; i++) cin>>p[i];}void work(){ for (int i=r+1; i<=80; i++) { for (int j=1; j<=r; j++) s[i]=(s[i]+p[j]*s[i-j])%mod; } matrix a,wjj; for (int i=1; i<=8; i++) wjj.m[1][i]=s[i]; for (int i=1; i<=7; i++) a.m[i+1][i]=1; for (int i=1; i<=r; i++) a.m[8-i+1][8]=p[i]; a=quick(a,k); a.m[k][9]=a.m[9][9]=1; a=quick(a,n); wjj=wjj*a; ans=wjj.m[1][9]; cout<<ans<<endl; }int main(){ int t; cin >> t; while (t--) { init(); work(); } return 0;}
- ACM 矩阵乘法 SPOJ AMR10E Stocks Prediction
- SPOJ AMR10E Stocks Prediction
- SPOJ AMR10E Stocks Prediction --二分求和+矩阵快速幂
- SPOJ 8059. Stocks Prediction (矩阵嵌矩阵)
- SPOJ AMR 10E Stocks Prediction 矩阵快速幂 + 二分求和
- ACM:蓝桥杯:矩阵乘法
- ACM矩阵乘法的题目总结
- ACM 矩阵乘法模板(T_T)+快速幂
- 【转载】ACM中矩阵乘法的应用
- stocks
- ACM从零开始---电子科大POJ "3*3矩阵的乘法"
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- 矩阵乘法
- POJ 1284 Primitive Roots (求原根个数)
- 学生信息管理系统-----第一遍验收
- 【ios】AVCapture的理解
- vistual studio 2012 安装失败,提示Microsoft Vistual Studio 2012 Devenv找不到元素,等错误信息
- TOJ 3774. Calculation (栈操作计算表达式)
- ACM 矩阵乘法 SPOJ AMR10E Stocks Prediction
- 《STl源码剖析》疑问?
- 关于C++中野指针的说明
- 开发RESTful WebService
- 通过函数给指针赋值(一)
- Linux基础及常用命令(笔试面试必备)
- 二叉树的遍历
- win7 下硬盘安装 Centos6.3(6.5)
- PHP中include()与require()的区别说明