2016弱校联盟十一专场10.5 F Fibonacci of Fibonacci(矩阵快速幂+找循环节)
2016-10-26 21:00
429 查看
题目分析
这道题第一步肯定是找循环节,因为要求的是Fn,但是这里的n = Fn,因此需要求循环节,其实就是用map处理一下而已,会发现循环正好是从(0,1)开始的为26880696,这题求余给的数很巧!!然后就可以先对下标进行矩阵快速幂,得到一个数之后再对上面进行矩阵快速幂即可。#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const LL mod = 20160519; const LL MOD = 26880696; struct Matirx{ LL mat[2][2]; }; Matirx Mul1(Matirx a, Matirx b){ Matirx ret; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ ret.mat[i][j] = 0; for(int k = 0; k < 2; k++) ret.mat[i][j] += a.mat[i][k]*b.mat[k][j]; ret.mat[i][j] %= MOD; } } return ret; } Matirx Mul2(Matirx a, Matirx b){ Matirx ret; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ ret.mat[i][j] = 0; for(int k = 0; k < 2; k++) ret.mat[i][j] += a.mat[i][k]*b.mat[k][j]; ret.mat[i][j] %= mod; } } return ret; } Matirx Mat_pow1(Matirx a, int n){ Matirx ret; memset(ret.mat, 0, sizeof(ret.mat)); for(int i = 0; i < 2; i++) ret.mat[i][i] = 1; while(n){ if(n&1) ret = Mul1(ret, a); a = Mul1(a, a); n >>= 1; } return ret; } Matirx Mat_pow2(Matirx a, int n){ Matirx ret; memset(ret.mat, 0, sizeof(ret.mat)); for(int i = 0; i < 2; i++) ret.mat[i][i] = 1LL; while(n){ if(n&1) ret = Mul2(ret, a); a = Mul2(a, a); n >>= 1; } return ret; } void init(Matirx &temp){ temp.mat[0][0] = 1; temp.mat[0][1] = 1; temp.mat[1][0] = 1; temp.mat[1][1] = 0; } int main(){ int T, n; scanf("%d", &T); while(T--){ scanf("%d", &n); Matirx temp; init(temp); Matirx ret = Mat_pow1(temp, n-1); init(temp); Matirx ans = Mat_pow2(temp, ret.mat[0][0]-1); printf("%lld\n", ans.mat[0][0]); } return 0; } #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define LL long long const int maxn = 25; LL bit[maxn], dp[maxn][15][5]; LL dfs(int pos, int pre, int x, bool zero, bool limit){ if(pos < 1) return 1LL; if(!limit && dp[pos][pre][x] != -1) return dp[pos][pre][x]; int len = limit?bit[pos]:9; LL ret = 0; for(int i = 0; i <= len; i++){ if(zero) ret += dfs(pos-1, i, x, zero&&i==0, limit&&i==len); else{ int temp = x; if(i < pre){ if(temp == 0) continue; temp = 1; } else if(i > pre){ if(temp == 1) continue; temp = 0; } ret += dfs(pos-1, i, temp, false, limit&&i==len); } } if(!limit) dp[pos][pre][x] = ret; return ret; } LL solve(LL n){ int len = 0; while(n){ bit[++len] = n%10; n /= 10; } return dfs(len, 0, 2, true, true); } int main(){ int T; LL l, r; scanf("%d", &T); memset(dp, -1, sizeof(dp)); while(T--){ scanf("%lld%lld", &l, &r); printf("%lld\n", solve(r) - solve(l-1)); } return 0; }
相关文章推荐
- 2016弱校联盟十一专场10.5 F Fibonacci of Fibonacci(矩阵快速幂 + 循环节)
- 2016弱校联盟十一专场10.5 F. Fibonacci of Fibonacci
- (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci
- 弱校联萌 Fibonacci of Fibonacci (矩阵快速幂加循环节降幂)
- hdoj 3306 Another kind of Fibonacci 【矩阵快速幂】
- 2016弱校联盟十一专场10.5 I Increasing or Decreasing(数位dp)
- HDU - 3306 Another kind of Fibonacci 矩阵快速幂
- hdu 3306 Another kind of Fibonacci(矩阵快速幂)
- POJ 3735 Training little cats(矩阵乘法 + 稀疏矩阵优化)——2016弱校联盟十一专场10.7(12点场)
- HDU 3306 Another kind of Fibonacci (矩阵快速幂)
- 2016弱校联盟十一专场10.5---As Easy As Possible(倍增)
- (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci (暴力 + 循环节)
- Another kind of Fibonacci(hdu3306)矩阵快速幂
- hdu 3306 Another kind of Fibonacci 矩阵快速幂
- 【循环节】【矩阵乘法】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem F. Fibonacci of Fibonacci
- [HDU 3306] Another kind of Fibonacci · 矩阵快速幂
- HDU 3306 Another kind of Fibonacci(矩阵快速幂)
- hdu3306--Another kind of Fibonacci(矩阵快速幂)
- [弱校联萌2016]2016弱校联盟十一专场10.5
- 2016弱校联盟十一专场10.5(12点场) Increasing or Decreasing