题目链接:https://vjudge.net/problem/HDU-5950

思路: 构造矩阵,然后利用矩阵快速幂。

  1 #include <bits/stdc++.h>
2 #include <time.h>
3 #include <set>
4 #include <map>
5 #include <stack>
6 #include <cmath>
7 #include <queue>
8 #include <cstdio>
9 #include <string>
10 #include <vector>
11 #include <cstring>
12 #include <utility>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 #include <list>
17 using namespace std;
18 //cout<<setprecision(10)<<fixed;
19 #define eps 1e-6
20 #define PI acos(-1.0)
21 #define lowbit(x) ((x)&(-x))
22 #define zero(x) (((x)>0?(x):-(x))<eps)
23 #define mem(s,n) memset(s,n,sizeof s);
24 #define rep(i,a,b) for(int i=a;i<=b;i++)
25 #define rep2(i,a,b) for(int i=a;i>=b;i--)
26 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
27 typedef long long ll;
28 typedef unsigned long long ull;
29 const int maxn=1e6+5;
30 const ll Inf=0x7f7f7f7f7f7f7f7f;
31 const ll mod=2147493647;
32 //const int N=3e3+5;
33 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判断一个数是不是 2 的正整数次幂
34 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//对 2 的非负整数次幂取模
35 int getBit(int a, int b) { return (a >> b) & 1; }// 获取 a 的第 b 位,最低位编号为 0
36 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 为 0,否则为 -1
37 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); }
38 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
39 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
40 inline int read()
41 {
42 int X=0; bool flag=1; char ch=getchar();
43 while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
44 while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
45 if(flag) return X;
46 return ~(X-1);
47 }
48 inline void write(int X)
49 {
50 if(X<0) {X=~(X-1); putchar('-');}
51 if(X>9) write(X/10);
52 putchar(X%10+'0');
53 }
54 /*
55 inline int write(int X)
56 {
57 if(X<0) {putchar('-'); X=~(X-1);}
58 int s[20],top=0;
59 while(X) {s[++top]=X%10; X/=10;}
60 if(!top) s[++top]=0;
61 while(top) putchar(s[top--]+'0');
62 }
63 */
64 void scan(__int128 &x)//输入
65 {
66 x = 0;
67 int f = 1;
68 char ch;
69 if((ch = getchar()) == '-') f = -f;
70 else x = x*10 + ch-'0';
71 while((ch = getchar()) >= '0' && ch <= '9')
72 x = x*10 + ch-'0';
73 x *= f;
74 }
75 void _print(__int128 x)
76 {
77 if(x > 9) _print(x/10);
78 putchar(x%10 + '0');
79 }
80 int Abs(int n) {
81 return (n ^ (n >> 31)) - (n >> 31);
82 /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 -1
83 若 n 为正数 n^0=n, 数不变,若 n 为负数有 n^(-1)
84 需要计算 n 和 -1 的补码,然后进行异或运算,
85 结果 n 变号并且为 n 的绝对值减 1,再减去 -1 就是绝对值 */
86 }
87 ll binpow(ll a, ll b) {
88 ll res = 1;
89 while (b > 0) {
90 if (b & 1) res = res * a%mod;
91 a = a * a%mod;
92 b >>= 1;
93 }
94 return res%mod;
95 }
96 void extend_gcd(ll a,ll b,ll &x,ll &y)
97 {
98 if(b==0) {
99 x=1,y=0;
100 return;
101 }
102 extend_gcd(b,a%b,x,y);
103 ll tmp=x;
104 x=y;
105 y=tmp-(a/b)*y;
106 }
107 ll mod_inverse(ll a,ll m)
108 {
109 ll x,y;
110 extend_gcd(a,m,x,y);
111 return (m+x%m)%m;
112 }
113 ll eulor(ll x)
114 {
115 ll cnt=x;
116 ll ma=sqrt(x);
117 for(int i=2;i<=ma;i++)
118 {
119 if(x%i==0) cnt=cnt/i*(i-1);
120 while(x%i==0) x/=i;
121 }
122 if(x>1) cnt=cnt/x*(x-1);
123 return cnt;
124 }
125 ll n,a,b;
126 typedef struct
127 {
128 ll mp[7][7];
129 void init()
130 {
131 mem(mp,0);
132 for(int i=0;i<7;i++)
133 mp[i][i]=1;
134 }
135 }matrix;
136 matrix pp={
137 1,1,0,0,0,0,0,
138 2,0,0,0,0,0,0,
139 1,0,1,0,0,0,0,
140 4,0,4,1,0,0,0,
141 6,0,6,3,1,0,0,
142 4,0,4,3,2,1,0,
143 1,0,1,1,1,1,1
144 };
145 matrix multi(matrix a,matrix b)
146 {
147 matrix res;
148 for(int i=0;i<7;i++)
149 {
150 for(int j=0;j<7;j++)
151 {
152 res.mp[i][j]=0;
153 for(int k=0;k<7;k++)
154 {
155 res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%mod)%mod;
156 }
157 }
158 }
159 return res;
160 }
161 matrix fastm (matrix a,ll x)
162 {
163 matrix res;
164 res.init();
165 while(x)
166 {
167 if(x&1) res=multi(res,a);
168 x>>=1;
169 a=multi(a,a);
170 }
171 return res;
172 }
173 int main()
174 {
175 int t=read();
176 while(t--)
177 {
178 scanf("%lld%lld%lld",&n,&a,&b);
179 if(n==1) printf("%lld\n",a);
180 else if(n==2) printf("%lld\n",b);
181 else
182 {
183 matrix now=fastm(pp,n-2);
184 ll num;
185 num=(b*now.mp[0][0])%mod;
186 num=(num+a*now.mp[1][0]%mod)%mod;
187 num=(num+16*now.mp[2][0]%mod)%mod;
188 num=(num+8*now.mp[3][0]%mod)%mod;
189 num=(num+4*now.mp[4][0]%mod)%mod;
190 num=(num+2*now.mp[5][0]%mod)%mod;
191 num=(num+now.mp[6][0]%mod)%mod;
192 printf("%lld\n",num);
193 }
194 }
195 return 0;
196 }

E - Recursive sequence HDU - 5950 (矩阵快速幂)的更多相关文章

  1. HDU 5950 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS

    传送门 好久没写数论题了写一次调了1h 首先发现递推式是一个乘方的形式,线性递推和矩阵快速幂似乎都做不了,那么是否能够把乘方运算变成加法运算和乘法运算呢? 使用原根!学过\(NTT\)的都知道\(99 ...

  3. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  4. HDU - 6395 Sequence (整除分块+矩阵快速幂)

    定义数列: $\left\{\begin{eqnarray*} F_1 &=& A \\ F_2 &=& B \\ F_n &=& C\cdot{}F_ ...

  5. HDU 6395 Sequence(分段矩阵快速幂)题解

    题意: 已知\(A,B,C,D,P,n\)以及 \[\left\{ \begin{aligned} & F_1 = A \\ & F_2 = B\\ & F_n = C*F_{ ...

  6. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  7. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  8. poj2778DNA Sequence (AC自动机+矩阵快速幂)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud DNA Sequence Time Limit: 1000MS   Memory ...

  9. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

随机推荐

  1. H.264视频压缩标准

    H.264 这部分一直在讲,但是却没有系统的来说.接下来要详细. 参看:H.264视频压缩标准 一.简介 H.264是最新的视频压缩标准,它也称为MPEG-4 Part 10或AVC(高级视频编码). ...

  2. MS16-032 windows本地提权

    试用系统:Tested on x32 Win7, x64 Win8, x64 2k12R2 提权powershell脚本: https://github.com/FuzzySecurity/Power ...

  3. python 编码问题随笔

    原文点击这里 借用原作者的一句话"据说,每个做 Python 开发的都被字符编码的问题搞晕过,最常见的错误就是 UnicodeEncodeError.UnicodeDecodeError,你 ...

  4. 属于我的md5sum程序

    目录 前言 介绍 使用说明 总结 前言 之所以想做这个软件是因为一直在使用的http://keir.net/hash.html软件有很多功能不能满足. 经过自学C#,研究多线程,异步更新UI,等等知识 ...

  5. golang1.16新特性速览

    今天是假期最后一天,明天起大家也要陆续复工了.golang1.16也在今天正式发布了. 原定计划是2月1号年前发布的,不过迟到也是golang的老传统了,正好也趁着最后的假期快速预览一下golang1 ...

  6. 一个最简单 node.js 命令行工具

    一个最简单 node.js 命令行工具 node.js cli $ node cli.js xyz # OR $ node cli xyz 接受参数 process.argv js "use ...

  7. how to close macos eject icon from menu bar

    how to close macOS eject icon from the menu bar close eject https://apple.stackexchange.com/question ...

  8. ts 在Function上创建静态属性和方法

    interface IMessage { (value: any): void; success(): void; error(): void; version: string; } const Me ...

  9. 【函数分享】每日PHP函数分享(2021-2-19)

    array_diff_uassoc - 用用户提供的回调函数做索引检查来计算数组的差集 说明 array_diff_uassoc ( array $array1 , array $array2 , a ...

  10. 05.其他创建numpy数组的方法

    >>> import numpy as np >>> np.zeros(10,dtype=int) array([0, 0, 0, 0, 0, 0, 0, 0, 0 ...