Hdoj 1757
描述
Lele now is thinking about a simple function f(x).
If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .
Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
輸入
The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
輸出
For each case, output f(k) % m in one line.
樣例輸入
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
樣例輸出
45
104
思路
很基礎的矩陣快速冪
代碼
#include <cstdio> #include<cstring> #define ll long long #define maxn 10 using namespace std;int k, mod;struct Mat {ll f[maxn][maxn];void cls(){memset(f, 0, sizeof(f));}//全部置為0 Mat() {cls();}friend Mat operator * (Mat a, Mat b){Mat res;for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn; j++)for(int k = 0; k < maxn; k++)(res.f[i][j] += a.f[i][k] * b.f[k][j]) %= mod;return res;} };Mat quick_pow(Mat a) { Mat ans;for(int i = 0; i < maxn; i++) ans.f[i][i] = 1;int b = k;while(b != 0) {if(b & 1) ans = ans * a;b >>= 1;a = a * a;}return ans; }int main() {Mat A, B;for(int i = 0; i < 10; i++)B.f[0][i] = 9 - i;for(int i = 1; i < 10; i++) A.f[i-1][i] = 1;while(~scanf("%d %d", &k, &mod)){Mat C;for(int i = 0; i < 10; i++) scanf("%d", &A.f[i][0]);if(k < 10) {printf("%d\n", k % mod); continue;}k -= 9;C = quick_pow(A);C = B * C;printf("%d\n", C.f[0][0]);}return 0; }轉載于:https://www.cnblogs.com/HackHarry/p/8392106.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: 直通车位置
- 下一篇: spring restful遇到的问题