HDU 4565 So Easy!(矩陣快速冪)
這裡寫的很詳細了啊:http://blog.csdn.net/acmmmm/article/details/9722515?utm_source=tuicool
記錄一下,回頭複習用啊。
So Easy!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2281 Accepted Submission(s): 708
Problem Description
A sequence Sn is defined as:
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
You, a top coder, say: So easy!
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
You, a top coder, say: So easy!
Input
There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
Output
For each the case, output an integer Sn.
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
Sample Output
4
14
4
Source
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
///#define mod 10007
const int maxn = 210;
using namespace std;
struct matrix
{
LL f[3][3];
};
LL mod;
matrix mul(matrix a, matrix b, int n)
{
matrix c;
memset(c.f, 0, sizeof(c.f));
for(int k = 0;k < n; k++)
{
for(int i = 0; i < n;i++)
{
if(!a.f[i][k]) continue;
for(int j = 0; j < n; j++)
{
if(!b.f[k][j]) continue;
c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j]+mod)%mod;
}
}
}
return c;
}
matrix pow_mod(matrix a, LL b, int n)
{
matrix s;
memset(s.f, 0 , sizeof(s.f));
for(int i = 0; i < n; i++) s.f[i][i] = 1LL;
while(b)
{
if(b&1) s = mul(s, a, n);
a = mul(a, a, n);
b >>= 1;
}
return s;
}
int main()
{
LL a, b, n;
while(~scanf("%I64d %I64d %I64d %I64d",&a, &b, &n, &mod))
{
if(n == 1)
{
printf("%I64d\n",2*a%mod);
continue;
}
matrix c;
memset(c.f, 0 , sizeof(c.f));
c.f[0][0] = 2LL*a;
c.f[0][1] = b-a*a;
c.f[1][0] = 1LL;
matrix d = pow_mod(c, n-1, 2);
printf("%I64d\n",((d.f[0][0]*2*a+d.f[0][1]*2)%mod+mod)%mod);
}
return 0;
}
相關文章
- HDU4565 So Easy! (矩陣)矩陣
- HDU 4565 So Easy!(公式化簡+矩陣)公式矩陣
- 【構造共軛函式+矩陣快速冪】HDU 4565 So Easy! (2013 長沙賽區邀請賽)函式矩陣
- HDU 1575 Tr A(矩陣快速冪)矩陣
- HDU 4686 (推公式+矩陣快速冪)公式矩陣
- HDU 4965 Fast Matrix Calculation(矩陣快速冪)AST矩陣
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- HDU 2256Problem of Precision(矩陣快速冪)矩陣
- HDU 1575 Tr A【矩陣快速冪取模】矩陣
- HDU 1005 Number Sequence:矩陣快速冪矩陣
- HDU5411CRB and Puzzle(矩陣快速冪)矩陣
- 矩陣快速冪矩陣
- HDU 2276 - Kiki & Little Kiki 2 (矩陣快速冪)矩陣
- HDU 4291 A Short problem(矩陣快速冪+迴圈節)矩陣
- HDU3221Brute-force Algorithm(矩陣快速冪&&指數降冪)Go矩陣
- 矩陣快速冪(快忘了)矩陣
- 矩陣快速冪總結矩陣
- 【矩陣乘法】【快速冪】遞推矩陣
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- HDU 4549M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- P3390 【模板】矩陣快速冪矩陣
- 從斐波那契到矩陣快速冪矩陣
- bzoj3240: [Noi2013]矩陣遊戲(矩陣乘法+快速冪)矩陣遊戲
- BZOJ 3329 Xorequ:數位dp + 矩陣快速冪矩陣
- UVA 10655 Contemplation! Algebra (矩陣快速冪)矩陣
- POJ 3150 Cellular Automaton(矩陣快速冪)矩陣
- 費馬小定理 + 費馬大定理 + 勾股數的求解 + 快速冪 + 矩陣快速冪 【模板】矩陣
- POJ 2778-DNA Sequence(AC自動機+構建鄰接矩陣+矩陣快速冪)矩陣
- HDU 2157 How many ways??:矩陣快速冪【i到j共經過k個節點的方法數】矩陣
- 2014多校聯合第9場1006||hdu 4965 矩陣乘法和快速冪矩陣
- hdu 1757 矩陣連乘矩陣
- bzoj4887: [Tjoi2017]可樂(矩陣乘法+快速冪)矩陣
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- LightOJ 1070 Algebraic Problem:矩陣快速冪 + 數學推導AI矩陣
- BZOJ3329: Xorequ(二進位制數位dp 矩陣快速冪)矩陣
- LightOJ 1132 Summing up Powers:矩陣快速冪 + 二項式定理矩陣