矩阵快速幂模板

Fibonacci

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 14
Problem Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, andFn = Fn − 1 + Fn − 2 forn ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

 

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

 

Output

For each test case, print the last four digits of Fn. If the last four digits ofFn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., printFn mod 10000).

 

Sample Input
  
  
0 9 999999999 1000000000 -1
 

Sample Output
  
  
0 34 626 6875
 

Source
PKU


#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
const int mod = 10000;
#define N 2
struct Matrix
{
    int a[N][N];
};
Matrix A = {1, 1, 1, 0};
Matrix B = {1, 1, 1, 0};
void mul(int A[][N], int B[][N])
{
    int R[N][N] = zero;
    int sum;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            sum = 0;
            for (int k = 0; k < N; k++)
            {
                sum += A[i][k] * B[k][j];
            }
            R[i][j] = sum % mod;
        }
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            A[i][j] = R[i][j];
        }
    }
}
void cal(Matrix &R, int n)
{
    if (n == 1)
    {
        R = B;
        return;
    }
    cal(R, n / 2);
    mul(R.a, R.a);
    if (n & 1)mul(R.a, B.a);
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n;
    while (scanf("%d", &n), n != -1)
    {
        if (n == 0)
        {
            printf("0\n");
            continue;
        }
        cal(A, n);
        printf("%d\n", A.a[0][1]);
    }

    return 0;
}

模板2

题目

A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2235    Accepted Submission(s): 1305


Problem Description
Lele now is thinking about a simple function f(x).

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
 

Input
The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
 

Output
For each case, output f(k) % m in one line.
 

Sample Input
  
  
10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0
 

Sample Output
  
  
45 104
 

Author
linle
 

Source
 


#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
struct node
{
    int m[10][10];
};
node b, tp, res, init;
int n, m;
node juzhen(node a, node b)
{
    node c;
    int i, j, k;
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; j++)
        {
            c.m[i][j] = 0;
            for (int k = 0; k < 10; k++)
            {
                c.m[i][j] += a.m[i][k] * b.m[k][j];
            }
            c.m[i][j] %= m;
        }
    }
    return c;
}
void f(int temp)
{
    tp = b;
    b = res;
    while (temp)
    {
        if (temp & 1)
            b = juzhen(b, tp);
        tp = juzhen(tp, tp);
        temp = temp >> 1;
    }
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int i, j, sum;
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; j++)
        {
            if (i - 1 == j)
                init.m[i][j] = 1;
            else
                init.m[i][j] = 0;
        }
    }
    while (scanf("%d%d", &n, &m) + 1)
    {
        b = init;
        for (i = 0; i < 10; i++)
            scanf("%d", &b.m[0][i]);
        for (i = 0; i < 10; i++)
        {
            for (j = 0; j < 10; j++)
            {
                if (i == j)
                    res.m[i][j] = 1;
                else
                    res.m[i][j] = 0;
            }
        }
        if (n >= 10)
        {
            sum = 0;
            f(n - 9);
            for (i = 0; i < 10; i++)
                sum += (b.m[0][i] * (9 - i))%m;
            printf("%d\n", sum % m);
        }
        else
            printf("%d\n", n % m);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值