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 |
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的更多相关文章
- asp.net MVC helper 和自定义函数@functions小结
asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...
- 【跟着子迟品 underscore】Array Functions 相关源码拾遗 & 小结
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...
- 【跟着子迟品 underscore】Object Functions 相关源码拾遗 & 小结
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...
- BZOJ 1131: [POI2008]Sta
Description 一棵树,问以那个节点为根时根的总和最大. Sol DFS+树形DP. 第一遍统计一下 size 和 d. 第二遍转移根,统计答案就行了. Code /************* ...
- ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)
一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...
- QM模块包含主数据(Master data)和功能(functions)
QM模块包含主数据(Master data)和功能(functions) QM主数据 QM主数据 1 Material Master MM01/MM02/MM50待测 物料主数据 2 Sa ...
- jQuery String Functions
In today's post, I have put together all jQuery String Functions. Well, I should say that these are ...
- 2-4. Using auto with Functions
在C++14中允许使用type deduction用于函数参数和函数返回值 Return Type Deduction in C++11 #include <iostream> using ...
- [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 ...
随机推荐
- C++面试基础篇(一)
1. static关键字的作用 (1)全局静态变量 在全局变量前面加上关键字static, 全局变量就定义为一个全局静态变量 在静态存储区,在整个程序运行期间一致存在. 初始化:未初始化的全局静态变量 ...
- Selenium的安装和使用
一.Selenium的安装,Selenium是一个自动化测试工具,利用它我们可以驱动浏览器执行特定的动作,如点击.下拉等操作.对于一些JavaScript渲染的页面来说,这种抓取方式非常有效.1.pi ...
- TOMCAT 搭建
第一步:下载 软件 和 JDK 第二个:https://www.oracle.com/java/technologies/javase-jdk16-downloads.html 传输到Linux里. ...
- vivo 敏感词匹配系统的设计与实践
一.前言 谛听系统是vivo的内容审核平台,保障了vivo各互联网产品持续健康的发展.谛听支持审核多种内容类型,但日常主要审核的内容是文本,下图是一个完整的文本审核流程,包括名单匹配.敏感词匹配.AI ...
- 总结HashSet以及分析部分底层源码
总结HashSet以及分析部分底层源码 1. HashSet继承的抽象类和实现的接口 继承的抽象类:AbstractSet 实现了Set接口 实现了Cloneable接口 实现了Serializabl ...
- day11 四层负载均衡和http协议
day11 四层负载均衡和http协议 四层负载均衡和七层负载均衡的区别 四层和七层负载均衡的区别 四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发.由此可以看出,七层负 ...
- nodejs-Express框架
JavaScript 标准参考教程(alpha) 草稿二:Node.js Express框架 GitHub TOP Express框架 来自<JavaScript 标准参考教程(alpha)&g ...
- Linux信号1
信号(signal)是一种软中断,他提供了一种处理异步事件的方法,也是进程间唯一的异步通信方式.在Linux系统中,根据POSIX标准扩展以后的信号机制,不仅可以用来通知某进程发生了什么事件,还可以给 ...
- android studio 编译NDK android studio 生成.so文件
详细配置使用请移步:https://www.jianshu.com/p/4c7d9a10933b android studio NDK 编译 第一步: app/build.gradle下面 添加代码: ...
- jdk1.6,1.7,1.8解压版无需安装(64位)
1.java SE 1.6各个版本 jdk http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads ...