【HDU】4291 A Short problem 矩阵快速幂

A Short problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1486    Accepted Submission(s): 540


Problem Description
  According to a research, VIM users tend to have shorter fingers, compared with Emacs users.
  Hence they prefer problems short, too. Here is a short one:
  Given n (1 <= n <= 1018), You should solve for
g(g(g(n))) mod 109 + 7

  where
g(n) = 3g(n - 1) + g(n - 2)

g(1) = 1

g(0) = 0

 

Input
  There are several test cases. For each test case there is an integer n in a single line.
  Please process until EOF (End Of File).
 

Output
  For each test case, please print a single line with a integer, the corresponding answer to this case.
 

Sample Input
  
  
0 1 2
 

Sample Output
  
  
0 1 42837
 

Source
2012 ACM/ICPC Asia Regional Chengdu Online

传送门:【HDU】4291 A Short problem


题目分析:
mod1 = 1e9 + 7 ,g(x) = 3 * g(x - 1) + g(x - 2)
首先暴力找循环节,先对外层g(x)%mod1,设a = g(1),b = g(0),则如果循环到i的时候g(i) == a && g(i-1) == b则x的循环节就是mod2 = i - 1。
那么对任意的x%mod2后带入g函数中与原样带入是等价的。
接下来设x = f(y),然后对该层f(y)%mod2,同样设a = f(1),b = f(0),则同样循环到j的时候满足f(j) == a && f(j-1) == b则y的循环节是mod3 = j - 1。
那么对于任意的y%mod3后带入f函数中与原样带入依旧等价。
所以我们得到了两个循环节再加上最初的mod1就构成了三重循环节。
所以,g(g(g(n)))%mod1 = g(g(g(n)%mod3)%mod2)%mod1。
其中
mod1 = 1000000007 ;
mod2 = 222222224 ;
mod3 = 183120 ;
问题得到完美解决~

代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )

typedef long long LL ;

const int MOD1 = 1000000007 ;
const int MOD2 = 222222224 ;
const int MOD3 = 183120 ;
const int MAXN = 2 ;

struct Matrix {
	int mat[MAXN][MAXN] ;
	int N ;
	
	Matrix () {}
	
	Matrix ( int n ) {
		N = n ;
		REP ( i , N )
			REP ( j , N )
				mat[i][j] = 0 ;
	}
	
	void init () {
		REP ( i , N )
			mat[i][i] = 1 ;
	}
	
	void build () {
		mat[0][0] = 3 ;
		mat[0][1] = 1 ;
		mat[1][0] = 1 ;
		mat[1][1] = 0 ;
	}
} ;

Matrix E , A ;

Matrix mul ( Matrix a , Matrix b , int mod ) {
	Matrix res = Matrix ( a.N ) ;
	REP ( i , a.N )
		REP ( j , a.N )
			REP ( k , a.N )
				res.mat[i][j] = ( res.mat[i][j] + ( LL ) a.mat[i][k] * b.mat[k][j] % mod ) % mod ;
	return res ;
}

LL pow ( LL k , int mod ) {
	Matrix res = E , tmp = A ;
	while ( k ) {
		if ( k & 1 )
			res = mul ( res , tmp , mod ) ;
		tmp = mul ( tmp , tmp , mod ) ;
		k >>= 1 ;
	}
	return  res.mat[0][0] ;
}

void work () {
	LL k ;
	E = Matrix ( 2 ) ;
	A = Matrix ( 2 ) ;
	E.init () ;
	A.build () ;
	while ( ~scanf ( "%I64d" , &k ) ) {
		if ( k >= 2 )
			k = pow ( k - 1 , MOD3 ) ;
		if ( k >= 2 )
			k = pow ( k - 1 , MOD2 ) ;
		if ( k >= 2 )
			k = pow ( k - 1 , MOD1 ) ;
		printf ( "%I64d\n" , k ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值