HDU 1757 A Simple Math Problem (矩阵快速幂)

HDU 1757 A Simple Math Problem (矩阵快速幂)

ACM

题目地址:HDU 1757 A Simple Math Problem

题意: 
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); 
问f(k)%m的值。

分析: 
裸矩阵快速幂,式子都给你了,跟HDU 2604 Queuing (矩阵快速幂)一样,试着做看看吧。

代码

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        1757.cpp
*  Create Date: 2014-08-03 09:24:45
*  Descripton:  Matrix quick power 
*/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;

const int N = 20;
const int SIZE = 20;		// max size of the matrix

ll k, MOD;

struct Mat{
	int n;
	ll v[SIZE][SIZE];	// value of matrix

	Mat(int _n = SIZE) {
		n = _n;
		memset(v, 0, sizeof(v));
	}

	void init(ll _v) {
		repf (i, 0, n - 1)
			v[i][i] = _v;
	}

	void output() {
		repf (i, 0, n - 1) {
			repf (j, 0, n - 1)
				printf("%lld ", v[i][j]);
			puts("");
		}
		puts("");
	}
} a, b;

Mat operator * (Mat a, Mat b) {
	Mat c(a.n);
	repf (i, 0, a.n - 1) {
		repf (j, 0, a.n - 1) {
			c.v[i][j] = 0;
			repf (k, 0, a.n - 1) {
				c.v[i][j] += (a.v[i][k] * b.v[k][j]) % MOD;
				c.v[i][j] %= MOD;
			}
		}
	}
	return c;
}

Mat operator ^ (Mat a, ll k) {
	Mat c(a.n);
	c.init(1);
	while (k) {
		if (k&1) c = a * c;
		a = a * a;
		k >>= 1;
	}
	return c;
}

void init() {
	// init
	repf (i, 0, 9)
		a.v[9 - i][0] = i;
	repf (i, 0, 8)
		b.v[i + 1][i] = 1;
}

void solve() {
	// read
	repf (i, 0, 9)
		scanf("%lld", &b.v[0][i]);

	Mat c;
	if (k <= 9) {
		printf("%lld\n", k % MOD);
	} else {
		c = b ^ (k - 9);
//		c.output();
		c = c * a;
		printf("%lld\n", c.v[0][0] % MOD);
	}
}

int main() {
	init();
	while (~scanf("%lld%lld", &k, &MOD)) {
		solve();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值