ural 1013

1013. K-based Numbers. Version 3

Time limit: 0.5 second
Memory limit: 64 MB
Let’s consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-based notation doesn’t contain two successive zeros. For example:
  • 1010230 is a valid 7-digit number;
  • 1000198 is not a valid number;
  • 0001235 is not a 7-digit number, it is a 4-digit number.
Given three numbers NK and M, you are to calculate an amount of valid K based numbers, containing N digits modulo M.
You may assume that 2 ≤ NKM ≤ 1018.

Input

The numbers NK and M in decimal notation separated by the line break.

Output

The result in decimal notation.

Sample

input output
2
10
100
90

數據量又大了點。。 直接快速冪 第一次java寫快速冪... 好蛋疼。。
import java.math.BigInteger;
import java.util.Scanner;

class Matrix{
	public BigInteger mod;
	public BigInteger d[][] = new BigInteger[3][3];
	public Matrix(Boolean flag, BigInteger _mod){
		for(int i=1; i<=2; i++) for(int j=1; j<=2; j++) d[i][j] = BigInteger.ZERO;	
		if(flag == true)
			d[1][1] = d[2][2] = BigInteger.ONE;
		mod = _mod;
	}
	Matrix mul(Matrix b){
		Matrix res = new Matrix(false, mod);
		for(int i=1; i<=2; i++)
			for(int j=1; j<=2; j++)
				for(int k=1; k<=2; k++)
					res.d[i][j] = res.d[i][j].add(d[i][k].multiply(b.d[k][j]).mod(mod)).mod(mod);
		return res;
	}
	Matrix pow(long b){
		Matrix res = new Matrix(true, mod);
		Matrix a = this;
		while(b != 0){
			if((b%2) != 0) res = res.mul(a);  
			b/=2;
			a = a.mul(a);
		}
		return res;
	}
}
public class Main {
	static Scanner s = new Scanner(System.in);
	public static void main(String[] args) {
		long n =  s.nextLong();
		BigInteger k = s.nextBigInteger();
		BigInteger mod = s.nextBigInteger();
		Matrix st = new Matrix(false, mod);
		st.d[1][1] = k.subtract(BigInteger.ONE);
		st.d[1][2] = k.subtract(BigInteger.ONE);
		st.d[2][1] = BigInteger.ONE;
		st.d[2][2] = BigInteger.ZERO;
		st = st.pow(n-1);
		
		BigInteger res1 = k.subtract(BigInteger.ONE).multiply(st.d[1][1]).mod(mod);
		BigInteger res2 = k.subtract(BigInteger.ONE).multiply(st.d[2][1]).mod(mod);
		BigInteger res = res1.add(res2).mod(mod);
		System.out.println(res);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章