1065 - Number Sequence &&1070 - Algebraic Problem
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Let's define another number sequence, given by the following function:
f(0) = a
f(1) = b
f(n) = f(n-1) + f(n-2), n > 1
When a = 0 and b = 1, this sequence gives the Fibonacci sequence. Changing the values of a and b, you can get many different sequences. Given the values of a, b, you have to find the last m digits of f(n).
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each test case consists of a single line containing four integers a b n m. The values of a and b range in [0,100], value of n ranges in [0, 109] and value of m ranges in [1, 4].
Output
For each case, print the case number and the last m digits of f(n). However, do NOT print any leading zero.
Sample Input |
Output for Sample Input |
4 0 1 11 3 0 1 42 4 0 1 22 4 0 1 21 4 |
Case 1: 89 Case 2: 4296 Case 3: 7711 Case 4: 946 |
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 typedef struct pp
12 {
13 LL m[4][4];
14 pp()
15 {
16 memset(m,0,sizeof(m));
17 }
18 } maxtr;
19 maxtr E()
20 {
21 maxtr ans;
22 int i,j;
23 for(i=0; i<4; i++)
24 {
25 for(j=0; j<4; j++)
26 {
27 if(i==j)
28 {
29 ans.m[i][j]=1;
30 }
31 else ans.m[i][j]=0;
32 }
33 }
34 return ans;
35 }
36 void Init (maxtr *p)
37 { int i,j;
38 for(i=0; i<2; i++)
39 {
40 for(j=0; j<2; j++)
41 {
42 if(i==1&&j==1)
43 {
44 p->m[i][j]=0;
45 }
46 else p->m[i][j]=1;
47 }
48 }
49 }
50 maxtr quick(maxtr ans ,int m,int mod)
51 {
52 maxtr ak=E();
53 int i,j;
54 while(m)
55 {
56 if(m&1)
57 {
58 maxtr cc;
59 for(i=0; i<2; i++)
60 {
61 for(j=0; j<2; j++)
62 {
63 for(int s=0; s<2; s++)
64 {
65 cc.m[i][j]=(cc.m[i][j]+ans.m[i][s]*ak.m[s][j]%mod)%mod;
66 }
67 }
68 }
69 ak=cc;
70 }
71 maxtr cc;
72 for(i=0; i<2; i++)
73 {
74 for(j=0; j<2; j++)
75 {
76 for(int s=0; s<2; s++)
77 {
78 cc.m[i][j]=(cc.m[i][j]+ans.m[i][s]*ans.m[s][j]%mod)%mod;
79 }
80 }
81 }
82 ans=cc;
83 m/=2;
84 }
85 return ak;
86 }
87 int main(void)
88 {
89 int i,j,k;
90 int s;
91 scanf("%d",&k);
92 int a,b,n,m;
93 for(s=1; s<=k; s++)
94 {
95 scanf("%d %d %d %d",&a,&b,&n,&m);
96 printf("Case %d: ",s);
97 if(n==0)
98 {
99 printf("%d\n",a%m);
100 }
101 else if(n==1)
102 {
103 printf("%d\n",b%m);
104 }
105 else
106 {
107 maxtr ak;
108 Init(&ak);
109 int mod=1;
110 for(i=1;i<=m;i++)
111 mod*=10;
112 ak=quick(ak,n-1,mod);
113 LL ask=ak.m[0][0]*b+ak.m[0][1]*a;
114 ask%=mod;
115 printf("%lld\n",ask);
116 }
117 }
118 return 0;
119 }
1065
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 unsigned long long LL;
11 typedef long long L;
12 typedef struct pp
13 {
14 LL m[4][4];
15 pp()
16 {
17 memset(m,0,sizeof(m));
18 }
19 } maxtr;
20 maxtr E()
21 {
22 int i,j;
23 maxtr ans;
24 for(i=0; i<=3; i++)
25 {
26 for(j=0; j<=3; j++)
27 {
28 if(i==j)
29 ans.m[i][j]=1;
30 else ans.m[i][j]=0;
31 }
32 }
33 return ans;
34 }
35 void Init(maxtr *p,LL x,LL y)
36 {
37 int i,j;
38 memset(p->m,0,sizeof(p->m));
39 p->m[0][0]=x;
40 p->m[0][1]=-y;
41 p->m[1][0]=1;
42 }
43 maxtr quick(maxtr ak,LL m)
44 {
45 int i,j,s;
46 maxtr ac=E();
47
48 while(m)
49 {
50 if(m&1)
51 {
52 maxtr cc;
53 for(i=0; i<2; i++)
54 {
55 for(j=0; j<2; j++)
56 {
57 for(s=0; s<2; s++)
58 {
59 cc.m[i][j]=(cc.m[i][j]+ak.m[i][s]*ac.m[s][j]);
60 }
61 }
62 }
63 ac=cc;
64 }
65 maxtr cc;
66 for(i=0; i<2; i++)
67 {
68 for(j=0; j<2; j++)
69 {
70 for(s=0; s<2; s++)
71 {
72 cc.m[i][j]=(cc.m[i][j]+ak.m[i][s]*ak.m[s][j]);
73 }
74 }
75 }
76 ak=cc;
77 m/=2;
78 }
79 return ac;
80 }
81 int main(void)
82 {
83 int i,j,k;
84 scanf("%d",&k);
85 int s;
86 for(s=1; s<=k; s++)
87 {
88 LL x,y,z;
89 scanf("%llu %llu %llu",&x,&y,&z);
90 LL f1=1;
91 LL f2=x;
92 LL f3=x*x-2*y;
93 maxtr ans;
94 Init(&ans,x,y);
95 printf("Case %d: ",s);
96 if(z==1)
97 {
98 printf("%llu\n",x);
99 }
100 else if(z==2)
101 {
102 printf("%llu\n",f3);
103 }
104 else if(z==0)
105 {
106 printf("2\n");
107 }
108 else
109 {
110 ans= quick(ans,z-2);
111 LL ak=ans.m[0][0]*f3+ans.m[0][1]*f2;
112 printf("%llu\n",ak);
113 }
114 }
115 return 0;
116 }
1070
1065 - Number Sequence &&1070 - Algebraic Problem的更多相关文章
- LightOJ 1070 - Algebraic Problem 矩阵高速幂
题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Sta ...
- LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)
题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 ...
- LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
- LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...
- LightOj 1065 - Number Sequence (矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...
- HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- Ubuntu16.04安装 2.8.5版本Ansible
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py && python get-pip.py pip install --upgrade ...
- MIT6.824 分布式系统实验
LAB1 mapreduce mapreduce中包含了两个角色,coordinator和worker,其中,前者掌管任务的分发和回收,后者执行任务.mapreduce分为两个阶段,map阶段和red ...
- LeetCode一维数组的动态和
一维数组的动态和 题目描述 给你一个数组 nums.数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]...nums[i]). 请返回 nums 的动态和. 示例 1: ...
- 【swift】CoreData Crash(崩溃)(Failed to call designated initializer on NSManagedObject class)
感谢另一篇博客:https://blog.csdn.net/devday/article/details/6577985 里面的图片和介绍,发现问题如他描述的一样,没有bundle 我的Xcode版本 ...
- linux shell学习之shell流程控制
在linux shell编程中,流程控制结构与语句,也算是shell脚本中的重点了,不了解的朋友,跟随脚本小编一起来学习下吧. linux控制流结构学习. 一,shell控制流结构 1.控制结构 ...
- Javascript 数组对象常用的API
常用的JS数组对象API ES5及以前的Api ECMAScript5为数组定义了5个迭代方法,每个方法接收两个参数, 一个是每项运行的函数,一个是运行该函数的作用域对象(可选项),传入这些方法的函数 ...
- table表格数据无缝循环滚动
分享一个好看的表格无缝滚动:(实战用起来很舒服) 直接copy代码到你的程序中: 1.HTML <div class="tablebox"> ...
- matplotlib画直线图的基本用法
一 figure使用 1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 # 从-3到中取50个数 5 x = np.linspac ...
- 使用IntelliJ IDEA创建简单的Spring Boot项目
方法一: File - New -Project 创建结束后进行测试运行,修改代码如下: package com.springboot.testone; import org.springframew ...
- 【C/C++】算法入门:排序/算法笔记
(设排序从小到大) 冒泡排序 这个大家都会,从第一个开始往后俩俩交换,然后第二个,最后到最后一个,复杂度n^2 选择排序 思路和冒泡差不多,比如要得到从小到大的排序,就是从第一个开始,i取1~n,每次 ...