1131 - Just Two Functions
Time Limit: 2 second(s) Memory Limit: 32 MB

Let

fn = a1 * fn-1 + b1 * fn-2 + c1 * gn-3

gn = a2 * gn-1 + b2 * gn-2 + c2 * fn-3

Find fn % M and gn % M. (% stands for the modulo operation.)

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a blank line. Next line contains three integers a1 b1 c1 (0 ≤ a1, b1, c1 < 25000). Next line contains three integers a2 b2 c2 (0 ≤ a2, b2, c2 < 25000). Next line contains three integers f0 f1 f2(0 ≤ f0, f1, f2 < 25000). Next line contains three integers g0 g1 g2 (0 ≤ g0, g1, g2 < 25000). The next line contains an integer M (1 ≤ M < 25000).

Next line contains an integer q (1 ≤ q ≤ 100) denoting the number of queries. Next line contains q space separated integers denoting n. Each of these integers is non-negative and less than 231.

Output

For each case, print the case number in a line. Then for each query, you have to print one line containing fn % M and gn % M.

Sample Input

Output for Sample Input

2

1 1 0

0 0 0

0 1 1

0 0 0

20000

10

1 2 3 4 5 6 7 8 9 10

1 1 1

1 1 1

2 2 2

2 2 2

20000

5

2 4 6 8 10

Case 1:

1 0

1 0

2 0

3 0

5 0

8 0

13 0

21 0

34 0

55 0

Case 2:

2 2

10 10

34 34

114 114

386 386


PROBLEM SETTER: JANE ALAM JAN
思路:矩阵快速幂;
比较简单,矩阵也比较好推。
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<math.h>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 char str[100];
12 char ask[100];
13 LL ans[200];
14 int M;
15 typedef struct pp
16 {
17 LL m[10][10];
18 pp()
19 {
20 memset(m,0,sizeof(m));
21 }
22 } maxtr;
23 LL xishu[10];
24 LL xisu[10];
25 LL f[10];
26 LL g[10];
27 maxtr E()
28 {
29 maxtr ac;
30 int i,j;
31 for(i=0; i<10; i++)
32 {
33 for(j=0; j<10; j++)
34 {
35 if(i==j)
36 {
37 ac.m[i][j]=1;
38 }
39 else ac.m[i][j]=0;
40 }
41 }
42 return ac;
43 }
44 void Init(maxtr *p)
45 {
46 int i,j,k;
47 memset(p->m,0,sizeof(p->m));
48 p->m[0][0]=xishu[0];
49 p->m[0][1]=xishu[1];
50 p->m[0][5]=xishu[2];
51 p->m[1][0]=1;
52 p->m[2][1]=1;
53 p->m[3][2]=xisu[2];
54 p->m[3][3]=xisu[0];
55 p->m[3][4]=xisu[1];
56 p->m[4][3]=1;
57 p->m[5][4]=1;
58 }
59 maxtr quick(maxtr C,LL m)
60 {
61 maxtr ak=E();
62 int s;
63 int i,j;
64 while(m)
65 {
66 if(m&1)
67 {
68 maxtr vv;
69 memset(vv.m,0,sizeof(vv.m));
70 for(i=0; i<=5; i++)
71 {
72 for(j=0; j<=5; j++)
73 {
74 for(s=0; s<=5; s++)
75 {
76 vv.m[i][j]=(vv.m[i][j]+C.m[i][s]*ak.m[s][j]%M)%M;
77 }
78 }
79 }
80 ak=vv;
81 }
82 maxtr vv;memset(vv.m,0,sizeof(vv.m));
83 for(i=0; i<=5; i++)
84 {
85 for(j=0; j<=5; j++)
86 {
87 for(s=0; s<=5; s++)
88 {
89 vv.m[i][j]=(vv.m[i][j]+C.m[i][s]*C.m[s][j]%M)%M;
90 }
91 }
92 }
93 C=vv;
94 m/=2;
95 }
96 return ak;
97 }
98 int main(void)
99 {
100 LL i,j,k;
101 scanf("%lld",&k);
102 LL s;
103 for(s=1; s<=k; s++)
104 {
105 for(i=0; i<3; i++)
106 {
107 scanf("%lld",&xishu[i]);
108 }
109 for(i=0; i<3; i++)
110 {
111 scanf("%lld",&xisu[i]);
112 }
113 for(i=0; i<3; i++)
114 {
115 scanf("%lld",&f[i]);
116 }
117 for(i=0; i<3; i++)
118 {
119 scanf("%lld",&g[i]);
120 }
121 scanf("%d",&M);
122 int cnt=0;
123 scanf("%d",&cnt);
124 for(i=0; i<cnt; i++)
125 {
126 scanf("%lld",&ans[i]);
127 }
128 printf("Case %d:\n",s);
129 for(i=0; i<cnt; i++)
130 {
131 if(ans[i]<2)
132 {
133 printf("%lld %lld\n",f[ans[i]]%M,g[ans[i]]%M);
134 }
135 else
136 {
137 maxtr ac;
138 memset(ac.m,0,sizeof(ac.m));
139 Init(&ac);
140 maxtr ak=quick(ac,ans[i]-2);
141 LL ak1=ak.m[0][0]*f[2]%M+ak.m[0][1]*f[1]%M+ak.m[0][5]*g[0]%M+ak.m[0][2]*f[0]%M+ak.m[0][3]*g[2]%M+ak.m[0][4]*g[1]%M;
142 ak1%=M;
143 LL ak2=ak.m[3][2]*f[0]%M+ak.m[3][3]*g[2]%M+ak.m[3][4]*g[1]%M+ak.m[3][0]*f[2]%M+ak.m[3][1]*f[1]%M+ak.m[3][5]*g[0]%M;
144 ak2%=M;
145 printf("%lld %lld\n",ak1,ak2);
146 }
147 }
148 }return 0;
149 }

1131 - Just Two Functions的更多相关文章

  1. asp.net MVC helper 和自定义函数@functions小结

    asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...

  2. 【跟着子迟品 underscore】Array Functions 相关源码拾遗 & 小结

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  3. 【跟着子迟品 underscore】Object Functions 相关源码拾遗 & 小结

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  4. BZOJ 1131: [POI2008]Sta

    Description 一棵树,问以那个节点为根时根的总和最大. Sol DFS+树形DP. 第一遍统计一下 size 和 d. 第二遍转移根,统计答案就行了. Code /************* ...

  5. ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)

    一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...

  6. QM模块包含主数据(Master data)和功能(functions)

    QM模块包含主数据(Master data)和功能(functions)   QM主数据   QM主数据 1 Material   Master MM01/MM02/MM50待测 物料主数据 2 Sa ...

  7. jQuery String Functions

    In today's post, I have put together all jQuery String Functions. Well, I should say that these are ...

  8. 2-4. Using auto with Functions

    在C++14中允许使用type deduction用于函数参数和函数返回值 Return Type Deduction in C++11 #include <iostream> using ...

  9. [Python] Pitfalls: About Default Parameter Values in Functions

    Today an interesting bug (pitfall) is found when I was trying debug someone's code. There is a funct ...

随机推荐

  1. 商业创新不能等?用友低代码开发平台YonBuilder为您加速!

    随着云计算.人工智能.物联网.大数据.5G等新一代技术的快速发展,越来越多的企业希望借助技术的力量加速数智化转型,期许通过更加敏捷和强大的应用系统推动企业的商业创新速度.但传统软件开发周期长.开发成本 ...

  2. TD课程通最终版本体验

    功能上,新版本增加了学校教室的上课情况,有无课程可以清楚查询,如下图: 在添加课程的设置上有改进,相比于之前编辑课程后不能保存,新版本在可保存的基础上又增加了登陆教务系统的功能,学生使用更加方便快捷, ...

  3. 如何删除苹果电脑垃圾文件-7个高级技巧释放大量苹果Mac

    硬盘空间用尽是一件很让人头疼的事情,尤其是MacBook Air等设备上的固态硬盘可用的储存空间很少.下面[微IT]为大家介绍7个高级技巧来释放大量的硬盘空间,当然这些高级技巧更改了系统功能和文件,必 ...

  4. Linux磁盘分区(一)之fdisk命令

    Linux磁盘分区(一)之fdisk命令转自:https://www.cnblogs.com/machangwei-8/p/10353683.html 一.fdisk 的介绍fdsik 能划分磁盘成为 ...

  5. ORACLE 本session产生的redo

    select * from v$statname a ,v$mystat bwhere a.STATISTIC# = b.STATISTIC# and a.name = 'redo size';

  6. 解决ViewPager与ScrollView 冲突

    ViewPager来实现左右滑动切换tab,如果tab的某一项中嵌入了水平可滑动的View就会让你有些不爽,比如想滑动tab项中的可水平滑动的控件,却导致tab切换. 因为Android事件机制是从父 ...

  7. archive后upload to app store时遇到app id不可用的问题

    问题如下图 出现此问题的原因有两种: 1.此app id在AppStore中已经存在,也就是说你使用别人注册的app ID ,  如果是这样,你只能更换app ID 2.此app ID是自己的,突然之 ...

  8. 在应用程序中的所有其他bean被销毁之前执行一步工作

    1.实现ServletContextListener.ApplicationContextAware两个接口,在销毁方法里借助ApplicationContextAware注入的application ...

  9. springboot+vue脚手架使用nginx前后端分离

    1.vue配置 /** * * 相对于该配置的nginx服务器请参考nginx配置文件 * */ module.exports = { // 基本路径 publicPath: '/', // 输出文件 ...

  10. 一个统计 CPU 内存 硬盘 使用率的shell脚本

    一个统计 CPU 内存 硬盘 使用率的shell脚本,供大家学习参考 #!/bin/bash #This script is use for describle CPU Hard Memery Uti ...