ural 1013
来源:互联网 发布:帝国cms整站模板 编辑:程序博客网 时间:2024/05/16 16:16
1013. K-based Numbers. Version 3
Time limit: 0.5 second
Memory limit: 64 MB
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 N, K and M, you are to calculate an amount of valid K based numbers, containing N digits modulo M.
You may assume that 2 ≤ N, K, M ≤ 1018.
Input
The numbers N, K and M in decimal notation separated by the line break.
Output
The result in decimal notation.
Sample
210100
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);}}
0 0
- URAL 1009/URAL 1012/URAL 1013
- ural 1013
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- Machine Learning 网络资源
- BZOJ 1121 [POI2008]激光发射器SZK 数学
- java 模拟多个客户端与服务器建立UDP连接
- iOS问题:[UIButton setCurrentPage:]: unrecognized selector sent to instance
- HDU 5000 Clone (多重背包)
- ural 1013
- UIday1502:XML 、JSON 数据结构解析
- Java基础知识记录
- cmd命令大全
- java io流的一部分解析
- Java习题4—IO流与异常
- Linux文件系统
- iOS 平板与 Android 平板的交互与动画
- 对《Java初体验》中代码的再探究