代码改变世界

杭电2842 Chinese Rings 构建矩阵二分幂

2012-02-23 08:21  javaspring  阅读(187)  评论(0编辑  收藏  举报

       由题意可以求得f(n)=2*f(n-2)+f(n-1)+1,之后构建矩阵用矩阵二分幂就可以解决了。。。。。。。题目:

Chinese Rings

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


Problem Description
Dumbear likes to play the Chinese Rings (Baguenaudier). It’s a game played with nine rings on a bar. The rules of this game are very simple: At first, the nine rings are all on the bar.
The first ring can be taken off or taken on with one step.
If the first k rings are all off and the (k + 1)th ring is on, then the (k + 2)th ring can be taken off or taken on with one step. (0 ≤ k ≤ 7)

Now consider a game with N (N ≤ 1,000,000,000) rings on a bar, Dumbear wants to make all the rings off the bar with least steps. But Dumbear is very dumb, so he wants you to help him.
 

Input
Each line of the input file contains a number N indicates the number of the rings on the bar. The last line of the input file contains a number "0".
 

Output
For each line, output an integer S indicates the least steps. For the integers may be very large, output S mod 200907.
 

Sample Input
1 4 0
 

Sample Output
1 10
 

ac代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int M=200907;
_int64 matrix[3][3];
void init(){
  matrix[0][0]=0;matrix[0][1]=2;matrix[0][2]=0;
  matrix[1][0]=1;matrix[1][1]=1;matrix[1][2]=0;
  matrix[2][0]=0;matrix[2][1]=1;matrix[2][2]=1;
}
void matrixmi(int x){
  _int64 x1,y1,z1,x2,y2,z2,x3,y3,z3;
  x1=(((matrix[0][0]%M)*(matrix[0][0]%M))%M+((matrix[0][1]%M)*(matrix[1][0]%M))%M+((matrix[0][2]%M)*(matrix[2][0]%M))%M)%M;
  y1=(((matrix[0][0]%M)*(matrix[0][1]%M))%M+((matrix[0][1]%M)*(matrix[1][1]%M))%M+((matrix[0][2]%M)*(matrix[2][1]%M))%M)%M;
  z1=(((matrix[0][0]%M)*(matrix[0][2]%M))%M+((matrix[0][1]%M)*(matrix[1][2]%M))%M+((matrix[0][2]%M)*(matrix[2][2]%M))%M)%M;
  x2=(((matrix[1][0]%M)*(matrix[0][0]%M))%M+((matrix[1][1]%M)*(matrix[1][0]%M))%M+((matrix[1][2]%M)*(matrix[2][0]%M))%M)%M;
  y2=(((matrix[1][0]%M)*(matrix[0][1]%M))%M+((matrix[1][1]%M)*(matrix[1][1]%M))%M+((matrix[1][2]%M)*(matrix[2][1]%M))%M)%M;
  z2=(((matrix[1][0]%M)*(matrix[0][2]%M))%M+((matrix[1][1]%M)*(matrix[1][2]%M))%M+((matrix[1][2]%M)*(matrix[2][2]%M))%M)%M;
  x3=(((matrix[2][0]%M)*(matrix[0][0]%M))%M+((matrix[2][1]%M)*(matrix[1][0]%M))%M+((matrix[2][2]%M)*(matrix[2][0]%M))%M)%M;
  y3=(((matrix[2][0]%M)*(matrix[0][1]%M))%M+((matrix[2][1]%M)*(matrix[1][1]%M))%M+((matrix[2][2]%M)*(matrix[2][1]%M))%M)%M;
  z3=(((matrix[2][0]%M)*(matrix[0][2]%M))%M+((matrix[2][1]%M)*(matrix[1][2]%M))%M+((matrix[2][2]%M)*(matrix[2][2]%M))%M)%M;
  matrix[0][0]=x1%M;matrix[0][1]=y1%M;matrix[0][2]=z1%M;matrix[1][0]=x2%M;
  matrix[1][1]=y2%M;matrix[1][2]=z2%M;matrix[2][0]=x3%M;matrix[2][1]=y3%M;matrix[2][2]=z3%M;
  if(x){
    x1=(2*(matrix[1][0]%M))%M;y1=(2*(matrix[1][1]%M))%M;z1=(2*(matrix[1][2]%M))%M;
	x2=((matrix[0][0]%M)+(matrix[1][0]%M))%M;y2=((matrix[0][1]%M)+(matrix[1][1]%M))%M;z2=((matrix[0][2]%M)+(matrix[1][2]%M))%M;
	x3=((matrix[1][0]%M)+(matrix[2][0]%M))%M;y3=((matrix[1][1]%M)+(matrix[2][1]%M))%M;z3=((matrix[1][2]%M)+(matrix[2][2]%M))%M;
	matrix[0][0]=x1%M;matrix[0][1]=y1%M;matrix[0][2]=z1%M;matrix[1][0]=x2%M;matrix[1][1]=y2%M;matrix[1][2]=z2%M;
	matrix[2][0]=x3%M;matrix[2][1]=y3%M;matrix[2][2]=z3%M;
  }
  matrix[0][0]%=M;matrix[0][1]%=M;matrix[0][2]%=M;matrix[1][0]%=M;matrix[1][1]%=M;matrix[1][2]%=M;
  matrix[2][0]%=M;matrix[2][1]%=M;matrix[2][2]%=M;
}
void dfs(int n){
	if(n==1){
	  return;
	}
	dfs(n/2);
	if(n%2)
		matrixmi(1);
	else
		matrixmi(0);
}
int main(){
  _int64 n;
  while(scanf("%I64d",&n),n){
    init();
	if(n==1)
		printf("1\n");
	else if(n==2)
		printf("2\n");
	else{
	  n-=2;
	  dfs(n);
	  _int64 x=((matrix[0][1]%M)+2*(matrix[1][1]%M)%M+(matrix[2][1]%M))%M;
	  printf("%I64d\n",x);
	}
  }
  return 0;
}